Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for some AspNetCoreMetricsInstrumentationOptions #3948

Merged
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Code review changes
Temppus committed Nov 27, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit c9da7f835283d269d359b60663b83b929e19efc4
Original file line number Diff line number Diff line change
@@ -15,7 +15,6 @@
// </copyright>

using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.AspNetCore.Http;

@@ -26,6 +25,13 @@ namespace OpenTelemetry.Instrumentation.AspNetCore
/// </summary>
public class AspNetCoreMetricsInstrumentationOptions
{
/// <summary>
/// Delegate for enrichment of recorded metric with additional tags.
/// </summary>
/// <param name="context"><see cref="HttpContext"/>: the HttpContext object. Both Request and Response are available.</param>
/// <param name="tags"><see cref="TagList"/>: List of tags to add. </param>
public delegate void AspNetCoreMetricEnrichmentFunc(HttpContext context, out TagList tags);

/// <summary>
/// Gets or sets a Filter function that determines whether or not to collect telemetry about requests on a per request basis.
/// The Filter gets the HttpContext, and should return a boolean.
@@ -37,9 +43,6 @@ public class AspNetCoreMetricsInstrumentationOptions
/// <summary>
/// Gets or sets an function to enrich an recorded metric with additional custom tags.
/// </summary>
/// <remarks>
/// <para><see cref="HttpContext"/>: the HttpContext object.</para>
/// </remarks>
public Func<HttpContext, IEnumerable<KeyValuePair<string, object>>> EnrichWithCustomTags { get; set; }
public AspNetCoreMetricEnrichmentFunc EnrichWithCustomTags { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -54,9 +54,17 @@ public override void OnEventWritten(string name, object payload)
return;
}

if (this.options.Filter?.Invoke(context) == false)
try
{
AspNetCoreInstrumentationEventSource.Log.RequestIsFilteredOut(Activity.Current.OperationName);
if (this.options.Filter?.Invoke(context) == false)
{
AspNetCoreInstrumentationEventSource.Log.RequestIsFilteredOut(Activity.Current.OperationName);
return;
}
}
catch (Exception ex)
{
AspNetCoreInstrumentationEventSource.Log.RequestFilterException(ex);
return;
}

@@ -93,11 +101,18 @@ public override void OnEventWritten(string name, object payload)
#endif
if (this.options.EnrichWithCustomTags != null)
{
var enrichedTags = this.options.EnrichWithCustomTags(context);
try
{
this.options.EnrichWithCustomTags(context, out var enrichedTags);

foreach (var keyValuePair in enrichedTags)
foreach (var keyValuePair in enrichedTags)
{
tags.Add(keyValuePair);
}
}
catch (Exception ex)
{
tags.Add(keyValuePair);
AspNetCoreInstrumentationEventSource.Log.EnrichmentException(ex);
}
}

Original file line number Diff line number Diff line change
@@ -72,9 +72,7 @@ public static MeterProviderBuilder AddAspNetCoreInstrumentation(
{
var options = sp.GetRequiredService<IOptionsMonitor<AspNetCoreMetricsInstrumentationOptions>>().Get(name);

// TODO: Implement an IDeferredMeterProviderBuilder

// TODO: Add AspNetCoreMetricsInstrumentationOptions ?
// TODO: Add additional options to AspNetCoreMetricsInstrumentationOptions ?
// RecordException - probably doesn't make sense for metric instrumentation
// EnableGrpcAspNetCoreSupport - this instrumentation will also need to also handle gRPC requests

Original file line number Diff line number Diff line change
@@ -16,9 +16,11 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
@@ -141,18 +143,21 @@ void ConfigureTestServices(IServiceCollection services)
[Fact]
public async Task MetricEnrichedWithCustomTags()
{
var tagsToAdd = new List<KeyValuePair<string, object>>()
var tagsToAdd = new KeyValuePair<string, object>[]
{
new("custom_tag_1", 1),
new("custom_tag_2", "one")
new("custom_tag_2", "one"),
};

var metricItems = new List<Metric>();

void ConfigureTestServices(IServiceCollection services)
{
this.meterProvider = Sdk.CreateMeterProviderBuilder()
.AddAspNetCoreInstrumentation(opt => opt.EnrichWithCustomTags = (ctx) => tagsToAdd)
.AddAspNetCoreInstrumentation(opt => opt.EnrichWithCustomTags = (HttpContext _, out TagList tags) =>
{
tags = new TagList(new Span<KeyValuePair<string, object>>(tagsToAdd));
})
.AddInMemoryExporter(metricItems)
.Build();
}