Skip to content

Commit

Permalink
chore(zipkin): improves zipkin example to not to depend on timeouts. (#…
Browse files Browse the repository at this point in the history
…1566)

* chore(zipkin): improves zipkin example to not to depend on timeouts.

* chore: improves variable name

Co-authored-by: Anthony Mirabella <[email protected]>

* chore(zipkin): makes lint happy.

* fix(zipkin): fixes example

Co-authored-by: Tyler Yahn <[email protected]>

* fix(zipkin): import.

Co-authored-by: Anthony Mirabella <[email protected]>
Co-authored-by: Tyler Yahn <[email protected]>
  • Loading branch information
3 people authored Mar 8, 2021
1 parent 3dc91f2 commit e981475
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
1 change: 1 addition & 0 deletions example/zipkin/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
go.opentelemetry.io/otel v0.18.0
go.opentelemetry.io/otel/exporters/trace/zipkin v0.18.0
go.opentelemetry.io/otel/sdk v0.18.0
go.opentelemetry.io/otel/trace v0.18.0
)

replace go.opentelemetry.io/otel/bridge/opencensus => ../../bridge/opencensus
Expand Down
22 changes: 15 additions & 7 deletions example/zipkin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,19 @@ import (
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/trace/zipkin"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
)

var logger = log.New(os.Stderr, "zipkin-example", log.Ldate|log.Ltime|log.Llongfile)

// initTracer creates a new trace provider instance and registers it as global trace provider.
func initTracer(url string) {
func initTracer(url string) func() {
// Create Zipkin Exporter and install it as a global tracer.
//
// For demoing purposes, always sample. In a production application, you should
// configure the sampler to a trace.ParentBased(trace.TraceIDRatioBased) set at the desired
// ratio.
err := zipkin.InstallNewPipeline(
exporter, err := zipkin.NewRawExporter(
url,
"zipkin-test",
zipkin.WithLogger(logger),
Expand All @@ -46,25 +47,32 @@ func initTracer(url string) {
if err != nil {
log.Fatal(err)
}

batcher := sdktrace.NewBatchSpanProcessor(exporter)

tp := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(batcher))
otel.SetTracerProvider(tp)

return func() {
_ = tp.Shutdown(context.Background())
}
}

func main() {
url := flag.String("zipkin", "http://localhost:9411/api/v2/spans", "zipkin url")
flag.Parse()

initTracer(*url)
shutdown := initTracer(*url)
defer shutdown()

ctx := context.Background()

tr := otel.GetTracerProvider().Tracer("component-main")
ctx, span := tr.Start(ctx, "foo")
ctx, span := tr.Start(ctx, "foo", trace.WithSpanKind(trace.SpanKindServer))
<-time.After(6 * time.Millisecond)
bar(ctx)
<-time.After(6 * time.Millisecond)
span.End()

// Wait for the spans to be exported.
<-time.After(5 * time.Second)
}

func bar(ctx context.Context) {
Expand Down

0 comments on commit e981475

Please sign in to comment.