Skip to content

Instantly share code, notes, and snippets.

@akvanhar akvanhar/opencensus-zipkin.go Secret
Created Oct 17, 2018

Embed
What would you like to do?
OpenCensus's Zipkin example implementation
package main
import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"log"
"os"
"go.opencensus.io/exporter/zipkin"
"go.opencensus.io/trace"
openzipkin "github.com/openzipkin/zipkin-go"
zipkinHTTP "github.com/openzipkin/zipkin-go/reporter/http"
)
// To run, first start your local zipkin in docker with:
// docker run -d -p 9411:9411 openzipkin/zipkin
// Tutorial here: https://opencensus.io/codelabs/zipkin/#0
func main() {
localEndpointURI := "192.168.1.5:5454"
reporterURI := "http://localhost:9411/api/v2/spans"
serviceName := "server"
br := bufio.NewReader(os.Stdin)
localEndpoint, err := openzipkin.NewEndpoint(serviceName, localEndpointURI)
if err != nil {
log.Fatalf("Failed to create Zipkin localEndpoint with URI %q error: %v", localEndpointURI, err)
}
reporter := zipkinHTTP.NewReporter(reporterURI)
ze := zipkin.NewExporter(reporter, localEndpoint)
// And now finally register it as a Trace Exporter
trace.RegisterExporter(ze)
// For demo purposes, set the trace sampling probability to be high
trace.ApplyConfig(trace.Config{DefaultSampler: trace.ProbabilitySampler(1.0)})
for {
if err := readEvaluateProcess(br); err != nil {
if err == io.EOF {
return
}
log.Fatal(err)
}
}
}
func readEvaluateProcess(br *bufio.Reader) error {
ctx, span := trace.StartSpan(context.Background(), "repl")
defer span.End()
fmt.Printf("> ")
line, err := readLine(ctx, br)
if err != nil {
return err
}
out, err := processLine(ctx, line)
if err != nil {
return err
}
fmt.Printf("< %s\n\n", out)
return nil
}
func readLine(ctx context.Context, br *bufio.Reader) ([]byte, error) {
ctx, span := trace.StartSpan(ctx, "readLine")
defer span.End()
line, _, err := br.ReadLine()
if err != nil {
return nil, err
}
return line, err
}
func processLine(ctx context.Context, in []byte) (out []byte, err error) {
_, span := trace.StartSpan(ctx, "processLine")
defer span.End()
return bytes.ToUpper(in), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.