Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .agents/skills/telemetry/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Each layer publishes under its own name, and only the presentation layer's start
| --- | --- | --- |
| Application | `ApplicationTelemetry.Name` activity source: a span per use case, named after the request, tagged with `application.request.name` and `application.request.outcome` | `LoggingBehaviour`, with `System.Diagnostics` only, so Application stays free of telemetry packages |
| Infrastructure | `Microsoft.EntityFrameworkCore` meter: queries, saves, active contexts, compiled query cache | Built into EF Core; startup subscribes to it by name |
| Presentation | `service.name`, `service.version` and `deployment.environment.name` on everything, plus its own instrumentation | `TelemetrySettings` and the startup's Telemetry region |
| Presentation | `service.name`, `service.version` and `deployment.environment.name` on everything, plus its own instrumentation | `TelemetrySettings`, which takes `service.version` from `VersionInfo`, and the startup's Telemetry region |

What each presentation layer adds:

Expand Down
10 changes: 5 additions & 5 deletions src/Presentation/Extensions/WebApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace CleanMinimalApi.Presentation.Extensions;

using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using Application;
using Application.Common.Telemetry;
using Application.Versions.Entities;
Expand Down Expand Up @@ -35,7 +34,8 @@ public static WebApplicationBuilder ConfigureApplicationBuilder(this WebApplicat
#region Telemetry

var assembly = typeof(WebApplicationBuilderExtensions).Assembly;
var telemetry = TelemetrySettings.FromConfiguration(builder.Configuration, builder.Environment.EnvironmentName, assembly);
var version = VersionInfo.FromAssembly(assembly);
var telemetry = TelemetrySettings.FromConfiguration(builder.Configuration, builder.Environment.EnvironmentName, version);

var openTelemetry = builder.Services.AddOpenTelemetry()
.ConfigureResource(resource => resource.AddAttributes(telemetry.ResourceAttributes))
Expand Down Expand Up @@ -79,10 +79,10 @@ public static WebApplicationBuilder ConfigureApplicationBuilder(this WebApplicat
_ = loggerConfiguration.ReadFrom.Configuration(configuration)
.Enrich.WithProperty(
"AssemblyVersion",
assembly.GetCustomAttribute<AssemblyFileVersionAttribute>()?.Version)
version.FileVersion)
.Enrich.WithProperty(
"AssemblyInformationalVersion",
assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion);
version.InformationalVersion);

// Logs leave over OTLP too, carrying the trace and span IDs of whatever was running when they were written.
if (telemetry.Enabled)
Expand Down Expand Up @@ -164,7 +164,7 @@ public static WebApplicationBuilder ConfigureApplicationBuilder(this WebApplicat
_ = builder.Services.AddMediator(options => options.ServiceLifetime = ServiceLifetime.Scoped);
_ = builder.Services.AddInfrastructure(builder.Configuration.GetConnectionString("MovieReviews"));
_ = builder.Services.AddApplication();
_ = builder.Services.AddSingleton(VersionInfo.FromAssembly(typeof(WebApplicationBuilderExtensions).Assembly));
_ = builder.Services.AddSingleton(version);
_ = builder.Services.AddHealthChecks().AddInfrastructureHealthChecks("ready");

#endregion Project Dependencies
Expand Down
8 changes: 4 additions & 4 deletions src/Presentation/Telemetry/TelemetrySettings.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace CleanMinimalApi.Presentation.Telemetry;

using System.Reflection;
using Application.Versions.Entities;
using Microsoft.Extensions.Configuration;

/// <summary>
Expand All @@ -16,7 +16,7 @@ public class TelemetrySettings
/// <summary>The <c>service.name</c> on everything this app sends.</summary>
public required string ServiceName { get; init; }

/// <summary>The <c>service.version</c>: the informational version of the presentation assembly.</summary>
/// <summary>The <c>service.version</c>: the informational version from <see cref="VersionInfo"/>, the same one the version endpoint returns.</summary>
public required string ServiceVersion { get; init; }

/// <summary>The <c>deployment.environment.name</c>, such as <c>local</c> or <c>docker</c>.</summary>
Expand All @@ -35,14 +35,14 @@ public class TelemetrySettings
["deployment.environment.name"] = this.EnvironmentName
};

public static TelemetrySettings FromConfiguration(IConfiguration configuration, string environmentName, Assembly assembly)
public static TelemetrySettings FromConfiguration(IConfiguration configuration, string environmentName, VersionInfo version)
{
var serviceName = configuration["OTEL_SERVICE_NAME"];

return new TelemetrySettings
{
ServiceName = string.IsNullOrWhiteSpace(serviceName) ? DefaultServiceName : serviceName,
ServiceVersion = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? "unknown",
ServiceVersion = string.IsNullOrWhiteSpace(version.InformationalVersion) ? "unknown" : version.InformationalVersion,
EnvironmentName = environmentName,
OtlpEndpoint = configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace CleanMinimalApi.Presentation.Tests.Unit.Telemetry;

using System.Reflection;
using System.Reflection.Emit;
using Application.Versions.Entities;
using Microsoft.Extensions.Configuration;
using Presentation.Telemetry;
using Shouldly;
Expand All @@ -14,15 +13,15 @@ public void FromConfiguration_ShouldReturn_DisabledDefaults_WhenNothingIsSet()
{
// Arrange
var configuration = new ConfigurationBuilder().Build();
var assembly = typeof(TelemetrySettingsTests).Assembly;
var version = new VersionInfo { FileVersion = "1.2.3.0", InformationalVersion = "1.2.3+e30eba8" };

// Act
var result = TelemetrySettings.FromConfiguration(configuration, "local", assembly);
var result = TelemetrySettings.FromConfiguration(configuration, "local", version);

// Assert
result.Enabled.ShouldBeFalse();
result.ServiceName.ShouldBe(TelemetrySettings.DefaultServiceName);
result.ServiceVersion.ShouldBe(assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()!.InformationalVersion);
result.ServiceVersion.ShouldBe("1.2.3+e30eba8");
result.EnvironmentName.ShouldBe("local");
}

Expand All @@ -39,25 +38,25 @@ public void FromConfiguration_ShouldReturn_EnabledSettings_WhenEndpointIsSet()
.Build();

// Act
var result = TelemetrySettings.FromConfiguration(configuration, "docker", typeof(TelemetrySettingsTests).Assembly);
var result = TelemetrySettings.FromConfiguration(configuration, "docker", new VersionInfo { FileVersion = "1.2.3.0", InformationalVersion = "1.2.3" });

// Assert
result.Enabled.ShouldBeTrue();
result.OtlpEndpoint.ShouldBe("http://aspire-dashboard:18889");
result.ResourceAttributes["service.name"].ShouldBe("movie-reviews");
result.ResourceAttributes["service.version"].ShouldBe(result.ServiceVersion);
result.ResourceAttributes["service.version"].ShouldBe("1.2.3");
result.ResourceAttributes["deployment.environment.name"].ShouldBe("docker");
}

[Fact]
public void FromConfiguration_ShouldReturn_UnknownVersion_WhenAssemblyHasNone()
public void FromConfiguration_ShouldReturn_UnknownVersion_WhenVersionIsEmpty()
{
// Arrange
var configuration = new ConfigurationBuilder().Build();
var assembly = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("NoVersion"), AssemblyBuilderAccess.Run);
var version = VersionInfo.FromAssembly(null);

// Act
var result = TelemetrySettings.FromConfiguration(configuration, "local", assembly);
var result = TelemetrySettings.FromConfiguration(configuration, "local", version);

// Assert
result.ServiceVersion.ShouldBe("unknown");
Expand Down
Loading