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"
|
2022-05-19 22:15:07 +02:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2020-05-08 14:25:20 +02:00
|
|
|
"time"
|
|
|
|
|
2020-05-11 12:42:36 +02:00
|
|
|
"google.golang.org/grpc"
|
2021-12-20 18:54:44 +02:00
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
2020-05-11 12:42:36 +02:00
|
|
|
|
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"
|
2023-01-24 18:10:41 +02:00
|
|
|
semconv "go.opentelemetry.io/otel/semconv/v1.17.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.
|
2022-05-19 22:15:07 +02:00
|
|
|
func initProvider() (func(context.Context) error, error) {
|
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"),
|
|
|
|
),
|
|
|
|
)
|
2022-05-19 22:15:07 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create resource: %w", err)
|
|
|
|
}
|
2020-10-31 20:16:55 +02:00
|
|
|
|
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
|
2022-11-28 17:22:59 +02:00
|
|
|
// probably connect directly to the service through dns.
|
2022-06-02 17:18:17 +02:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, time.Second)
|
|
|
|
defer cancel()
|
2022-11-28 17:22:59 +02:00
|
|
|
conn, err := grpc.DialContext(ctx, "localhost:30080",
|
|
|
|
// Note the use of insecure transport here. TLS is recommended in production.
|
|
|
|
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
|
|
|
grpc.WithBlock(),
|
|
|
|
)
|
2022-05-19 22:15:07 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create gRPC connection to collector: %w", err)
|
|
|
|
}
|
2021-06-11 17:18:17 +02:00
|
|
|
|
|
|
|
// Set up a trace exporter
|
2021-11-25 18:06:21 +02:00
|
|
|
traceExporter, err := otlptracegrpc.New(ctx, otlptracegrpc.WithGRPCConn(conn))
|
2022-05-19 22:15:07 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create trace exporter: %w", err)
|
|
|
|
}
|
2021-06-11 17:18:17 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2022-05-19 22:15:07 +02:00
|
|
|
// Shutdown will flush any remaining spans and shut down the exporter.
|
|
|
|
return tracerProvider.Shutdown, nil
|
2020-06-23 17:37:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
log.Printf("Waiting for connection...")
|
|
|
|
|
2022-05-19 22:15:07 +02:00
|
|
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
shutdown, err := initProvider()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := shutdown(ctx); err != nil {
|
|
|
|
log.Fatal("failed to shutdown TracerProvider: %w", err)
|
|
|
|
}
|
|
|
|
}()
|
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
|
|
|
|
2022-04-18 16:31:31 +02:00
|
|
|
// Attributes represent additional key-value descriptors that can be bound
|
|
|
|
// to a metric observer or recorder.
|
|
|
|
commonAttrs := []attribute.KeyValue{
|
|
|
|
attribute.String("attrA", "chocolate"),
|
|
|
|
attribute.String("attrB", "raspberry"),
|
|
|
|
attribute.String("attrC", "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(
|
2022-05-19 22:15:07 +02:00
|
|
|
ctx,
|
2020-07-31 20:34:46 +02:00
|
|
|
"CollectorExporter-Example",
|
2022-04-18 16:31:31 +02:00
|
|
|
trace.WithAttributes(commonAttrs...))
|
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
|
|
|
}
|