mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-01-26 03:52:03 +02:00
move global trace provider api to global package. (#240)
* move global trace provider api to global package. * fix doc.
This commit is contained in:
parent
918efcacc1
commit
68310ab974
@ -21,10 +21,11 @@ import (
|
||||
"go.opentelemetry.io/api/key"
|
||||
"go.opentelemetry.io/api/metric"
|
||||
"go.opentelemetry.io/api/trace"
|
||||
"go.opentelemetry.io/global"
|
||||
)
|
||||
|
||||
var (
|
||||
tracer = trace.GlobalProvider().GetTracer("ex.com/basic")
|
||||
tracer = global.TraceProvider().GetTracer("ex.com/basic")
|
||||
|
||||
meter = metric.GlobalMeter() // TODO: should share resources ^^^?
|
||||
|
||||
|
@ -30,6 +30,7 @@ import (
|
||||
"go.opentelemetry.io/api/key"
|
||||
"go.opentelemetry.io/api/trace"
|
||||
"go.opentelemetry.io/exporter/trace/stackdriver"
|
||||
"go.opentelemetry.io/global"
|
||||
"go.opentelemetry.io/plugin/httptrace"
|
||||
sdktrace "go.opentelemetry.io/sdk/trace"
|
||||
)
|
||||
@ -53,12 +54,12 @@ func initTracer() {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
trace.SetGlobalProvider(tp)
|
||||
global.SetTraceProvider(tp)
|
||||
}
|
||||
|
||||
func main() {
|
||||
initTracer()
|
||||
tr := trace.GlobalProvider().GetTracer("stackdriver/example/client")
|
||||
tr := global.TraceProvider().GetTracer("stackdriver/example/client")
|
||||
|
||||
client := http.DefaultClient
|
||||
ctx := distributedcontext.NewContext(context.Background(),
|
||||
|
@ -23,6 +23,7 @@ import (
|
||||
"go.opentelemetry.io/api/distributedcontext"
|
||||
"go.opentelemetry.io/api/trace"
|
||||
"go.opentelemetry.io/exporter/trace/stackdriver"
|
||||
"go.opentelemetry.io/global"
|
||||
"go.opentelemetry.io/plugin/httptrace"
|
||||
sdktrace "go.opentelemetry.io/sdk/trace"
|
||||
)
|
||||
@ -46,13 +47,13 @@ func initTracer() {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
trace.SetGlobalProvider(tp)
|
||||
global.SetTraceProvider(tp)
|
||||
}
|
||||
|
||||
func main() {
|
||||
initTracer()
|
||||
|
||||
tr := trace.GlobalProvider().GetTracer("stackdriver/example/server")
|
||||
tr := global.TraceProvider().GetTracer("stackdriver/example/server")
|
||||
|
||||
helloHandler := func(w http.ResponseWriter, req *http.Request) {
|
||||
attrs, entries, spanCtx := httptrace.Extract(req.Context(), req)
|
||||
|
@ -29,6 +29,7 @@ import (
|
||||
"go.opentelemetry.io/api/key"
|
||||
"go.opentelemetry.io/api/trace"
|
||||
"go.opentelemetry.io/exporter/trace/stdout"
|
||||
"go.opentelemetry.io/global"
|
||||
"go.opentelemetry.io/plugin/httptrace"
|
||||
sdktrace "go.opentelemetry.io/sdk/trace"
|
||||
)
|
||||
@ -49,7 +50,7 @@ func initTracer() {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
trace.SetGlobalProvider(tp)
|
||||
global.SetTraceProvider(tp)
|
||||
}
|
||||
|
||||
func main() {
|
||||
@ -62,7 +63,7 @@ func main() {
|
||||
|
||||
var body []byte
|
||||
|
||||
tr := trace.GlobalProvider().GetTracer("example/client")
|
||||
tr := global.TraceProvider().GetTracer("example/client")
|
||||
err := tr.WithSpan(ctx, "say hello",
|
||||
func(ctx context.Context) error {
|
||||
req, _ := http.NewRequest("GET", "http://localhost:7777/hello", nil)
|
||||
|
@ -22,6 +22,7 @@ import (
|
||||
"go.opentelemetry.io/api/distributedcontext"
|
||||
"go.opentelemetry.io/api/trace"
|
||||
"go.opentelemetry.io/exporter/trace/stdout"
|
||||
"go.opentelemetry.io/global"
|
||||
"go.opentelemetry.io/plugin/httptrace"
|
||||
sdktrace "go.opentelemetry.io/sdk/trace"
|
||||
)
|
||||
@ -42,12 +43,12 @@ func initTracer() {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
trace.SetGlobalProvider(tp)
|
||||
global.SetTraceProvider(tp)
|
||||
}
|
||||
|
||||
func main() {
|
||||
initTracer()
|
||||
tr := trace.GlobalProvider().GetTracer("example/server")
|
||||
tr := global.TraceProvider().GetTracer("example/server")
|
||||
|
||||
helloHandler := func(w http.ResponseWriter, req *http.Request) {
|
||||
attrs, entries, spanCtx := httptrace.Extract(req.Context(), req)
|
||||
|
@ -19,6 +19,7 @@ import (
|
||||
|
||||
"go.opentelemetry.io/api/key"
|
||||
"go.opentelemetry.io/api/trace"
|
||||
"go.opentelemetry.io/global"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -31,7 +32,7 @@ func SubOperation(ctx context.Context) error {
|
||||
|
||||
// Using global provider. Alternative is to have application provide a getter
|
||||
// for its component to get the instance of the provider.
|
||||
tr := trace.GlobalProvider().GetTracer("example/namedtracer/foo")
|
||||
tr := global.TraceProvider().GetTracer("example/namedtracer/foo")
|
||||
return tr.WithSpan(
|
||||
ctx,
|
||||
"Sub operation...",
|
||||
|
@ -23,6 +23,7 @@ import (
|
||||
"go.opentelemetry.io/api/trace"
|
||||
"go.opentelemetry.io/example/namedtracer/foo"
|
||||
"go.opentelemetry.io/exporter/trace/stdout"
|
||||
"go.opentelemetry.io/global"
|
||||
sdktrace "go.opentelemetry.io/sdk/trace"
|
||||
)
|
||||
|
||||
@ -47,7 +48,7 @@ func initTracer() {
|
||||
if err != nil {
|
||||
log.Panicf("failed to initialize trace provider %v\n", err)
|
||||
}
|
||||
trace.SetGlobalProvider(tp)
|
||||
global.SetTraceProvider(tp)
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
@ -25,6 +25,7 @@ import (
|
||||
|
||||
otelcore "go.opentelemetry.io/api/core"
|
||||
oteltrace "go.opentelemetry.io/api/trace"
|
||||
"go.opentelemetry.io/global"
|
||||
|
||||
"go.opentelemetry.io/experimental/bridge/opentracing/internal"
|
||||
)
|
||||
@ -118,7 +119,7 @@ func TestMixedAPIs(t *testing.T) {
|
||||
})
|
||||
ctx := context.Background()
|
||||
|
||||
oteltrace.SetGlobalProvider(otelProvider)
|
||||
global.SetTraceProvider(otelProvider)
|
||||
ot.SetGlobalTracer(otTracer)
|
||||
|
||||
tc.setup(t, mockOtelTracer)
|
||||
@ -424,7 +425,7 @@ func (tm *tracerMessTest) setup(t *testing.T, tracer *internal.MockTracer) {
|
||||
|
||||
func (tm *tracerMessTest) check(t *testing.T, tracer *internal.MockTracer) {
|
||||
globalOtTracer := ot.GlobalTracer()
|
||||
globalOtelTracer := oteltrace.GlobalProvider().GetTracer("")
|
||||
globalOtelTracer := global.TraceProvider().GetTracer("")
|
||||
if len(tm.recordedOTSpanTracers) != 3 {
|
||||
t.Errorf("Expected 3 recorded OpenTracing tracers from spans, got %d", len(tm.recordedOTSpanTracers))
|
||||
}
|
||||
@ -543,7 +544,7 @@ func min(a, b int) int {
|
||||
}
|
||||
|
||||
func runOtelOTOtel(t *testing.T, ctx context.Context, name string, callback func(*testing.T, context.Context)) {
|
||||
tr := oteltrace.GlobalProvider().GetTracer("")
|
||||
tr := global.TraceProvider().GetTracer("")
|
||||
ctx, span := tr.Start(ctx, fmt.Sprintf("%s_Otel_OTOtel", name), oteltrace.WithSpanKind(oteltrace.SpanKindClient))
|
||||
defer span.End()
|
||||
callback(t, ctx)
|
||||
@ -560,7 +561,7 @@ func runOtelOTOtel(t *testing.T, ctx context.Context, name string, callback func
|
||||
}
|
||||
|
||||
func runOTOtelOT(t *testing.T, ctx context.Context, name string, callback func(*testing.T, context.Context)) {
|
||||
tr := oteltrace.GlobalProvider().GetTracer("")
|
||||
tr := global.TraceProvider().GetTracer("")
|
||||
span, ctx := ot.StartSpanFromContext(ctx, fmt.Sprintf("%s_OT_OtelOT", name), ot.Tag{Key: "span.kind", Value: "client"})
|
||||
defer span.Finish()
|
||||
callback(t, ctx)
|
||||
|
@ -23,8 +23,8 @@ import (
|
||||
"go.opentelemetry.io/api/core"
|
||||
"go.opentelemetry.io/api/key"
|
||||
|
||||
apitrace "go.opentelemetry.io/api/trace"
|
||||
"go.opentelemetry.io/exporter/trace/jaeger"
|
||||
"go.opentelemetry.io/global"
|
||||
sdktrace "go.opentelemetry.io/sdk/trace"
|
||||
)
|
||||
|
||||
@ -45,7 +45,6 @@ func initTracer() func() {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
exporter.RegisterSimpleSpanProcessor()
|
||||
|
||||
// For demoing purposes, always sample. In a production application, you should
|
||||
// configure this to a trace.ProbabilitySampler set at the desired
|
||||
@ -56,7 +55,7 @@ func initTracer() func() {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
apitrace.SetGlobalProvider(tp)
|
||||
global.SetTraceProvider(tp)
|
||||
return func() {
|
||||
exporter.Flush()
|
||||
}
|
||||
@ -68,14 +67,14 @@ func main() {
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
tr := apitrace.GlobalProvider().GetTracer("component-main")
|
||||
tr := global.TraceProvider().GetTracer("component-main")
|
||||
ctx, span := tr.Start(ctx, "/foo")
|
||||
bar(ctx)
|
||||
span.End()
|
||||
}
|
||||
|
||||
func bar(ctx context.Context) {
|
||||
tr := apitrace.GlobalProvider().GetTracer("component-bar")
|
||||
tr := global.TraceProvider().GetTracer("component-bar")
|
||||
_, span := tr.Start(ctx, "/bar")
|
||||
defer span.End()
|
||||
|
||||
|
@ -12,28 +12,32 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package trace
|
||||
package global
|
||||
|
||||
import "sync/atomic"
|
||||
import (
|
||||
"sync/atomic"
|
||||
|
||||
"go.opentelemetry.io/api/trace"
|
||||
)
|
||||
|
||||
type globalProvider struct {
|
||||
p Provider
|
||||
p trace.Provider
|
||||
}
|
||||
|
||||
var globalP atomic.Value
|
||||
|
||||
// GlobalProvider returns trace provider registered with global registry.
|
||||
// If no trace provider is registered then an instance of NoopTraceProvider is returned.
|
||||
// TraceProvider returns the registered global trace provider.
|
||||
// If none is registered then an instance of NoopTraceProvider is returned.
|
||||
// Use the trace provider to create a named tracer. E.g.
|
||||
// tracer := trace.GlobalProvider().GetTracer("example.com/foo")
|
||||
func GlobalProvider() Provider {
|
||||
// tracer := global.TraceProvider().GetTracer("example.com/foo")
|
||||
func TraceProvider() trace.Provider {
|
||||
if gp := globalP.Load(); gp != nil {
|
||||
return gp.(globalProvider).p
|
||||
}
|
||||
return NoopTraceProvider{}
|
||||
return trace.NoopTraceProvider{}
|
||||
}
|
||||
|
||||
// SetGlobalProvider sets the provider as a global trace provider.
|
||||
func SetGlobalProvider(m Provider) {
|
||||
globalP.Store(globalProvider{p: m})
|
||||
// SetTraceProvider registers p as the global trace provider.
|
||||
func SetTraceProvider(p trace.Provider) {
|
||||
globalP.Store(globalProvider{p: p})
|
||||
}
|
@ -12,12 +12,13 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package trace_test
|
||||
package global_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"go.opentelemetry.io/api/trace"
|
||||
"go.opentelemetry.io/global"
|
||||
)
|
||||
|
||||
type TestProvider1 struct {
|
||||
@ -33,10 +34,10 @@ func TestMulitpleGlobalProvider(t *testing.T) {
|
||||
|
||||
p1 := TestProvider1{}
|
||||
p2 := trace.NoopTraceProvider{}
|
||||
trace.SetGlobalProvider(&p1)
|
||||
trace.SetGlobalProvider(&p2)
|
||||
global.SetTraceProvider(&p1)
|
||||
global.SetTraceProvider(&p2)
|
||||
|
||||
got := trace.GlobalProvider()
|
||||
got := global.TraceProvider()
|
||||
want := &p2
|
||||
if got != want {
|
||||
t.Fatalf("Provider: got %p, want %p\n", got, want)
|
@ -28,6 +28,7 @@ import (
|
||||
"go.opentelemetry.io/api/core"
|
||||
"go.opentelemetry.io/api/key"
|
||||
"go.opentelemetry.io/api/trace"
|
||||
"go.opentelemetry.io/global"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -54,7 +55,7 @@ func newClientTracer(ctx context.Context) *clientTracer {
|
||||
Context: ctx,
|
||||
levels: make(map[string]trace.Span),
|
||||
}
|
||||
ct.tr = trace.GlobalProvider().GetTracer("go.opentelemetry.io/plugin/httptrace")
|
||||
ct.tr = global.TraceProvider().GetTracer("go.opentelemetry.io/plugin/httptrace")
|
||||
ct.open("http.request")
|
||||
return ct
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ import (
|
||||
"go.opentelemetry.io/api/core"
|
||||
"go.opentelemetry.io/api/propagation"
|
||||
"go.opentelemetry.io/api/trace"
|
||||
"go.opentelemetry.io/global"
|
||||
prop "go.opentelemetry.io/propagation"
|
||||
)
|
||||
|
||||
@ -129,7 +130,7 @@ func WithMessageEvents(events ...event) Option {
|
||||
func NewHandler(handler http.Handler, operation string, opts ...Option) http.Handler {
|
||||
h := Handler{handler: handler, operation: operation}
|
||||
defaultOpts := []Option{
|
||||
WithTracer(trace.GlobalProvider().GetTracer("go.opentelemtry.io/plugin/othttp")),
|
||||
WithTracer(global.TraceProvider().GetTracer("go.opentelemtry.io/plugin/othttp")),
|
||||
WithPropagator(prop.HTTPTraceContextPropagator{}),
|
||||
WithSpanOptions(trace.WithSpanKind(trace.SpanKindServer)),
|
||||
}
|
||||
|
@ -25,6 +25,7 @@ import (
|
||||
"go.opentelemetry.io/api/core"
|
||||
"go.opentelemetry.io/api/trace"
|
||||
"go.opentelemetry.io/exporter/trace/stdout"
|
||||
"go.opentelemetry.io/global"
|
||||
"go.opentelemetry.io/plugin/othttp"
|
||||
sdktrace "go.opentelemetry.io/sdk/trace"
|
||||
)
|
||||
@ -55,7 +56,7 @@ func ExampleNewHandler() {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
trace.SetGlobalProvider(tp)
|
||||
global.SetTraceProvider(tp)
|
||||
|
||||
figureOutName := func(ctx context.Context, s string) (string, error) {
|
||||
pp := strings.SplitN(s, "/", 2)
|
||||
|
Loading…
x
Reference in New Issue
Block a user