1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-04 09:43:23 +02:00
opentelemetry-go/example/otel-collector
2020-05-14 11:11:51 +02:00
..
k8s add k8s files 2020-05-14 11:11:51 +02:00
go.mod add resource attributes to provider 2020-05-13 12:38:23 +02:00
go.sum fix precommit 2020-05-13 12:55:33 +02:00
main.go Merge branch 'master' of github.com:stefanprisca/opentelemetry-go into sp-master 2020-05-13 12:40:02 +02:00
Makefile add k8s files 2020-05-14 11:11:51 +02:00
README.md Update example/otel-collector/README.md 2020-05-12 13:04:09 +02:00

OpenTelemetry Collector Traces Example

This example illustrates how to export traces from the OpenTelemetry-Go SDK to the OpenTelemetry Collector, and from there to a Jaeger instance. The complete flow is:

otel-collector-demo -> otel-collector -> Jaeger

Prerequisites

The demo assumes you already have both an instance of the OpenTelemetry Collector and Jaeger up and running. For information about how to set these up, please follow the corresponding documentation:

Moreover, this demo is built on a Kubernetes cluster running both the OpenTelemetry Collector, and Jaeger. Therefore, the included OpenTelemetry Collector configuration is a Kubernetes ConfigMap. However, the main concepts should be transferable to any other platform you want to run this demo on.

Configuring the OTEL Collector

In order to enable our application to send traces to the OpenTelemetry Collector, we need to first configure the OTLP receiver:

receivers:
      otlp: {}

This will create the receiver on the Collector side, and open up port 55680 for receiving traces. The rest of the configuration is quite standard, with the only mention that we need to create the Jaeger exporter:

exporters:
      jaeger:
        # Replace with a real endpoint.
        endpoint: "<jaeger-service-url>:14250"

After this, apply the configuration to your OpenTelemetry Collector instance (with kubectl apply -f otel-controller-config.yaml for k8s users). You should see that the Collector creates the otlp receiver:

{"level":"info","ts":1589184143.206609,"caller":"builder/receivers_builder.go:79","msg":"Receiver started.","component_kind":"receiver","component_type":"otlp","component_name":"otlp"}

and the Jaeger exporter:

{"level":"info","ts":1589184143.1535392,"caller":"builder/exporters_builder.go:94","msg":"Exporter started.","component_kind":"exporter","component_type":"jaeger","component_name":"jaeger"}

Writing the demo

Having the OpenTelemetry Collector started with the OTLP port open for traces, and connected to Jaeger, let's look at the go app that will send traces to the Collector.

First, we need to create an exporter using the otlp package:

exp, _ := otlp.NewExporter(otlp.WithInsecure(),
        // For debug purposes
        otlp.WithGRPCDialOption(grpc.WithBlock()))
defer func() {
		_ = exp.Stop()
	}()

This will initialize the exporter with the default configuration. In this configuration, it will try to connect to an OTLP receiver at the address localhost:55680. If your OpenTelemetry Collector is running at a different address, use the otlp.WithAddress function to change the default address.

Feel free to remove the blocking operation, but it might come in handy when testing the connection. Also, make sure to close the exporter before the app exits.

The next steps are the same as for all other OpenTelemetry-Go SDK uses:

  1. Create a trace provider from the otlp exporter:
tp, _ := sdktrace.NewProvider(
		sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}),
		sdktrace.WithBatcher(exp, // add following two options to ensure flush
			sdktrace.WithScheduleDelayMillis(5),
			sdktrace.WithMaxExportBatchSize(2),
        ))
  1. Start sending traces:
tracer := tp.Tracer("test-tracer")
ctx, span := tracer.Start(context.Background(), "CollectorExporter-Example")
	defer span.End()

The traces should now be visible from the Jaeger UI (if you have it installed).

Notes