Skip to content

Commit

Permalink
update otel
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasisnes committed Sep 30, 2024
1 parent bff1ca8 commit 76ed499
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public static WebApplicationBuilder ConfigureServices(WebApplicationBuilder buil
{
builder.AddAltinnHostDefaults();
builder.AddAltinnDefaultOpenTelemetry(otel => otel.ServiceName = service);
builder.AddAltinnDefaultPostgresConnection();

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,13 @@ public static IHostApplicationBuilder AddAltinnOpenTelemetry(this IHostApplicati
var otel = builder.Services.AddOpenTelemetry()
.ConfigureResource(resource =>
{
var assembly = Assembly.GetCallingAssembly().FullName;
var serviceVersion = Environment.GetEnvironmentVariable("CONTAINER_APP_REVISION");
var serviceInstanceId = Environment.GetEnvironmentVariable("CONTAINER_APP_REPLICA_NAME");
resource.AddService(config.ServiceName, assembly, serviceVersion, string.IsNullOrEmpty(serviceInstanceId), serviceInstanceId);
resource.AddService(config.ServiceName, config.ServiceName, config.ServiceVersion, string.IsNullOrEmpty(config.ServiceInstanceId), config.ServiceInstanceId);
})
.WithLogging()
.WithTracing(tracing =>
tracing.AddAspNetCoreInstrumentation(instrumentation =>
{
instrumentation.Filter = (context) =>
{
foreach (var collectRequest in config.Filters)
{
if (!collectRequest(context))
{
return false;
}
}

return true;
};
instrumentation.Filter = (context) => config.Filters.All(filter => filter(context));
instrumentation.EnrichWithHttpRequest = (activity, request) =>
{
foreach (var header in config.TraceHeaders)
Expand All @@ -86,7 +72,6 @@ public static IHostApplicationBuilder AddAltinnOpenTelemetry(this IHostApplicati
}
};
})
.AddHttpClientInstrumentation()
)
.WithMetrics(options =>
options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,32 @@ namespace Altinn.Authorization.Configuration.OpenTelemetry.Options
/// </summary>
public class AltinnOpenTelemetryOptions
{
/// <summary>
/// Gets or sets the name of the service being monitored.
/// This is typically used to identify the service in OpenTelemetry traces.
/// </summary>
public string ServiceName { get; set; }

/// <summary>
/// Ges or sets OTEL Service version. It's by default set to the container app's revision
/// </summary>
public string ServiceVersion { get; set; } = Environment.GetEnvironmentVariable("CONTAINER_APP_REVISION");

/// <summary>
/// Ges or sets OTEL Service InstanceID. It's by default set to the container's replica name
/// </summary>
public string ServiceInstanceId { get; set; } = Environment.GetEnvironmentVariable("CONTAINER_APP_REPLICA_NAME");

/// <summary>
/// Gets or sets the connection string for the telemetry backend for application insights.
/// </summary>
public string ApplicationInsightsConnectionString { get; set; }

/// <summary>
/// Default Sampling Ratio is 5% of successful traces gets sent
/// </summary>
public float SamplingRatio { get; set; } = 0.05F;

/// <summary>
/// Initializes a new instance of the <see cref="AltinnOpenTelemetryOptions"/> class
/// and applies additional configuration through the provided delegate.
Expand Down Expand Up @@ -68,21 +94,5 @@ public AltinnOpenTelemetryOptions ConfigureFilter(params Func<HttpContext, bool>
Filters.AddRange(configureFilter);
return this;
}

/// <summary>
/// Gets or sets the name of the service being monitored.
/// This is typically used to identify the service in OpenTelemetry traces.
/// </summary>
public string ServiceName { get; set; }

/// <summary>
/// Gets or sets the connection string for the telemetry backend for application insights.
/// </summary>
public string ApplicationInsightsConnectionString { get; set; }

/// <summary>
/// Default Sampling Ratio is 5% of successfull traces get's sent
/// </summary>
public float SamplingRatio { get; set; } = 0.05F;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public abstract class ConnectionPoolFactory : IPostgresConnectionPoolFactory
/// A semaphore to control concurrent access to the connection pool creation.
/// Ensures that only one connection pool is created at a time.
/// </summary>
protected SemaphoreSlim Semaphore { get; } = new SemaphoreSlim(1);
protected SemaphoreSlim Semaphore { get; } = new(1);

/// <inheritdoc />
public abstract Task<NpgsqlDataSource> Create(CancellationToken cancellationToken = default);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public TokenIssuer(TokenCredential credential)
/// <summary>
/// The scopes required for authenticating against the PostgreSQL server using Azure Active Directory.
/// </summary>
private static string[] TokenScopes { get; } = { "https://ossrdbms-aad.database.windows.net/.default" };
private static string[] TokenScopes { get; } = ["https://ossrdbms-aad.database.windows.net/.default"];

/// <summary>
/// Refreshes the access token asynchronously using the provided Azure credentials.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ public static IHostApplicationBuilder AddAltinnDefaultPostgresConnection(this IH
{
options.ConfigureManagedIdentity(cred =>
{
cred.TokenCredentials =
altinnAppSettings.EntraId.Identities.PostgresAdmin.TokenCredential;
cred.TokenCredentials = altinnAppSettings.EntraId.Identities.PostgresAdmin.TokenCredential;
});
}

Expand All @@ -58,7 +57,6 @@ public static IHostApplicationBuilder AddAltinnDefaultPostgresConnection(this IH
/// <returns>The modified <see cref="IHostApplicationBuilder"/> instance.</returns>
public static IHostApplicationBuilder AddAltinnPostgresConnection(this IHostApplicationBuilder builder, Action<AltinnPostgresOptions> configureOptions)
{
// Configure the services with the provided PostgreSQL options
builder.Services.Configure(configureOptions);
builder.Services.AddSingleton<IPostgresConnectionPoolFactory>(services =>
{
Expand Down

0 comments on commit 76ed499

Please sign in to comment.