Skip to content

Commit

Permalink
refactor resource attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
arealmaas committed Dec 10, 2024
1 parent 59b8c77 commit d101149
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 78 deletions.
26 changes: 1 addition & 25 deletions src/Digdir.Domain.Dialogporten.GraphQL/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,32 +74,8 @@ static void BuildAndRun(string[] args, TelemetryConfiguration telemetryConfigura
settings.Endpoint = configuration["OTEL_EXPORTER_OTLP_ENDPOINT"];
settings.Protocol = configuration["OTEL_EXPORTER_OTLP_PROTOCOL"];
settings.AppInsightsConnectionString = configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"];
settings.ResourceAttributes = configuration["OTEL_RESOURCE_ATTRIBUTES"];
settings.TraceSources.Add(DialogportenGraphQLSource);

var resourceAttributes = configuration["OTEL_RESOURCE_ATTRIBUTES"];
if (!string.IsNullOrEmpty(resourceAttributes))
{
try
{
var attributes = System.Web.HttpUtility.ParseQueryString(
resourceAttributes.Replace(',', '&')
);
foreach (string key in attributes.Keys)
{
if (!string.IsNullOrEmpty(key))
{
settings.ResourceAttributes[key] = attributes[key] ?? string.Empty;
}
}
}
catch (Exception ex)
{
throw new InvalidOperationException(
"Failed to parse OTEL_RESOURCE_ATTRIBUTES. Expected format: key1=value1,key2=value2",
ex
);
}
}
});

builder.Services
Expand Down
26 changes: 1 addition & 25 deletions src/Digdir.Domain.Dialogporten.Service/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,7 @@ static void BuildAndRun(string[] args, TelemetryConfiguration telemetryConfigura
settings.Endpoint = configuration["OTEL_EXPORTER_OTLP_ENDPOINT"];
settings.Protocol = configuration["OTEL_EXPORTER_OTLP_PROTOCOL"];
settings.AppInsightsConnectionString = configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"];

var resourceAttributes = configuration["OTEL_RESOURCE_ATTRIBUTES"];
if (!string.IsNullOrEmpty(resourceAttributes))
{
try
{
var attributes = System.Web.HttpUtility.ParseQueryString(
resourceAttributes.Replace(',', '&')
);
foreach (string key in attributes.Keys)
{
if (!string.IsNullOrEmpty(key))
{
settings.ResourceAttributes[key] = attributes[key] ?? string.Empty;
}
}
}
catch (Exception ex)
{
throw new InvalidOperationException(
"Failed to parse OTEL_RESOURCE_ATTRIBUTES. Expected format: key1=value1,key2=value2",
ex
);
}
}
settings.ResourceAttributes = configuration["OTEL_RESOURCE_ATTRIBUTES"];
});

builder.Services
Expand Down
26 changes: 1 addition & 25 deletions src/Digdir.Domain.Dialogporten.WebApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,31 +84,7 @@ static void BuildAndRun(string[] args, TelemetryConfiguration telemetryConfigura
settings.Endpoint = configuration["OTEL_EXPORTER_OTLP_ENDPOINT"];
settings.Protocol = configuration["OTEL_EXPORTER_OTLP_PROTOCOL"];
settings.AppInsightsConnectionString = configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"];

var resourceAttributes = configuration["OTEL_RESOURCE_ATTRIBUTES"];
if (!string.IsNullOrEmpty(resourceAttributes))
{
try
{
var attributes = System.Web.HttpUtility.ParseQueryString(
resourceAttributes.Replace(',', '&')
);
foreach (string key in attributes.Keys)
{
if (!string.IsNullOrEmpty(key))
{
settings.ResourceAttributes[key] = attributes[key] ?? string.Empty;
}
}
}
catch (Exception ex)
{
throw new InvalidOperationException(
"Failed to parse OTEL_RESOURCE_ATTRIBUTES. Expected format: key1=value1,key2=value2",
ex
);
}
}
settings.ResourceAttributes = configuration["OTEL_RESOURCE_ATTRIBUTES"];
});

builder.Services
Expand Down
26 changes: 23 additions & 3 deletions src/Digdir.Library.Utils.AspNet/AspNetUtilitiesExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,29 @@ public static WebApplicationBuilder ConfigureTelemetry(
{
var resourceBuilder = resource.AddService(serviceName: settings.ServiceName ?? builder.Environment.ApplicationName);

foreach (var attr in settings.ResourceAttributes)
var resourceAttributes = settings.ResourceAttributes;
if (!string.IsNullOrEmpty(resourceAttributes))
{
resourceBuilder.AddAttributes(new[] { new KeyValuePair<string, object>(attr.Key, attr.Value) });
try
{
var attributes = System.Web.HttpUtility.ParseQueryString(
resourceAttributes.Replace(',', '&')
);
foreach (string key in attributes.Keys)
{
if (!string.IsNullOrEmpty(key))
{
resourceBuilder.AddAttributes(new[] { new KeyValuePair<string, object>(key, attributes[key] ?? string.Empty) });
}
}
}
catch (Exception ex)
{
throw new InvalidOperationException(
"Failed to parse OTEL_RESOURCE_ATTRIBUTES. Expected format: key1=value1,key2=value2",
ex
);
}
}
});

Expand Down Expand Up @@ -158,7 +178,7 @@ public class TelemetrySettings
public string? Endpoint { get; set; }
public string? Protocol { get; set; }
public string? AppInsightsConnectionString { get; set; }
public Dictionary<string, string> ResourceAttributes { get; set; } = new();
public string? ResourceAttributes { get; set; }
public HashSet<string> TraceSources { get; set; } = new()
{
AzureSource,
Expand Down

0 comments on commit d101149

Please sign in to comment.