2020-05-11 12:10:12 +02:00
|
|
|
// 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.
|
|
|
|
|
2021-06-11 17:18:17 +02:00
|
|
|
// Example using OTLP exporters + collector + third-party backends. For
|
2020-06-23 17:37:07 +02:00
|
|
|
// information about using the exporter, see:
|
|
|
|
// https://pkg.go.dev/go.opentelemetry.io/otel/exporters/otlp?tab=doc#example-package-Insecure
|
2020-05-08 14:25:20 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2020-05-11 12:42:36 +02:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2020-11-16 19:30:54 +02:00
|
|
|
"go.opentelemetry.io/otel"
|
2021-02-18 19:59:37 +02:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2021-06-11 17:18:17 +02:00
|
|
|
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
|
2020-11-13 17:34:24 +02:00
|
|
|
"go.opentelemetry.io/otel/propagation"
|
2020-05-20 22:08:43 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/resource"
|
2020-05-08 14:25:20 +02:00
|
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
2021-10-22 17:43:47 +02:00
|
|
|
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
|
2020-11-07 00:13:31 +02:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2020-05-08 14:25:20 +02:00
|
|
|
)
|
|
|
|
|
2020-06-23 17:37:07 +02:00
|
|
|
// Initializes an OTLP exporter, and configures the corresponding trace and
|
|
|
|
// metric providers.
|
2020-09-20 19:40:47 +02:00
|
|
|
func initProvider() func() {
|
2020-10-31 20:16:55 +02:00
|
|
|
ctx := context.Background()
|
2020-06-23 17:37:07 +02:00
|
|
|
|
2020-10-31 20:16:55 +02:00
|
|
|
res, err := resource.New(ctx,
|
|
|
|
resource.WithAttributes(
|
|
|
|
// the service name used to display traces in backends
|
|
|
|
semconv.ServiceNameKey.String("test-service"),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
handleErr(err, "failed to create resource")
|
|
|
|
|
2021-06-11 17:18:17 +02:00
|
|
|
// If the OpenTelemetry Collector is running on a local cluster (minikube or
|
|
|
|
// microk8s), it should be accessible through the NodePort service at the
|
|
|
|
// `localhost:30080` endpoint. Otherwise, replace `localhost` with the
|
|
|
|
// endpoint of your cluster. If you run the app inside k8s, then you can
|
|
|
|
// probably connect directly to the service through dns
|
|
|
|
|
|
|
|
// Set up a trace exporter
|
|
|
|
traceExporter, err := otlptracegrpc.New(ctx,
|
|
|
|
otlptracegrpc.WithInsecure(),
|
|
|
|
otlptracegrpc.WithEndpoint("localhost:30080"),
|
|
|
|
otlptracegrpc.WithDialOption(grpc.WithBlock()),
|
|
|
|
)
|
|
|
|
handleErr(err, "failed to create trace exporter")
|
|
|
|
|
|
|
|
// Register the trace exporter with a TracerProvider, using a batch
|
|
|
|
// span processor to aggregate spans before export.
|
|
|
|
bsp := sdktrace.NewBatchSpanProcessor(traceExporter)
|
2020-09-24 00:16:13 +02:00
|
|
|
tracerProvider := sdktrace.NewTracerProvider(
|
2021-03-18 18:34:47 +02:00
|
|
|
sdktrace.WithSampler(sdktrace.AlwaysSample()),
|
2020-10-31 20:16:55 +02:00
|
|
|
sdktrace.WithResource(res),
|
2020-09-20 19:40:47 +02:00
|
|
|
sdktrace.WithSpanProcessor(bsp),
|
2020-06-23 17:37:07 +02:00
|
|
|
)
|
2021-06-11 17:18:17 +02:00
|
|
|
otel.SetTracerProvider(tracerProvider)
|
2020-06-23 17:37:07 +02:00
|
|
|
|
2020-10-02 21:27:16 +02:00
|
|
|
// set global propagator to tracecontext (the default is no-op).
|
2020-11-16 19:30:54 +02:00
|
|
|
otel.SetTextMapPropagator(propagation.TraceContext{})
|
2020-06-23 17:37:07 +02:00
|
|
|
|
2020-09-20 19:40:47 +02:00
|
|
|
return func() {
|
2021-03-10 18:19:59 +02:00
|
|
|
// Shutdown will flush any remaining spans and shut down the exporter.
|
|
|
|
handleErr(tracerProvider.Shutdown(ctx), "failed to shutdown TracerProvider")
|
2020-09-20 19:40:47 +02:00
|
|
|
}
|
2020-06-23 17:37:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
log.Printf("Waiting for connection...")
|
|
|
|
|
2020-09-20 19:40:47 +02:00
|
|
|
shutdown := initProvider()
|
|
|
|
defer shutdown()
|
2020-06-23 17:37:07 +02:00
|
|
|
|
2020-11-16 19:30:54 +02:00
|
|
|
tracer := otel.Tracer("test-tracer")
|
2020-06-23 17:37:07 +02:00
|
|
|
|
|
|
|
// labels represent additional key-value descriptors that can be bound to a
|
|
|
|
// metric observer or recorder.
|
2021-02-18 19:59:37 +02:00
|
|
|
commonLabels := []attribute.KeyValue{
|
|
|
|
attribute.String("labelA", "chocolate"),
|
|
|
|
attribute.String("labelB", "raspberry"),
|
|
|
|
attribute.String("labelC", "vanilla"),
|
2020-05-08 14:25:20 +02:00
|
|
|
}
|
2020-05-13 12:38:23 +02:00
|
|
|
|
2020-06-23 17:37:07 +02:00
|
|
|
// work begins
|
2020-07-31 20:34:46 +02:00
|
|
|
ctx, span := tracer.Start(
|
|
|
|
context.Background(),
|
|
|
|
"CollectorExporter-Example",
|
2020-11-07 00:13:31 +02:00
|
|
|
trace.WithAttributes(commonLabels...))
|
2020-09-20 19:40:47 +02:00
|
|
|
defer span.End()
|
2020-05-08 14:25:20 +02:00
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
_, iSpan := tracer.Start(ctx, fmt.Sprintf("Sample-%d", i))
|
2020-06-23 17:37:07 +02:00
|
|
|
log.Printf("Doing really hard work (%d / 10)\n", i+1)
|
|
|
|
|
2020-05-11 12:10:12 +02:00
|
|
|
<-time.After(time.Second)
|
2020-05-08 14:25:20 +02:00
|
|
|
iSpan.End()
|
|
|
|
}
|
2020-05-20 10:38:34 +02:00
|
|
|
|
2020-06-23 17:37:07 +02:00
|
|
|
log.Printf("Done!")
|
2020-05-08 14:25:20 +02:00
|
|
|
}
|
2020-06-23 17:37:07 +02:00
|
|
|
|
|
|
|
func handleErr(err error, message string) {
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("%s: %v", message, err)
|
|
|
|
}
|
|
|
|
}
|