1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2026-06-03 18:35:08 +02:00

Update example instrumentation names (#5612)

Part of #5412 

- Use the recommended package name for the instrumentation exemplified
in the repository.
- Use the recommended detection of a `TracerProvider` from passed
context.
This commit is contained in:
Tyler Yahn
2024-07-12 11:53:18 -07:00
committed by GitHub
parent 9535f08920
commit f5b4e99025
10 changed files with 32 additions and 13 deletions
+1 -1
View File
@@ -9,7 +9,7 @@ import (
"go.opentelemetry.io/otel/metric"
)
const name = "rolldice"
const name = "go.opentelemetry.io/otel/example/dice"
var (
tracer = otel.Tracer(name)
+1 -1
View File
@@ -18,7 +18,7 @@ var lemonsKey = attribute.Key("ex.com/lemons")
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 := otel.Tracer("example/namedtracer/foo")
tr := otel.Tracer("go.opentelemetry.io/otel/example/namedtracer/foo")
var span trace.Span
_, span = tr.Start(ctx, "Sub operation...")
+1 -1
View File
@@ -52,7 +52,7 @@ func main() {
}
// Create a named tracer with package path as its name.
tracer := tp.Tracer("example/namedtracer/main")
tracer := tp.Tracer("go.opentelemetry.io/otel/example/namedtracer")
ctx := context.Background()
defer func() { _ = tp.Shutdown(ctx) }()
+2 -1
View File
@@ -76,7 +76,8 @@ func tracing(otExporter sdktrace.SpanExporter) {
tp.ForceFlush(ctx)
log.Println("Creating OpenTelemetry span\n-- It should have the OpenCensus span as a parent, since the OpenCensus span was written with using OpenTelemetry APIs.")
ctx, otspan := tp.Tracer("simple").Start(ctx, "OpenTelemetrySpan")
tracer := tp.Tracer("go.opentelemetry.io/otel/example/opencensus")
ctx, otspan := tracer.Start(ctx, "OpenTelemetrySpan")
otspan.End()
tp.ForceFlush(ctx)
+3 -2
View File
@@ -130,8 +130,9 @@ func main() {
}
}()
tracer := otel.Tracer("test-tracer")
meter := otel.Meter("test-meter")
name := "go.opentelemetry.io/otel/example/otel-collector"
tracer := otel.Tracer(name)
meter := otel.Meter(name)
// Attributes represent additional key-value descriptors that can be bound
// to a metric observer or recorder.
+1 -1
View File
@@ -30,7 +30,7 @@ func New(next func(r *http.Request)) *Handler {
// global progatators and tracer providers.
return &Handler{
propagators: otel.GetTextMapPropagator(),
tracer: otel.Tracer("examples/passthrough/handler"),
tracer: otel.Tracer("go.opentelemetry.io/otel/example/passthrough/handler"),
next: next,
}
}
+11 -3
View File
@@ -18,6 +18,8 @@ import (
"go.opentelemetry.io/otel/trace"
)
const name = "go.opentelemetry.io/otel/example/passthrough"
func main() {
ctx := context.Background()
@@ -37,16 +39,22 @@ func main() {
// This is roughly what an instrumented http client does.
log.Println("The \"make outer request\" span should be recorded, because it is recorded with a Tracer from the SDK TracerProvider")
var span trace.Span
ctx, span = tp.Tracer("example/passthrough/outer").Start(ctx, "make outer request")
tracer := tp.Tracer(name)
ctx, span = tracer.Start(ctx, "make outer request")
defer span.End()
r = r.WithContext(ctx)
otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(r.Header))
backendFunc := func(r *http.Request) {
// This is roughly what an instrumented http server does.
ctx := otel.GetTextMapPropagator().Extract(r.Context(), propagation.HeaderCarrier(r.Header))
ctx := r.Context()
tp := trace.SpanFromContext(ctx).TracerProvider()
tracer := tp.Tracer(name)
ctx = otel.GetTextMapPropagator().Extract(ctx, propagation.HeaderCarrier(r.Header))
log.Println("The \"handle inner request\" span should be recorded, because it is recorded with a Tracer from the SDK TracerProvider")
_, span := tp.Tracer("example/passthrough/inner").Start(ctx, "handle inner request")
_, span := tracer.Start(ctx, "handle inner request")
defer span.End()
// Do "backend work"
+1 -1
View File
@@ -21,7 +21,7 @@ import (
"go.opentelemetry.io/otel/sdk/metric"
)
const meterName = "github.com/open-telemetry/opentelemetry-go/example/prometheus"
const meterName = "go.opentelemetry.io/otel/example/prometheus"
func main() {
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
+4 -2
View File
@@ -21,6 +21,8 @@ import (
"go.opentelemetry.io/otel/trace"
)
const name = "go.opentelemetry.io/otel/example/zipkin"
var logger = log.New(os.Stderr, "zipkin-example", log.Ldate|log.Ltime|log.Llongfile)
// initTracer creates a new trace provider instance and registers it as global trace provider.
@@ -69,7 +71,7 @@ func main() {
}
}()
tr := otel.GetTracerProvider().Tracer("component-main")
tr := otel.GetTracerProvider().Tracer(name)
ctx, span := tr.Start(ctx, "foo", trace.WithSpanKind(trace.SpanKindServer))
<-time.After(6 * time.Millisecond)
bar(ctx)
@@ -78,7 +80,7 @@ func main() {
}
func bar(ctx context.Context) {
tr := otel.GetTracerProvider().Tracer("component-bar")
tr := trace.SpanFromContext(ctx).TracerProvider().Tracer(name)
_, span := tr.Start(ctx, "bar")
<-time.After(6 * time.Millisecond)
span.End()