mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-01-18 03:22:12 +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:
parent
995be31f42
commit
930b4d01f6
@ -49,6 +49,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
|
|||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
- Zipkin example no longer mentions `ParentSampler`, corrected to `ParentBased`. (#1171)
|
- 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
|
## [0.11.0] - 2020-08-24
|
||||||
|
|
||||||
|
@ -40,7 +40,7 @@ import (
|
|||||||
|
|
||||||
// Initializes an OTLP exporter, and configures the corresponding trace and
|
// Initializes an OTLP exporter, and configures the corresponding trace and
|
||||||
// metric providers.
|
// metric providers.
|
||||||
func initProvider() (*otlp.Exporter, *push.Controller) {
|
func initProvider() func() {
|
||||||
|
|
||||||
// If the OpenTelemetry Collector is running on a local cluster (minikube or
|
// If the OpenTelemetry Collector is running on a local cluster (minikube or
|
||||||
// microk8s), it should be accessible through the NodePort service at the
|
// 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")
|
handleErr(err, "failed to create exporter")
|
||||||
|
|
||||||
|
bsp := sdktrace.NewBatchSpanProcessor(exp)
|
||||||
tracerProvider := sdktrace.NewProvider(
|
tracerProvider := sdktrace.NewProvider(
|
||||||
sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}),
|
sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}),
|
||||||
sdktrace.WithResource(resource.New(
|
sdktrace.WithResource(resource.New(
|
||||||
// the service name used to display traces in backends
|
// the service name used to display traces in backends
|
||||||
semconv.ServiceNameKey.String("test-service"),
|
semconv.ServiceNameKey.String("test-service"),
|
||||||
)),
|
)),
|
||||||
sdktrace.WithBatcher(exp),
|
sdktrace.WithSpanProcessor(bsp),
|
||||||
)
|
)
|
||||||
|
|
||||||
pusher := push.New(
|
pusher := push.New(
|
||||||
@ -76,17 +77,18 @@ func initProvider() (*otlp.Exporter, *push.Controller) {
|
|||||||
global.SetMeterProvider(pusher.Provider())
|
global.SetMeterProvider(pusher.Provider())
|
||||||
pusher.Start()
|
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() {
|
func main() {
|
||||||
log.Printf("Waiting for connection...")
|
log.Printf("Waiting for connection...")
|
||||||
|
|
||||||
exp, pusher := initProvider()
|
shutdown := initProvider()
|
||||||
defer func() {
|
defer shutdown()
|
||||||
handleErr(exp.Shutdown(context.Background()), "failed to stop exporter")
|
|
||||||
}()
|
|
||||||
defer pusher.Stop() // pushes any last exports to the receiver
|
|
||||||
|
|
||||||
tracer := global.Tracer("test-tracer")
|
tracer := global.Tracer("test-tracer")
|
||||||
meter := global.Meter("test-meter")
|
meter := global.Meter("test-meter")
|
||||||
@ -112,6 +114,7 @@ func main() {
|
|||||||
context.Background(),
|
context.Background(),
|
||||||
"CollectorExporter-Example",
|
"CollectorExporter-Example",
|
||||||
apitrace.WithAttributes(commonLabels...))
|
apitrace.WithAttributes(commonLabels...))
|
||||||
|
defer span.End()
|
||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 10; i++ {
|
||||||
_, iSpan := tracer.Start(ctx, fmt.Sprintf("Sample-%d", i))
|
_, iSpan := tracer.Start(ctx, fmt.Sprintf("Sample-%d", i))
|
||||||
log.Printf("Doing really hard work (%d / 10)\n", i+1)
|
log.Printf("Doing really hard work (%d / 10)\n", i+1)
|
||||||
@ -122,7 +125,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Done!")
|
log.Printf("Done!")
|
||||||
span.End()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleErr(err error, message string) {
|
func handleErr(err error, message string) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user