Skip to content

Commit

Permalink
Fix missing shutdown of the batch processor (open-telemetry#1186)
Browse files Browse the repository at this point in the history
Signed-off-by: Hui Kang <kangh@us.ibm.com>

Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>
  • Loading branch information
huikang and Aneurysm9 authored Sep 20, 2020
1 parent 995be31 commit 930b4d0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Fixed

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

## [0.11.0] - 2020-08-24

Expand Down
20 changes: 11 additions & 9 deletions example/otel-collector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import (

// Initializes an OTLP exporter, and configures the corresponding trace and
// metric providers.
func initProvider() (*otlp.Exporter, *push.Controller) {
func initProvider() func() {

// If the OpenTelemetry Collector is running on a local cluster (minikube or
// microk8s), it should be accessible through the NodePort service at the
Expand All @@ -54,13 +54,14 @@ func initProvider() (*otlp.Exporter, *push.Controller) {
)
handleErr(err, "failed to create exporter")

bsp := sdktrace.NewBatchSpanProcessor(exp)
tracerProvider := sdktrace.NewProvider(
sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}),
sdktrace.WithResource(resource.New(
// the service name used to display traces in backends
semconv.ServiceNameKey.String("test-service"),
)),
sdktrace.WithBatcher(exp),
sdktrace.WithSpanProcessor(bsp),
)

pusher := push.New(
Expand All @@ -76,17 +77,18 @@ func initProvider() (*otlp.Exporter, *push.Controller) {
global.SetMeterProvider(pusher.Provider())
pusher.Start()

return exp, pusher
return func() {
bsp.Shutdown() // shutdown the processor
handleErr(exp.Shutdown(context.Background()), "failed to stop exporter")
pusher.Stop() // pushes any last exports to the receiver
}
}

func main() {
log.Printf("Waiting for connection...")

exp, pusher := initProvider()
defer func() {
handleErr(exp.Shutdown(context.Background()), "failed to stop exporter")
}()
defer pusher.Stop() // pushes any last exports to the receiver
shutdown := initProvider()
defer shutdown()

tracer := global.Tracer("test-tracer")
meter := global.Meter("test-meter")
Expand All @@ -112,6 +114,7 @@ func main() {
context.Background(),
"CollectorExporter-Example",
apitrace.WithAttributes(commonLabels...))
defer span.End()
for i := 0; i < 10; i++ {
_, iSpan := tracer.Start(ctx, fmt.Sprintf("Sample-%d", i))
log.Printf("Doing really hard work (%d / 10)\n", i+1)
Expand All @@ -122,7 +125,6 @@ func main() {
}

log.Printf("Done!")
span.End()
}

func handleErr(err error, message string) {
Expand Down

0 comments on commit 930b4d0

Please sign in to comment.