1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-03-29 21:47:00 +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
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 32 additions and 13 deletions

View File

@ -15,6 +15,13 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Fixed ### Fixed
- Correct comments for the priority of the `WithEndpoint` and `WithEndpointURL` options and their corresponding environment variables in `go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp`. (#5584) - Correct comments for the priority of the `WithEndpoint` and `WithEndpointURL` options and their corresponding environment variables in `go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp`. (#5584)
- Correct the `Tracer`, `Meter`, and `Logger` names used in `go.opentelemetry.io/otel/example/dice`. (#5612)
- Correct the `Tracer` names used in `go.opentelemetry.io/otel/example/namedtracer`. (#5612)
- Correct the `Tracer` name used in `go.opentelemetry.io/otel/example/opencensus`. (#5612)
- Correct the `Tracer` and `Meter` names used in `go.opentelemetry.io/otel/example/otel-collector`. (#5612)
- Correct the `Tracer` names used in `go.opentelemetry.io/otel/example/passthrough`. (#5612)
- Correct the `Meter` name used in `go.opentelemetry.io/otel/example/prometheus`. (#5612)
- Correct the `Tracer` names used in `go.opentelemetry.io/otel/example/zipkin`. (#5612)
<!-- Released section --> <!-- Released section -->
<!-- Don't change this section unless doing release --> <!-- Don't change this section unless doing release -->

View File

@ -9,7 +9,7 @@ import (
"go.opentelemetry.io/otel/metric" "go.opentelemetry.io/otel/metric"
) )
const name = "rolldice" const name = "go.opentelemetry.io/otel/example/dice"
var ( var (
tracer = otel.Tracer(name) tracer = otel.Tracer(name)

View File

@ -18,7 +18,7 @@ var lemonsKey = attribute.Key("ex.com/lemons")
func SubOperation(ctx context.Context) error { func SubOperation(ctx context.Context) error {
// Using global provider. Alternative is to have application provide a getter // Using global provider. Alternative is to have application provide a getter
// for its component to get the instance of the provider. // 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 var span trace.Span
_, span = tr.Start(ctx, "Sub operation...") _, span = tr.Start(ctx, "Sub operation...")

View File

@ -52,7 +52,7 @@ func main() {
} }
// 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("go.opentelemetry.io/otel/example/namedtracer")
ctx := context.Background() ctx := context.Background()
defer func() { _ = tp.Shutdown(ctx) }() defer func() { _ = tp.Shutdown(ctx) }()

View File

@ -76,7 +76,8 @@ func tracing(otExporter sdktrace.SpanExporter) {
tp.ForceFlush(ctx) 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.") 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() otspan.End()
tp.ForceFlush(ctx) tp.ForceFlush(ctx)

View File

@ -130,8 +130,9 @@ func main() {
} }
}() }()
tracer := otel.Tracer("test-tracer") name := "go.opentelemetry.io/otel/example/otel-collector"
meter := otel.Meter("test-meter") tracer := otel.Tracer(name)
meter := otel.Meter(name)
// Attributes represent additional key-value descriptors that can be bound // Attributes represent additional key-value descriptors that can be bound
// to a metric observer or recorder. // to a metric observer or recorder.

View File

@ -30,7 +30,7 @@ func New(next func(r *http.Request)) *Handler {
// global progatators and tracer providers. // global progatators and tracer providers.
return &Handler{ return &Handler{
propagators: otel.GetTextMapPropagator(), propagators: otel.GetTextMapPropagator(),
tracer: otel.Tracer("examples/passthrough/handler"), tracer: otel.Tracer("go.opentelemetry.io/otel/example/passthrough/handler"),
next: next, next: next,
} }
} }

View File

@ -18,6 +18,8 @@ import (
"go.opentelemetry.io/otel/trace" "go.opentelemetry.io/otel/trace"
) )
const name = "go.opentelemetry.io/otel/example/passthrough"
func main() { func main() {
ctx := context.Background() ctx := context.Background()
@ -37,16 +39,22 @@ func main() {
// This is roughly what an instrumented http client does. // 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") 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 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() defer span.End()
r = r.WithContext(ctx) r = r.WithContext(ctx)
otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(r.Header)) otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(r.Header))
backendFunc := func(r *http.Request) { backendFunc := func(r *http.Request) {
// This is roughly what an instrumented http server does. // 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") 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() defer span.End()
// Do "backend work" // Do "backend work"

View File

@ -21,7 +21,7 @@ import (
"go.opentelemetry.io/otel/sdk/metric" "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() { func main() {
rng := rand.New(rand.NewSource(time.Now().UnixNano())) rng := rand.New(rand.NewSource(time.Now().UnixNano()))

View File

@ -21,6 +21,8 @@ import (
"go.opentelemetry.io/otel/trace" "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) 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. // 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)) ctx, span := tr.Start(ctx, "foo", trace.WithSpanKind(trace.SpanKindServer))
<-time.After(6 * time.Millisecond) <-time.After(6 * time.Millisecond)
bar(ctx) bar(ctx)
@ -78,7 +80,7 @@ func main() {
} }
func bar(ctx context.Context) { func bar(ctx context.Context) {
tr := otel.GetTracerProvider().Tracer("component-bar") tr := trace.SpanFromContext(ctx).TracerProvider().Tracer(name)
_, span := tr.Start(ctx, "bar") _, span := tr.Start(ctx, "bar")
<-time.After(6 * time.Millisecond) <-time.After(6 * time.Millisecond)
span.End() span.End()