1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-02-09 13:37:12 +02:00

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

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

* chore: improves variable name

Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>

* chore(zipkin): makes lint happy.

* fix(zipkin): fixes example

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>

* fix(zipkin): import.

Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>
Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
This commit is contained in:
José Carlos Chávez 2021-03-08 20:23:15 +01:00 committed by GitHub
parent 3dc91f2d76
commit e981475827
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 7 deletions

View File

@ -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

View File

@ -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),
@ -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) {