|
| 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 otlptracehttp_test |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "log" |
| 20 | + |
| 21 | + "go.opentelemetry.io/otel" |
| 22 | + "go.opentelemetry.io/otel/exporters/otlp/otlptrace" |
| 23 | + "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" |
| 24 | + "go.opentelemetry.io/otel/sdk/resource" |
| 25 | + sdktrace "go.opentelemetry.io/otel/sdk/trace" |
| 26 | + semconv "go.opentelemetry.io/otel/semconv/v1.4.0" |
| 27 | + "go.opentelemetry.io/otel/trace" |
| 28 | +) |
| 29 | + |
| 30 | +const ( |
| 31 | + instrumentationName = "github.com/instrumentron" |
| 32 | + instrumentationVersion = "v0.1.0" |
| 33 | +) |
| 34 | + |
| 35 | +var ( |
| 36 | + tracer = otel.GetTracerProvider().Tracer( |
| 37 | + instrumentationName, |
| 38 | + trace.WithInstrumentationVersion(instrumentationVersion), |
| 39 | + trace.WithSchemaURL(semconv.SchemaURL), |
| 40 | + ) |
| 41 | +) |
| 42 | + |
| 43 | +func add(ctx context.Context, x, y int64) int64 { |
| 44 | + var span trace.Span |
| 45 | + _, span = tracer.Start(ctx, "Addition") |
| 46 | + defer span.End() |
| 47 | + |
| 48 | + return x + y |
| 49 | +} |
| 50 | + |
| 51 | +func multiply(ctx context.Context, x, y int64) int64 { |
| 52 | + var span trace.Span |
| 53 | + _, span = tracer.Start(ctx, "Multiplication") |
| 54 | + defer span.End() |
| 55 | + |
| 56 | + return x * y |
| 57 | +} |
| 58 | + |
| 59 | +func newResource() *resource.Resource { |
| 60 | + return resource.NewWithAttributes( |
| 61 | + semconv.SchemaURL, |
| 62 | + semconv.ServiceNameKey.String("otlptrace-example"), |
| 63 | + semconv.ServiceVersionKey.String("0.0.1"), |
| 64 | + ) |
| 65 | +} |
| 66 | + |
| 67 | +func installExportPipeline(ctx context.Context) func() { |
| 68 | + client := otlptracehttp.NewClient() |
| 69 | + exporter, err := otlptrace.New(ctx, client) |
| 70 | + if err != nil { |
| 71 | + log.Fatalf("creating OTLP trace exporter: %v", err) |
| 72 | + } |
| 73 | + |
| 74 | + tracerProvider := sdktrace.NewTracerProvider( |
| 75 | + sdktrace.WithBatcher(exporter), |
| 76 | + sdktrace.WithResource(newResource()), |
| 77 | + ) |
| 78 | + otel.SetTracerProvider(tracerProvider) |
| 79 | + |
| 80 | + return func() { |
| 81 | + if err := tracerProvider.Shutdown(ctx); err != nil { |
| 82 | + log.Fatalf("stopping tracer provider: %v", err) |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func Example() { |
| 88 | + ctx := context.Background() |
| 89 | + // Registers a tracer Provider globally. |
| 90 | + cleanup := installExportPipeline(ctx) |
| 91 | + defer cleanup() |
| 92 | + |
| 93 | + log.Println("the answer is", add(ctx, multiply(ctx, multiply(ctx, 2, 2), 10), 2)) |
| 94 | +} |
0 commit comments