You've already forked opentelemetry-go
mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-07-15 01:04:25 +02:00
Add a Shutdown method to sdk TraceProvider (#1227)
* Add a Shutdown method to api TraceProvider - sdktraceprovider shutdown span processors - In examples, replace processosr shutdown with traceprovider's shutdown Signed-off-by: Hui Kang <kangh@us.ibm.com> * remove shutdown in the api provider interface * Add context in parameter and return error * handle error in shutdown * Update CHANGELOG.md Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
This commit is contained in:
@ -52,6 +52,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
|
|||||||
- OTLP Metric exporter supports Histogram aggregation. (#1209)
|
- OTLP Metric exporter supports Histogram aggregation. (#1209)
|
||||||
- The `Code` struct from the `go.opentelemetry.io/otel/codes` package now supports JSON marshaling and unmarshaling as well as implements the `Stringer` interface. (#1214)
|
- The `Code` struct from the `go.opentelemetry.io/otel/codes` package now supports JSON marshaling and unmarshaling as well as implements the `Stringer` interface. (#1214)
|
||||||
- A Baggage API to implement the OpenTelemetry specification. (#1217)
|
- A Baggage API to implement the OpenTelemetry specification. (#1217)
|
||||||
|
- Add Shutdown method to sdk/trace/provider, shutdown processors in the order they were registered. (#1227)
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
|
@ -46,8 +46,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bsp := sdktrace.NewBatchSpanProcessor(exporter)
|
bsp := sdktrace.NewBatchSpanProcessor(exporter)
|
||||||
defer bsp.Shutdown()
|
|
||||||
tp := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(bsp))
|
tp := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(bsp))
|
||||||
|
defer func() { _ = tp.Shutdown(context.Background()) }()
|
||||||
pusher := push.New(
|
pusher := push.New(
|
||||||
basic.New(
|
basic.New(
|
||||||
simple.NewWithExactDistribution(),
|
simple.NewWithExactDistribution(),
|
||||||
|
@ -35,12 +35,12 @@ var (
|
|||||||
var tp *sdktrace.TracerProvider
|
var tp *sdktrace.TracerProvider
|
||||||
|
|
||||||
// initTracer creates and registers trace provider instance.
|
// initTracer creates and registers trace provider instance.
|
||||||
func initTracer() func() {
|
func initTracer() {
|
||||||
var err error
|
var err error
|
||||||
exp, err := stdout.NewExporter(stdout.WithPrettyPrint())
|
exp, err := stdout.NewExporter(stdout.WithPrettyPrint())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("failed to initialize stdout exporter %v\n", err)
|
log.Panicf("failed to initialize stdout exporter %v\n", err)
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
bsp := sdktrace.NewBatchSpanProcessor(exp)
|
bsp := sdktrace.NewBatchSpanProcessor(exp)
|
||||||
tp = sdktrace.NewTracerProvider(
|
tp = sdktrace.NewTracerProvider(
|
||||||
@ -52,17 +52,16 @@ func initTracer() func() {
|
|||||||
sdktrace.WithSpanProcessor(bsp),
|
sdktrace.WithSpanProcessor(bsp),
|
||||||
)
|
)
|
||||||
global.SetTracerProvider(tp)
|
global.SetTracerProvider(tp)
|
||||||
return bsp.Shutdown
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// initialize trace provider.
|
// initialize trace provider.
|
||||||
shutdown := initTracer()
|
initTracer()
|
||||||
defer shutdown()
|
|
||||||
|
|
||||||
// Create a named tracer with package path as its name.
|
// Create a named tracer with package path as its name.
|
||||||
tracer := tp.Tracer("example/namedtracer/main")
|
tracer := tp.Tracer("example/namedtracer/main")
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
defer func() { _ = tp.Shutdown(ctx) }()
|
||||||
ctx = otel.ContextWithBaggageValues(ctx, fooKey.String("foo1"), barKey.String("bar1"))
|
ctx = otel.ContextWithBaggageValues(ctx, fooKey.String("foo1"), barKey.String("bar1"))
|
||||||
|
|
||||||
var span otel.Span
|
var span otel.Span
|
||||||
|
@ -80,7 +80,7 @@ func initProvider() func() {
|
|||||||
pusher.Start()
|
pusher.Start()
|
||||||
|
|
||||||
return func() {
|
return func() {
|
||||||
bsp.Shutdown() // shutdown the processor
|
handleErr(tracerProvider.Shutdown(context.Background()), "failed to shutdown provider")
|
||||||
handleErr(exp.Shutdown(context.Background()), "failed to stop exporter")
|
handleErr(exp.Shutdown(context.Background()), "failed to stop exporter")
|
||||||
pusher.Stop() // pushes any last exports to the receiver
|
pusher.Stop() // pushes any last exports to the receiver
|
||||||
}
|
}
|
||||||
|
@ -492,6 +492,7 @@ func TestNewExporter_withMultipleAttributeTypes(t *testing.T) {
|
|||||||
sdktrace.WithMaxExportBatchSize(10),
|
sdktrace.WithMaxExportBatchSize(10),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
defer func() { _ = tp.Shutdown(context.Background()) }()
|
||||||
|
|
||||||
tr := tp.Tracer("test-tracer")
|
tr := tp.Tracer("test-tracer")
|
||||||
testKvs := []label.KeyValue{
|
testKvs := []label.KeyValue{
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
package trace
|
package trace
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
||||||
@ -180,6 +181,21 @@ func (p *TracerProvider) ApplyConfig(cfg Config) {
|
|||||||
p.config.Store(&c)
|
p.config.Store(&c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Shutdown shuts down the span processors in the order they were registered
|
||||||
|
func (p *TracerProvider) Shutdown(ctx context.Context) error {
|
||||||
|
spss, ok := p.spanProcessors.Load().(spanProcessorStates)
|
||||||
|
if !ok || len(spss) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, sps := range spss {
|
||||||
|
sps.state.Do(func() {
|
||||||
|
sps.sp.Shutdown()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// WithSyncer registers the exporter with the TracerProvider using a
|
// WithSyncer registers the exporter with the TracerProvider using a
|
||||||
// SimpleSpanProcessor.
|
// SimpleSpanProcessor.
|
||||||
func WithSyncer(e export.SpanExporter) TracerProviderOption {
|
func WithSyncer(e export.SpanExporter) TracerProviderOption {
|
||||||
|
48
sdk/trace/provider_test.go
Normal file
48
sdk/trace/provider_test.go
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
// Copyright The OpenTelemetry Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package trace
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
export "go.opentelemetry.io/otel/sdk/export/trace"
|
||||||
|
)
|
||||||
|
|
||||||
|
type basicSpanProcesor struct {
|
||||||
|
running bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *basicSpanProcesor) Shutdown() {
|
||||||
|
t.running = false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *basicSpanProcesor) OnStart(s *export.SpanData) {}
|
||||||
|
func (t *basicSpanProcesor) OnEnd(s *export.SpanData) {}
|
||||||
|
func (t *basicSpanProcesor) ForceFlush() {}
|
||||||
|
|
||||||
|
func TestShutdownTraceProvider(t *testing.T) {
|
||||||
|
stp := NewTracerProvider()
|
||||||
|
sp := &basicSpanProcesor{}
|
||||||
|
stp.RegisterSpanProcessor(sp)
|
||||||
|
|
||||||
|
sp.running = true
|
||||||
|
|
||||||
|
_ = stp.Shutdown(context.Background())
|
||||||
|
|
||||||
|
if sp.running != false {
|
||||||
|
t.Errorf("Error shutdown basicSpanProcesor\n")
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user