Skip to content

Commit

Permalink
Update Telemetry to read MeterOptions and ActivitySourceOptions from …
Browse files Browse the repository at this point in the history
…derived classes.
  • Loading branch information
dgarciarubio committed Oct 16, 2024
1 parent 89d7326 commit 589e17a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ public static IEndpointRouteBuilder MapWeatherForecast(this IEndpointRouteBuilde
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

endpoints.MapGet("/weatherforecast", (IWeatherForecastTelemetry telemetry) =>
endpoints.MapGet("/weather-forecast", (IWeatherForecastTelemetry telemetry) =>
{
using var _ = telemetry.ActivitySource.StartActivity(name: "sample_activity", kind: ActivityKind.Internal);
telemetry.Logger.LogInformation("Requesting weather forecast");

using var activity = telemetry.ActivitySource.StartActivity(name: "weather_forecast.request", kind: ActivityKind.Internal);

var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
namespace System.Diagnostics;

/// <summary>
/// Options for creating an <see cref="ActivitySource"/>.
/// </summary>
public class ActivitySourceOptions
{
private string _name;

/// <summary>
/// The ActivitySource name.
/// </summary>
public string Name
{
get => _name;
set
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}

_name = value;
}
}

/// <summary>
/// The optional ActivitySource version.
/// </summary>
public string? Version { get; set; }

/// <summary>
/// The optional list of key-value pair tags associated with the ActivitySource.
/// </summary>
public IEnumerable<KeyValuePair<string, object?>>? Tags { get; set; }

/// <summary>
/// Constructs a new instance of <see cref="ActivitySourceOptions"/>.
/// </summary>
/// <param name="name">The ActivitySource name.</param>
public ActivitySourceOptions(string name)
{
Name = name;

Debug.Assert(_name is not null);
}
}
18 changes: 6 additions & 12 deletions AspNetCore.Examples.OpenTelemetry.TelemetryExtensions/Telemetry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,21 @@ public class Telemetry<TCategoryName> : ITelemetry<TCategoryName>
{
private bool disposedValue;

internal static readonly string CategoryName = TypeNameHelper.GetTypeDisplayName(typeof(TCategoryName), includeGenericParameters: false, nestedTypeDelimiter: '.');
public static readonly string CategoryName = TypeNameHelper.GetTypeDisplayName(typeof(TCategoryName), includeGenericParameters: false, nestedTypeDelimiter: '.');

public Telemetry(ILoggerFactory loggerFactory, IMeterFactory meterFactory)
{
Logger = loggerFactory.CreateLogger<TCategoryName>();
ActivitySource = new ActivitySource(CategoryName, Version);
Meter = meterFactory.Create(new MeterOptions(CategoryName)
{
Version = Version,
Scope = Scope,
Tags = Tags,
});
ActivitySource = new ActivitySource(ActivitySourceOptions.Name, ActivitySourceOptions.Version, ActivitySourceOptions.Tags);
Meter = meterFactory.Create(MeterOptions);
}

public ILogger<TCategoryName> Logger { get; }
public ActivitySource ActivitySource { get; }
public Meter Meter { get; }

protected virtual string? Version { get; }
protected virtual object? Scope { get; }
protected virtual IEnumerable<KeyValuePair<string, object?>>? Tags { get; }
protected virtual ActivitySourceOptions ActivitySourceOptions => new ActivitySourceOptions(CategoryName);
protected virtual MeterOptions MeterOptions => new MeterOptions(CategoryName);

protected virtual void Dispose(bool disposing)
{
Expand All @@ -48,4 +42,4 @@ public void Dispose()
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}

0 comments on commit 589e17a

Please sign in to comment.