Skip to content

Commit

Permalink
Cleanup otlptrace testable examples
Browse files Browse the repository at this point in the history
  • Loading branch information
pellared committed Nov 6, 2023
1 parent 213a435 commit d8e952e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 62 deletions.
42 changes: 42 additions & 0 deletions exporters/otlp/otlptrace/otlptracegrpc/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package otlptracegrpc_test

import (
"context"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/sdk/trace"
)

func Example() {
ctx := context.Background()
exp, err := otlptracegrpc.New(ctx)
if err != nil {
panic(err)
}

tracerProvider := trace.NewTracerProvider(trace.WithBatcher(exp))
defer func() {
if err := tracerProvider.Shutdown(ctx); err != nil {
panic(err)
}
}()
otel.SetTracerProvider(tracerProvider)

// From here, the tracerProvider can be used by instrumentation to collect
// telemetry.
}
72 changes: 10 additions & 62 deletions exporters/otlp/otlptrace/otlptracehttp/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,79 +16,27 @@ package otlptracehttp_test

import (
"context"
"fmt"
"log"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
"go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/otel/sdk/trace"
)

const (
instrumentationName = "github.com/instrumentron"
instrumentationVersion = "0.1.0"
)

var tracer = otel.GetTracerProvider().Tracer(
instrumentationName,
trace.WithInstrumentationVersion(instrumentationVersion),
trace.WithSchemaURL(semconv.SchemaURL),
)

func add(ctx context.Context, x, y int64) int64 {
var span trace.Span
_, span = tracer.Start(ctx, "Addition")
defer span.End()

return x + y
}

func multiply(ctx context.Context, x, y int64) int64 {
var span trace.Span
_, span = tracer.Start(ctx, "Multiplication")
defer span.End()

return x * y
}

func newResource() *resource.Resource {
return resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceName("otlptrace-example"),
semconv.ServiceVersion("0.0.1"),
)
}

func installExportPipeline(ctx context.Context) (func(context.Context) error, error) {
exporter, err := otlptracehttp.New(ctx)
if err != nil {
return nil, fmt.Errorf("creating OTLP trace exporter: %w", err)
}

tracerProvider := sdktrace.NewTracerProvider(
sdktrace.WithBatcher(exporter),
sdktrace.WithResource(newResource()),
)
otel.SetTracerProvider(tracerProvider)

return tracerProvider.Shutdown, nil
}

func Example() {
ctx := context.Background()
// Registers a tracer Provider globally.
shutdown, err := installExportPipeline(ctx)
exp, err := otlptracehttp.New(ctx)
if err != nil {
log.Fatal(err)
panic(err)
}

tracerProvider := trace.NewTracerProvider(trace.WithBatcher(exp))
defer func() {
if err := shutdown(ctx); err != nil {
log.Fatal(err)
if err := tracerProvider.Shutdown(ctx); err != nil {
panic(err)
}
}()
otel.SetTracerProvider(tracerProvider)

log.Println("the answer is", add(ctx, multiply(ctx, multiply(ctx, 2, 2), 10), 2))
// From here, the tracerProvider can be used by instrumentation to collect
// telemetry.
}

0 comments on commit d8e952e

Please sign in to comment.