Skip to content

Commit

Permalink
ensure batch span processors are shutdown in examples (#1199)
Browse files Browse the repository at this point in the history
  • Loading branch information
dashpole authored Sep 24, 2020
1 parent 1e72b51 commit 304d4cd
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- Zipkin example no longer mentions `ParentSampler`, corrected to `ParentBased`. (#1171)
- Fix missing shutdown processor in otel-collector example. (#1186)
- Fix missing shutdown processor in basic and namedtracer examples. (#1197)

## [0.11.0] - 2020-08-24

Expand Down
1 change: 1 addition & 0 deletions example/basic/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ replace (
require (
go.opentelemetry.io/otel v0.11.0
go.opentelemetry.io/otel/exporters/stdout v0.11.0
go.opentelemetry.io/otel/sdk v0.11.0
)
22 changes: 20 additions & 2 deletions example/basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import (
"go.opentelemetry.io/otel/api/trace"
"go.opentelemetry.io/otel/exporters/stdout"
"go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/sdk/metric/controller/push"
"go.opentelemetry.io/otel/sdk/metric/processor/basic"
"go.opentelemetry.io/otel/sdk/metric/selector/simple"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)

var (
Expand All @@ -34,14 +38,28 @@ var (
)

func main() {
pusher, err := stdout.InstallNewPipeline([]stdout.Option{
exporter, err := stdout.NewExporter([]stdout.Option{
stdout.WithQuantiles([]float64{0.5, 0.9, 0.99}),
stdout.WithPrettyPrint(),
}, nil)
}...)
if err != nil {
log.Fatalf("failed to initialize stdout export pipeline: %v", err)
}

bsp := sdktrace.NewBatchSpanProcessor(exporter)
defer bsp.Shutdown()
tp := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(bsp))
pusher := push.New(
basic.New(
simple.NewWithExactDistribution(),
exporter,
),
exporter,
)
pusher.Start()
defer pusher.Stop()
global.SetTracerProvider(tp)
global.SetMeterProvider(pusher.MeterProvider())

tracer := global.Tracer("ex.com/basic")
meter := global.Meter("ex.com/basic")
Expand Down
11 changes: 7 additions & 4 deletions example/namedtracer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,30 @@ var (
var tp *sdktrace.TracerProvider

// initTracer creates and registers trace provider instance.
func initTracer() {
func initTracer() func() {
var err error
exp, err := stdout.NewExporter(stdout.WithPrettyPrint())
if err != nil {
log.Panicf("failed to initialize stdout exporter %v\n", err)
return
return nil
}
bsp := sdktrace.NewBatchSpanProcessor(exp)
tp = sdktrace.NewTracerProvider(
sdktrace.WithConfig(
sdktrace.Config{
DefaultSampler: sdktrace.AlwaysSample(),
},
),
sdktrace.WithBatcher(exp),
sdktrace.WithSpanProcessor(bsp),
)
global.SetTracerProvider(tp)
return bsp.Shutdown
}

func main() {
// initialize trace provider.
initTracer()
shutdown := initTracer()
defer shutdown()

// Create a named tracer with package path as its name.
tracer := tp.Tracer("example/namedtracer/main")
Expand Down

0 comments on commit 304d4cd

Please sign in to comment.