1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-30 21:20:04 +02:00

Fix missing shutdown of the batch processor (#1186)

Signed-off-by: Hui Kang <kangh@us.ibm.com>

Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>
This commit is contained in:
huikang 2020-09-20 13:40:47 -04:00 committed by GitHub
parent 995be31f42
commit 930b4d01f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 9 deletions

View File

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

View File

@ -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
@ -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(
@ -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")
@ -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)
@ -122,7 +125,6 @@ func main() {
}
log.Printf("Done!")
span.End()
}
func handleErr(err error, message string) {