Skip to content

Instantly share code, notes, and snippets.

@akvanhar
Last active October 17, 2018 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akvanhar/af3cdde975f12d90d0332768cff0f777 to your computer and use it in GitHub Desktop.
Save akvanhar/af3cdde975f12d90d0332768cff0f777 to your computer and use it in GitHub Desktop.
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