Setting Up Docker Services for Monitoring

To effectively monitor your .NET applications using OpenTelemetry, a typical setup involves running the application alongside a monitoring backend like Zipkin. Below is a guide to configuring your services using docker-compose and setting up the necessary environment variables.

Docker Compose Configuration

Create a docker-compose.yml to define your services:

version: '3.8'

services:
  zipkin:
    image: openzipkin/zipkin
    ports:
      - 9411:9411

  rabbitmq:
    image: rabbitmq:3-management-alpine
    ports:
      - 5672:5672
      - 15672:15672

  webapi:
    build:
      context: ../..
      dockerfile: ./examples/MicroserviceExample/WebApi/Dockerfile
    image: opentelemetry-example-webapi
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - RABBITMQ_HOSTNAME=rabbitmq
      - RABBITMQ_DEFAULT_USER=guest
      - RABBITMQ_DEFAULT_PASS=guest
      - ZIPKIN_HOSTNAME=zipkin
    ports:
      - 5000:5000
    depends_on:
      - rabbitmq
      - zipkin

  workerservice:
    build:
      context: ../..
      dockerfile: ./examples/MicroserviceExample/WorkerService/Dockerfile
    image: opentelemetry-example-workerservice
    environment:
      - DOTNET_ENVIRONMENT=Development
      - RABBITMQ_HOSTNAME=rabbitmq
      - RABBITMQ_DEFAULT_USER=guest
      - RABBITMQ_DEFAULT_PASS=guest
      - ZIPKIN_HOSTNAME=zipkin
    restart: on-failure
    depends_on:
      - rabbitmq
      - zipkin

This setup includes:

  • Zipkin: The tracing backend to gather and visualize trace data.
  • RabbitMQ: A message broker that facilitates communication between services, essential for distributed tracing.
  • Web API and Worker Service: Your application services that emit tracing data.

Service Configuration

The Worker Service is defined in a Dockerfile, which can be configured as follows:

ARG SDK_VERSION=8.0
FROM mcr.microsoft.com/dotnet/sdk:${SDK_VERSION} AS build
ARG PUBLISH_CONFIGURATION=Release
ARG PUBLISH_FRAMEWORK=net8.0
WORKDIR /app
COPY . ./
RUN dotnet publish ./examples/MicroserviceExample/WorkerService -c "${PUBLISH_CONFIGURATION}" -f "${PUBLISH_FRAMEWORK}" -o /out -p:IntegrationBuild=true

FROM mcr.microsoft.com/dotnet/aspnet:${SDK_VERSION} AS runtime
WORKDIR /app
COPY --from=build /out ./
ENTRYPOINT ["dotnet", "WorkerService.dll"]

This Dockerfile handles the build and runtime environment for the Worker Service, ensuring it is properly configured for deployment.

Instrumentation for Monitoring

Integrating OpenTelemetry

In your .NET services, you need to add OpenTelemetry instrumentation. Typically, this involves configuring the OpenTelemetry SDK to send telemetry data to Zipkin. Below is how to set up the OpenTelemetry SDK in your application:

using OpenTelemetry;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

var builder = WebApplication.CreateBuilder(args);

// Create a resource to identify the service
builder.Services.AddOpenTelemetryTracing(tracerProviderBuilder =>
{
    tracerProviderBuilder
        .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("YourServiceName"))
        .AddAspNetCoreInstrumentation()
        .AddRabbitMQInstrumentation()
        .AddZipkinExporter(options =>
        {
            options.Endpoint = new Uri("http://zipkin:9411/api/v2/spans");
        });
});

// Add other service configurations here

var app = builder.Build();

// Configure the HTTP request pipeline
app.UseRouting();
app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
});

app.Run();

Environment Variables

Make sure the environment variables specified in the docker-compose.yml are correctly set to ensure seamless integration between services and monitoring:

  • RABBITMQ_HOSTNAME: The hostname for the RabbitMQ service.
  • ZIPKIN_HOSTNAME: The hostname for the Zipkin service.

These variables are crucial for the services to communicate and send telemetry data effectively.

Running and Accessing Monitoring Services

To start the services, execute the following command in the directory containing your docker-compose.yml:

docker-compose up

After the services are running, you can access Zipkin at http://localhost:9411 to visualize the traces gathered by your application.

Conclusion

This setup provides a complete monitoring solution for your .NET applications using OpenTelemetry. By following these configurations, you can efficiently gather tracing data and monitor your services in a production environment, allowing deeper insight into your application’s performance and behavior.

(Note: Source of the information is from files provided by the user.)