Skip to content

Commit

Permalink
feat: or-2583 Add more telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
koenmetsu committed Jan 24, 2025
1 parent 9f1f8a2 commit b31b54e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 33 deletions.
23 changes: 11 additions & 12 deletions src/AssociationRegistry.KboMutations.SyncLambda/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,11 @@
using Npgsql;
using Weasel.Core;
using PostgreSqlOptionsSection = AssociationRegistry.KboMutations.SyncLambda.Logging.PostgreSqlOptionsSection;
using SsmClientWrapper = AssociationRegistry.KboMutations.SsmClientWrapper;

namespace AssociationRegistry.KboMutations.SyncLambda;

using global::OpenTelemetry.Metrics;
using global::OpenTelemetry.Trace;
using KboMutations.Configuration;
using Notifications;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
using System.Diagnostics.Metrics;

public class Function
Expand Down Expand Up @@ -76,20 +71,20 @@ private static async Task FunctionHandler(SQSEvent @event, ILambdaContext contex
var eventConflictResolver = new EventConflictResolver(Array.Empty<IEventPreConflictResolutionStrategy>(),
Array.Empty<IEventPostConflictResolutionStrategy>());

_openTelemetrySetup = new OpenTelemetrySetup();

var meter = new Meter(OpenTelemetrySetup.MeterName);

var counter = meter.CreateCounter<int>("kbosync_started");
counter.Add(1);

var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddProvider(new LambdaLoggerProvider(context.Logger));

_openTelemetrySetup.SetUpLogging(builder);
});

_openTelemetrySetup = new OpenTelemetrySetup(context.Logger);

var meter = new Meter(OpenTelemetrySetup.MeterName);

var counter = meter.CreateCounter<int>("kbosync_started");
counter.Add(1);

var repository = new VerenigingsRepository(new EventStore.EventStore(store, eventConflictResolver, loggerFactory.CreateLogger<EventStore.EventStore>()));

var geefOndernemingService = new MagdaGeefVerenigingService(
Expand Down Expand Up @@ -147,6 +142,10 @@ private static async Task<DocumentStore> SetUpDocumentStore(IConfiguration confi
throw new ApplicationException("PostgresSqlOptions is missing some values");

var opts = new StoreOptions();

opts.OpenTelemetry.TrackConnections = TrackLevel.Normal;
opts.OpenTelemetry.TrackEventCounters();

var connectionStringBuilder = new NpgsqlConnectionStringBuilder();
connectionStringBuilder.Host = postgresSection.Host;
connectionStringBuilder.Database = postgresSection.Database;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace AssociationRegistry.KboMutations.SyncLambda;

using Amazon.Lambda.Core;
using Microsoft.Extensions.Logging;
using Npgsql;
using OpenTelemetry;
using OpenTelemetry.Exporter;
using OpenTelemetry.Logs;
Expand All @@ -12,19 +14,19 @@ namespace AssociationRegistry.KboMutations.SyncLambda;
public class OpenTelemetrySetup : IDisposable
{
private readonly OpenTelemetryResources _resources;
private readonly Object _resource;
private const string OtelAuthHeader = "OTEL_AUTH_HEADER";
private const string MetricsUri = "OTLP_METRICS_URI";
private const string TracingUri = "OTLP_TRACING_URI";
private const string OtlpLogsUri = "OTLP_LOGS_URI";
public TracerProvider TracerProvider { get; }
public MeterProvider MeterProvider { get; }

public const string MeterName = "KboMutations.SyncLambda.Metrics";
public const string MeterName = "kbomutations.sync.lambda.metrics";

public OpenTelemetrySetup()
public OpenTelemetrySetup(ILambdaLogger contextLogger)
{
_resources = GetResources();
_resources = GetResources(contextLogger);

MeterProvider = SetupMeter();
TracerProvider = SetUpTracing();
}
Expand All @@ -35,7 +37,8 @@ public MeterProvider SetupMeter()

var builder = Sdk.CreateMeterProviderBuilder()
.ConfigureResource(_resources.ConfigureResourceBuilder)
.AddMeter(MeterName.ToLowerInvariant())
.AddMeter(MeterName)
.AddMeter("Marten")
.AddConsoleExporter()
.AddRuntimeInstrumentation()
.AddHttpClientInstrumentation();
Expand All @@ -58,8 +61,9 @@ public TracerProvider SetUpTracing()
var otlpTracingUri = Environment.GetEnvironmentVariable(TracingUri);

var builder = Sdk.CreateTracerProviderBuilder()
.AddHttpClientInstrumentation()
.AddNpgsql()
.ConfigureResource(_resources.ConfigureResourceBuilder)
.AddSource("AssociationRegistry")
.AddConsoleExporter();

if (!string.IsNullOrEmpty(otlpTracingUri))
Expand All @@ -84,30 +88,42 @@ public void SetUpLogging(ILoggingBuilder builder)

if (!string.IsNullOrEmpty(otlpLogssUri))

options.AddOtlpExporter(options =>
options.AddOtlpExporter(exporterOptions =>
{
options.Endpoint = new Uri(otlpLogssUri);
exporterOptions.Endpoint = new Uri(otlpLogssUri);

AddAuth(options);
AddAuth(exporterOptions);
});
});
}

public OpenTelemetryResources GetResources()
public OpenTelemetryResources GetResources(ILambdaLogger contextLogger)
{
var serviceName = "KboMutations.SyncLambda"; // Explicit service name
var serviceName = "KboMutations.SyncLambda";
var assemblyVersion = Assembly.GetExecutingAssembly()?.GetName()?.Version?.ToString() ?? "unknown";
var serviceInstanceId = Environment.GetEnvironmentVariable("AWS_LAMBDA_FUNCTION_NAME") ?? Environment.MachineName;
var environment = Environment.GetEnvironmentVariable("ENVIRONMENT")?.ToLowerInvariant() ?? "unknown";

Action<ResourceBuilder> configureResource = r => r
.AddService(
serviceName,
serviceVersion: assemblyVersion,
serviceInstanceId: Environment.GetEnvironmentVariable("AWS_LAMBDA_FUNCTION_NAME") ?? Environment.MachineName)
.AddAttributes(
new Dictionary<string, object>
{
["deployment.environment"] = Environment.GetEnvironmentVariable("ENVIRONMENT")?.ToLowerInvariant() ?? "unknown",
});
Action<ResourceBuilder> configureResource = r =>
{
r
.AddService(
serviceName,
serviceVersion: assemblyVersion,
serviceInstanceId: serviceInstanceId)
.AddAttributes(
new Dictionary<string, object>
{
["deployment.environment"] = environment,
});
};

contextLogger.LogInformation("Resource configuration: " +
"Service name '{ServiceName}', " +
"ServiceVersion '{AssemblyVersion}', " +
"Service Instance Id '{ServiceInstanceId}', " +
"Env '{Env}'",
serviceName, assemblyVersion, serviceInstanceId, environment);

return new OpenTelemetryResources(serviceName, configureResource);
}
Expand Down

0 comments on commit b31b54e

Please sign in to comment.