Skip to content

Commit

Permalink
Update example instrumentation names (#5612)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MrAlias authored Jul 12, 2024
1 parent 9535f08 commit f5b4e99
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 13 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### 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 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 -->
<!-- Don't change this section unless doing release -->
Expand Down
2 changes: 1 addition & 1 deletion example/dice/rolldice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion example/namedtracer/foo/foo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...")
Expand Down
2 changes: 1 addition & 1 deletion example/namedtracer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) }()

Expand Down
3 changes: 2 additions & 1 deletion example/opencensus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 3 additions & 2 deletions example/otel-collector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion example/passthrough/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down
14 changes: 11 additions & 3 deletions example/passthrough/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"go.opentelemetry.io/otel/trace"
)

const name = "go.opentelemetry.io/otel/example/passthrough"

func main() {
ctx := context.Background()

Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion example/prometheus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down
6 changes: 4 additions & 2 deletions example/zipkin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down

0 comments on commit f5b4e99

Please sign in to comment.