Skip to content

Instantly share code, notes, and snippets.

@akvanhar akvanhar/opentracing.go Secret
Last active Oct 17, 2018

Embed
What would you like to do?
OpenTracing example app
package main
import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"log"
"os"
opentracing "github.com/opentracing/opentracing-go"
jaeger "github.com/uber/jaeger-client-go"
"github.com/uber/jaeger-client-go/transport/zipkin"
)
func main() {
br := bufio.NewReader(os.Stdin)
backendURI := "http://localhost:9411/api/v1/spans"
transport, _ := zipkin.NewHTTPTransport(backendURI)
reporter := jaeger.NewRemoteReporter(transport)
sampler := jaeger.NewConstSampler(true)
zstracer, zscloser := jaeger.NewTracer("opentracing-events", sampler, reporter)
opentracing.SetGlobalTracer(zstracer)
defer zscloser.Close()
for {
if err := readEvaluateProcess(br); err != nil {
if err == io.EOF {
return
}
log.Fatal(err)
}
}
}
func readEvaluateProcess(br *bufio.Reader) error {
span, ctx := opentracing.StartSpanFromContext(context.Background(), "repl")
defer span.Finish()
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) {
span, ctx := opentracing.StartSpanFromContext(ctx, "readLine")
defer span.Finish()
line, _, err := br.ReadLine()
span.SetTag("input", string(line))
if err != nil {
return nil, err
}
return line, err
}
func processLine(ctx context.Context, in []byte) (out []byte, err error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "processLine")
output := bytes.ToUpper(in)
span.SetTag("output", string(output))
defer span.Finish()
return output, 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.