|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "log" |
| 20 | + "net/http" |
| 21 | + "time" |
| 22 | + |
| 23 | + "go.opentelemetry.io/otel" |
| 24 | + "go.opentelemetry.io/otel/example/passthrough/handler" |
| 25 | + "go.opentelemetry.io/otel/exporters/stdout" |
| 26 | + "go.opentelemetry.io/otel/propagation" |
| 27 | + sdktrace "go.opentelemetry.io/otel/sdk/trace" |
| 28 | + "go.opentelemetry.io/otel/trace" |
| 29 | +) |
| 30 | + |
| 31 | +func main() { |
| 32 | + ctx := context.Background() |
| 33 | + |
| 34 | + initPassthroughGlobals() |
| 35 | + tp := nonGlobalTracer() |
| 36 | + defer func() { _ = tp.Shutdown(ctx) }() |
| 37 | + |
| 38 | + // make an initial http request |
| 39 | + r, err := http.NewRequest("", "", nil) |
| 40 | + if err != nil { |
| 41 | + panic(err) |
| 42 | + } |
| 43 | + |
| 44 | + // This is roughly what an instrumented http client does. |
| 45 | + log.Println("The \"make outer request\" span should be recorded, because it is recorded with a Tracer from the SDK TracerProvider") |
| 46 | + var span trace.Span |
| 47 | + ctx, span = tp.Tracer("example/passthrough/outer").Start(ctx, "make outer request") |
| 48 | + defer span.End() |
| 49 | + r = r.WithContext(ctx) |
| 50 | + otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(r.Header)) |
| 51 | + |
| 52 | + backendFunc := func(r *http.Request) { |
| 53 | + // This is roughly what an instrumented http server does. |
| 54 | + ctx := otel.GetTextMapPropagator().Extract(r.Context(), propagation.HeaderCarrier(r.Header)) |
| 55 | + log.Println("The \"handle inner request\" span should be recorded, because it is recorded with a Tracer from the SDK TracerProvider") |
| 56 | + _, span := tp.Tracer("example/passthrough/inner").Start(ctx, "handle inner request") |
| 57 | + defer span.End() |
| 58 | + |
| 59 | + // Do "backend work" |
| 60 | + time.Sleep(time.Second) |
| 61 | + } |
| 62 | + // This handler will be a passthrough, since we didn't set a global TracerProvider |
| 63 | + passthroughHandler := handler.New(backendFunc) |
| 64 | + passthroughHandler.HandleHTTPReq(r) |
| 65 | +} |
| 66 | + |
| 67 | +func initPassthroughGlobals() { |
| 68 | + // We explicitly DO NOT set the global TracerProvider using otel.SetTracerProvider(). |
| 69 | + // The unset TracerProvider returns a "non-recording" span, but still passes through context. |
| 70 | + log.Println("Register a global TextMapPropagator, but do not register a global TracerProvider to be in \"passthrough\" mode.") |
| 71 | + log.Println("The \"passthrough\" mode propagates the TraceContext and Baggage, but does not record spans.") |
| 72 | + otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})) |
| 73 | +} |
| 74 | + |
| 75 | +// nonGlobalTracer creates a trace provider instance for testing, but doesn't |
| 76 | +// set it as the global tracer provider |
| 77 | +func nonGlobalTracer() *sdktrace.TracerProvider { |
| 78 | + var err error |
| 79 | + exp, err := stdout.NewExporter(stdout.WithPrettyPrint()) |
| 80 | + if err != nil { |
| 81 | + log.Panicf("failed to initialize stdout exporter %v\n", err) |
| 82 | + } |
| 83 | + bsp := sdktrace.NewBatchSpanProcessor(exp) |
| 84 | + tp := sdktrace.NewTracerProvider( |
| 85 | + sdktrace.WithSampler(sdktrace.AlwaysSample()), |
| 86 | + sdktrace.WithSpanProcessor(bsp), |
| 87 | + ) |
| 88 | + return tp |
| 89 | +} |
0 commit comments