Configuration for the OpenTelemetry Demo involves setting environment variables, modifying code settings, and preparing the necessary services for your development setup. Below are the detailed steps and code examples to properly configure your environment.
Step 1: Setup Environment Variables
Depending on the specific services being initialized, set environment variables to control the behavior of OpenTelemetry. Variables can typically be set in your shell configuration for local development.
# Setting endpoints for the OpenTelemetry collector
export OTEL_EXPORTER_OTLP_ENDPOINT="http://localhost:4317"
# Setting the service name for tracing
export OTEL_SERVICE_NAME="opentelemetry-demo-service"
Step 2: Configure Go Module
When building the Go application, ensure that the module name reflects the proper path. The module name for the checkout service is set in the go.mod
file.
module github.com/open-telemetry/opentelemetry-demo/src/checkoutservice
go 1.16
Be sure not to use -mod=vendor
when building the service. Build the service with the typical command:
go build
Step 3: Initialize OpenTelemetry in Your Code
Depending on the programming language, initialization of OpenTelemetry differs slightly. Below are examples for various supported languages.
TypeScript
In your index.ts
, ensure proper initialization of OpenTelemetry.
import { NodeTracerProvider } from '@opentelemetry/node';
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { getTracer } from '@opentelemetry/api';
const provider = new NodeTracerProvider();
provider.register();
registerInstrumentations({
tracerProvider: provider,
instrumentations: [],
});
const tracer = getTracer('opentelemetry-demo');
Python
In your app.py
, set up tracing as follows:
from opentelemetry import trace
from opentelemetry.exporter.otlp.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.traces import TracerProvider
from opentelemetry.sdk.resources import Resource
resource = Resource(attributes={"service.name": "opentelemetry-demo-service"})
trace.set_tracer_provider(TracerProvider(resource=resource))
exporter = OTLPSpanExporter(endpoint="http://localhost:4317")
Java
For Java, initialize OpenTelemetry in your main application file:
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.api.GlobalOpenTelemetry;
public class Main {
public static void main(String[] args) {
OpenTelemetry openTelemetry = OpenTelemetrySdk.builder().build();
Tracer tracer = openTelemetry.getTracer("opentelemetry-demo");
// Your tracing logic here
}
}
C#
In a C# application, configure the OpenTelemetry SDK in the Program.cs
:
using OpenTelemetry;
using OpenTelemetry.Trace;
class Program {
static void Main(string[] args) {
Sdk.CreateTracerProviderBuilder()
.AddSource("opentelemetry-demo")
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("opentelemetry-demo-service"))
.Build();
// Tracing logic here
}
}
Docker Configuration
If utilizing Docker, ensure your Dockerfile
is correctly set up to copy the necessary configuration files and environment variables. An example Dockerfile
snippet for Python services:
FROM python:3.9
WORKDIR /app
COPY . .
ENV OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317
ENV OTEL_SERVICE_NAME=opentelemetry-demo-service
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
Step 4: Using Makefile for Build Management
In the root directory of your project, include a Makefile
to automate the build process. An example Makefile
could look like this:
.PHONY: build
build:
go build github.com/open-telemetry/opentelemetry-demo/src/checkoutservice
run: build
./checkoutservice
Step 5: Verify Configuration
To validate that your configuration works as expected:
- Run your application in your chosen development environment.
- Use tools like
curl
or browser access to ensure that telemetry data is being sent to the specified endpoint. - Observe logs to check for errors in the initialization of OpenTelemetry.
Source for configuration setup: OpenTelemetry Documentation Basic Examples.