Skip to content

Open Telemetry Instrumentation

Leonard Sperry edited this page Sep 2, 2024 · 3 revisions

HaKafkaNet has support for Open Telemetry. You will need some way to view the data. The current recommended options are:

  • Aspire Dashboard: documentation or stand-alone Docker image
  • Open Telemetry Collector: documentation. This can be used with several backends such as the Grafana stack with Loki, Prometheus, and Tempo.

Metrics and Traces:

When configured, HakafkaNet adds several metrics and traces to your telemetry data indlucing:

  • Metrics
    • Cache hit counter
    • Cache miss counter
    • Trace Counter
    • Current active trace total
    • Kafka message counter
  • Traces
    • Any call to your automations
    • Any call to HA API
    • Any call to the cache

To enable HaKafkaNet telemetry, you need only modify your program.cs by adding the following:

var otlpEndpoint = "http://your_otlp_endpoint:4317";

services.AddOpenTelemetry()
    .ConfigureResource(resource => {
        resource.AddService(serviceName: "home-automations");
    }).WithTracing(tracing =>{ 
        tracing
            .AddAspNetCoreInstrumentation()
            .AddHttpClientInstrumentation()
            .AddHaKafkaNetInstrumentation() // <- HaKafkaNet instrumentation
            //.AddSource(KafkaFlowInstrumentation.ActivitySourceName)
            .AddOtlpExporter(exporterOptions => {
                exporterOptions.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.Grpc;
                exporterOptions.Endpoint = new Uri(otlpEndpoint);
                exporterOptions.ExportProcessorType = ExportProcessorType.Batch;
            });
    }).WithMetrics(metrics => {
        metrics.AddAspNetCoreInstrumentation()
            .AddMeter("Microsoft.AspNetCore.Hosting")
            .AddMeter("Microsoft.AspNetCore.Server.Kestrel")
            .AddHaKafkaNetInstrumentation() // <- HaKafkaNet instrumentation
            .AddAspNetCoreInstrumentation()
            .AddHttpClientInstrumentation()
            .AddOtlpExporter((exporterOptions, metricReaderOptions) =>{
                exporterOptions.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.Grpc;
                exporterOptions.Endpoint = new Uri(otlpEndpoint);
                exporterOptions.ExportProcessorType = ExportProcessorType.Batch;
            });
    });

builder.Logging.AddOpenTelemetry(logging => {
    logging.IncludeScopes = true;
    logging.IncludeFormattedMessage = true;
    logging.ParseStateValues = true;
    logging.AddOtlpExporter((exporterOptions, logProcessOptions) =>{
            exporterOptions.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.Grpc;
            exporterOptions.Endpoint = new Uri(otlpEndpoint);
            exporterOptions.ExportProcessorType = ExportProcessorType.Batch;
        });
});