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/LambdaFunction/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace CleanLambdaFunction.Presentation.LambdaFunction;

using System.Reflection;
using Amazon.Lambda.Annotations;
using Application;
using Application.Common.Telemetry;
Expand Down Expand Up @@ -66,7 +65,8 @@ public HostApplicationBuilder ConfigureHostBuilder()
#region Telemetry

var assembly = typeof(Startup).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);

// The generated classes build two service providers from these services, one for the functions and one for
// the host. The providers are built here, once, so both share them and nothing is exported twice.
Expand Down Expand Up @@ -109,10 +109,10 @@ public HostApplicationBuilder ConfigureHostBuilder()
.Enrich.WithProperty("ApplicationName", ApplicationName)
.Enrich.WithProperty(
"AssemblyVersion",
assembly.GetCustomAttribute<AssemblyFileVersionAttribute>()?.Version)
version.FileVersion)
.Enrich.WithProperty(
"AssemblyInformationalVersion",
assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion)
version.InformationalVersion)
.WriteTo.Console(new RenderedCompactJsonFormatter())
.ReadFrom.Configuration(configuration);

Expand All @@ -136,7 +136,7 @@ public HostApplicationBuilder ConfigureHostBuilder()
_ = builder.Services.AddMediator(options => options.ServiceLifetime = ServiceLifetime.Scoped);
_ = builder.Services.AddInfrastructure(builder.Configuration.GetConnectionString("MovieReviews") ?? DefaultConnectionString);
_ = builder.Services.AddApplication();
_ = builder.Services.AddSingleton(VersionInfo.FromAssembly(typeof(Startup).Assembly));
_ = builder.Services.AddSingleton(version);

#endregion Services

Expand Down
10 changes: 5 additions & 5 deletions src/Presentation/LambdaFunction/Telemetry/TelemetrySettings.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace CleanLambdaFunction.Presentation.LambdaFunction.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>, from <c>DOTNET_ENVIRONMENT</c>, or <c>Production</c> when it isn't set.</summary>
Expand All @@ -41,16 +41,16 @@ public class TelemetrySettings
/// </summary>
/// <param name="configuration">The settings to read <c>OTEL_SERVICE_NAME</c> and <c>OTEL_EXPORTER_OTLP_ENDPOINT</c> from.</param>
/// <param name="environmentName">The host's environment name.</param>
/// <param name="assembly">The assembly whose informational version becomes <c>service.version</c>.</param>
/// <param name="version">The app's version, whose informational version becomes <c>service.version</c>.</param>
/// <returns>The telemetry settings.</returns>
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 CleanLambdaFunction.Presentation.Tests.Unit.Telemetry;

using System.Reflection;
using System.Reflection.Emit;
using Application.Versions.Entities;
using Microsoft.Extensions.Configuration;
using Presentation.LambdaFunction.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