Open Telemetry

From bibbleWiki
Jump to navigation Jump to search

Introduction

This is a quick page about open telemetry which I came across doing the Effect_TS.

Four things

Four things must ye know about Open Telemetry

  • Traces - Spans
  • Metrics - Performances, counts etc
  • Logs - No Explanation required
  • Baggage - contextual information we want to pass from span to span

This is an example of a trace with spans.

Trace: "User logs in"
├── Span: "Frontend sends login request"
├── Span: "API receives request"
│   ├── Span: "Validate credentials"
│   └── Span: "Query user DB"

Other Terms

  • OTLP - Open Telemetry Protocol Sent over gRPC or HTTP.
  • Collector - Own Service to accept data and push it out
  • Instrumentation - How you make the data. Some frameworks like expressJS, effectTS have automatic but you can use a OpenTelemetry SDK

Configuring Collector

There are six things you configure in a collector

  • Receivers - How is how you collector takes in data
  • Processors - Cleansing, Transform
  • Extensions - Extra things, e.g. health monitor
  • Exporters - Push data out, e.g Loki, Prometheus
  • Pipelines - The path that telemetry data—traces, metrics, or logs—follows from ingestion to export
  • Connectors - Can export data from one pipeline and feed it into another
connectors:
  spanmetrics:

service:
  pipelines:
    traces:
      receivers: [otlp]
      exporters: [spanmetrics]

    metrics:
      receivers: [spanmetrics]
      exporters: [prometheus]

Example with Node

npm install @opentelemetry/sdk-node \
  @opentelemetry/api \
  @opentelemetry/auto-instrumentations-node \
  @opentelemetry/sdk-metrics \
  @opentelemetry/sdk-trace-node

We can run this with

npx  tsx --import  ./src/opentelemetry/instrumentation.ts  ./src/app.ts

Example with CSharp

Configure Resources

In CSharp you have, and I expect in the other too, Resources. These are things you configure once and a attached to the OTel data. They are only sent once per batch. To set these up you do the following. Add the nuget packages

dotnet add package OpenTelemetry
dotnet add package OpenTelemetry.Extensions.Hosting

And then add an extension to build them.

    public static IServiceCollection AddAppConfiguration(this IServiceCollection services, IConfiguration configuration)
    {
        services.AddOpenTelemetry()
            .ConfigureResource(resourceBuilder =>
                    resourceBuilder.AddAttributes(new Dictionary<string, object>
                    {
                        { "service.name", "GrpcServerCs" },
                        { "service.namespace", "GrpcServer" },
                        { "service.instance.id", Environment.MachineName },
                        { "service.version", "1.0.0" },
                        { "deployment.environment", "development" }
                    })
        );
        return services;
    }