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

Fix standard metric for updated semantic conv #37603

Merged
merged 1 commit into from
Jul 13, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,22 @@ internal static (string? MessagingUrl, string? SourceOrTarget) GetMessagingUrlAn
return null;
}

///<summary>
/// Gets dependency target from activity tag objects.
///</summary>
internal static string? GetNewSchemaDependencyTarget(this AzMonList tagObjects, OperationType type)
{
switch (type)
{
case OperationType.Http:
return tagObjects.GetNewSchemaHttpDependencyTarget();
case OperationType.Db:
return tagObjects.GetDbDependencyTargetAndName().DbTarget;
default:
return null;
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool IsDefaultPort(int port)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public override void OnEnd(Activity activity)
if (_requestDuration.Enabled)
{
activity.SetTag("_MS.ProcessedByMetricExtractors", "(Name: X,Ver:'1.1')");
ReportRequestDurationMetric(activity, SemanticConventions.AttributeHttpStatusCode);
ReportRequestDurationMetric(activity);
}
}
if (activity.Kind == ActivityKind.Client || activity.Kind == ActivityKind.Internal)
Expand All @@ -62,12 +62,12 @@ public override void OnEnd(Activity activity)
// TODO: other activity kinds
}

private void ReportRequestDurationMetric(Activity activity, string statusCodeAttribute)
private void ReportRequestDurationMetric(Activity activity)
{
string? statusCodeAttributeValue = null;
foreach (ref readonly var tag in activity.EnumerateTagObjects())
{
if (tag.Key == statusCodeAttribute)
if (tag.Key == SemanticConventions.AttributeHttpResponseStatusCode || tag.Key == SemanticConventions.AttributeHttpStatusCode)
{
statusCodeAttributeValue = tag.Value?.ToString();
break;
Expand All @@ -90,9 +90,24 @@ private void ReportDependencyDurationMetric(Activity activity)
{
var activityTagsProcessor = TraceHelper.EnumerateActivityTags(activity);

var dependencyTarget = activityTagsProcessor.MappedTags.GetDependencyTarget(activityTagsProcessor.activityType);
string? dependencyTarget;
string? statusCode;

var statusCode = AzMonList.GetTagValue(ref activityTagsProcessor.MappedTags, SemanticConventions.AttributeHttpStatusCode)?.ToString();
if (activityTagsProcessor.activityType.HasFlag(OperationType.V2))
{
// Reverting it for dependency type checks below
activityTagsProcessor.activityType &= ~OperationType.V2;

dependencyTarget = activityTagsProcessor.MappedTags.GetNewSchemaDependencyTarget(activityTagsProcessor.activityType);

statusCode = AzMonList.GetTagValue(ref activityTagsProcessor.MappedTags, SemanticConventions.AttributeHttpResponseStatusCode)?.ToString();
}
else
{
dependencyTarget = activityTagsProcessor.MappedTags.GetDependencyTarget(activityTagsProcessor.activityType);

statusCode = AzMonList.GetTagValue(ref activityTagsProcessor.MappedTags, SemanticConventions.AttributeHttpStatusCode)?.ToString();
}

var dependencyType = activityTagsProcessor.MappedTags.GetDependencyType(activityTagsProcessor.activityType);

Expand All @@ -108,6 +123,8 @@ private void ReportDependencyDurationMetric(Activity activity)

// Report metric
_dependencyDuration.Record(activity.Duration.TotalMilliseconds, tags);

activityTagsProcessor.Return();
}

protected override void Dispose(bool disposing)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,55 @@ public void ValidateRequestDurationMetric()
Assert.Equal(StandardMetricConstants.RequestDurationMetricIdValue, metricId);
}

[Fact]
public void ValidateRequestDurationMetricNew()
{
var activitySource = new ActivitySource(nameof(StandardMetricTests.ValidateRequestDurationMetric));
var traceTelemetryItems = new List<TelemetryItem>();
var metricTelemetryItems = new List<TelemetryItem>();

var standardMetricCustomProcessor = new StandardMetricsExtractionProcessor(new AzureMonitorMetricExporter(new MockTransmitter(metricTelemetryItems)));

var traceServiceName = new KeyValuePair<string, object>("service.name", "trace.service");
var resourceAttributes = new KeyValuePair<string, object>[] { traceServiceName };

using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.SetSampler(new AlwaysOnSampler())
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddAttributes(resourceAttributes))
.AddSource(nameof(StandardMetricTests.ValidateRequestDurationMetric))
.AddProcessor(standardMetricCustomProcessor)
.AddProcessor(new BatchActivityExportProcessor(new AzureMonitorTraceExporter(new MockTransmitter(traceTelemetryItems))))
.Build();

using (var activity = activitySource.StartActivity("Test", ActivityKind.Server))
{
activity?.SetTag(SemanticConventions.AttributeHttpResponseStatusCode, 200);
}

tracerProvider?.ForceFlush();

WaitForActivityExport(traceTelemetryItems);

standardMetricCustomProcessor._meterProvider?.ForceFlush();

Assert.Single(metricTelemetryItems);

var metricTelemetry = metricTelemetryItems.Last()!;
Assert.Equal("MetricData", metricTelemetry.Data.BaseType);
var metricData = (MetricsData)metricTelemetry.Data.BaseData;
Assert.True(metricData.Properties.TryGetValue(StandardMetricConstants.RequestSuccessKey, out var isSuccess));
Assert.Equal("True", isSuccess);
Assert.True(metricData.Properties.TryGetValue(StandardMetricConstants.RequestResultCodeKey, out var resultCode));
Assert.Equal("200", resultCode);
Assert.True(metricData.Properties.TryGetValue(StandardMetricConstants.IsAutoCollectedKey, out var isAutoCollectedFlag));
Assert.Equal("True", isAutoCollectedFlag);
Assert.True(metricData.Properties.TryGetValue(StandardMetricConstants.CloudRoleInstanceKey, out _));
Assert.True(metricData.Properties.TryGetValue(StandardMetricConstants.CloudRoleNameKey, out var cloudRoleName));
Assert.Equal("trace.service", cloudRoleName);
Assert.True(metricData.Properties.TryGetValue(StandardMetricConstants.MetricIdKey, out var metricId));
Assert.Equal(StandardMetricConstants.RequestDurationMetricIdValue, metricId);
}

[Fact]
public void ValidateDependencyDurationMetric()
{
Expand Down Expand Up @@ -123,6 +172,61 @@ public void ValidateDependencyDurationMetric()
Assert.Equal("www.foo.com", dependencyTarget);
}

[Fact]
public void ValidateDependencyDurationMetricNew()
{
var activitySource = new ActivitySource(nameof(StandardMetricTests.ValidateDependencyDurationMetric));
var traceTelemetryItems = new List<TelemetryItem>();
var metricTelemetryItems = new List<TelemetryItem>();

var standardMetricCustomProcessor = new StandardMetricsExtractionProcessor(new AzureMonitorMetricExporter(new MockTransmitter(metricTelemetryItems)));

var traceServiceName = new KeyValuePair<string, object>("service.name", "trace.service");
var resourceAttributes = new KeyValuePair<string, object>[] { traceServiceName };

using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.SetSampler(new AlwaysOnSampler())
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddAttributes(resourceAttributes))
.AddSource(nameof(StandardMetricTests.ValidateDependencyDurationMetric))
.AddProcessor(standardMetricCustomProcessor)
.AddProcessor(new BatchActivityExportProcessor(new AzureMonitorTraceExporter(new MockTransmitter(traceTelemetryItems))))
.Build();

using (var activity = activitySource.StartActivity("Test", ActivityKind.Client))
{
activity?.SetTag(SemanticConventions.AttributeHttpResponseStatusCode, 200);
activity?.SetTag(SemanticConventions.AttributeHttpRequestMethod, "Get");
activity?.SetTag(SemanticConventions.AttributeServerAddress, "foo.com");
}

tracerProvider?.ForceFlush();

WaitForActivityExport(traceTelemetryItems);

standardMetricCustomProcessor._meterProvider?.ForceFlush();

Assert.Single(metricTelemetryItems);

var metricTelemetry = metricTelemetryItems.Last()!;
Assert.Equal("MetricData", metricTelemetry.Data.BaseType);
var metricData = (MetricsData)metricTelemetry.Data.BaseData;
Assert.True(metricData.Properties.TryGetValue(StandardMetricConstants.DependencySuccessKey, out var isSuccess));
Assert.Equal("True", isSuccess);
Assert.True(metricData.Properties.TryGetValue(StandardMetricConstants.DependencyResultCodeKey, out var resultCode));
Assert.Equal("200", resultCode);
Assert.True(metricData.Properties.TryGetValue(StandardMetricConstants.IsAutoCollectedKey, out var isAutoCollectedFlag));
Assert.Equal("True", isAutoCollectedFlag);
Assert.True(metricData.Properties.TryGetValue(StandardMetricConstants.CloudRoleInstanceKey, out _));
Assert.True(metricData.Properties.TryGetValue(StandardMetricConstants.CloudRoleNameKey, out var cloudRoleName));
Assert.Equal("trace.service", cloudRoleName);
Assert.True(metricData.Properties.TryGetValue(StandardMetricConstants.MetricIdKey, out var metricId));
Assert.Equal(StandardMetricConstants.DependencyDurationMetricIdValue, metricId);
Assert.True(metricData.Properties.TryGetValue(StandardMetricConstants.DependencyTypeKey, out var dependencyType));
Assert.Equal("Http", dependencyType);
Assert.True(metricData.Properties.TryGetValue(StandardMetricConstants.DependencyTargetKey, out var dependencyTarget));
Assert.Equal("foo.com", dependencyTarget);
}

[Theory]
[InlineData(ActivityKind.Server)]
[InlineData(ActivityKind.Client)]
Expand Down Expand Up @@ -171,6 +275,54 @@ public void ValidateNullStatusCode(ActivityKind kind)
}
}

[Theory]
[InlineData(ActivityKind.Server)]
[InlineData(ActivityKind.Client)]
public void ValidateNullStatusCodeNew(ActivityKind kind)
{
var activitySource = new ActivitySource(nameof(StandardMetricTests.ValidateNullStatusCode));
var traceTelemetryItems = new List<TelemetryItem>();
var metricTelemetryItems = new List<TelemetryItem>();

var standardMetricCustomProcessor = new StandardMetricsExtractionProcessor(new AzureMonitorMetricExporter(new MockTransmitter(metricTelemetryItems)));

using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.SetSampler(new AlwaysOnSampler())
.AddSource(nameof(StandardMetricTests.ValidateNullStatusCode))
.AddProcessor(standardMetricCustomProcessor)
.AddProcessor(new BatchActivityExportProcessor(new AzureMonitorTraceExporter(new MockTransmitter(traceTelemetryItems))))
.Build();

using (var activity = activitySource.StartActivity("Test", kind))
{
activity?.SetTag(SemanticConventions.AttributeHttpRequestMethod, "Get");
activity?.SetTag(SemanticConventions.AttributeUrlFull, "https://www.foo.com");
}

tracerProvider?.ForceFlush();

WaitForActivityExport(traceTelemetryItems);

standardMetricCustomProcessor._meterProvider?.ForceFlush();

// Standard Metrics + Resource Metrics.
Assert.Single(metricTelemetryItems);
var metricTelemetry = metricTelemetryItems.Last()!;
Assert.Equal("MetricData", metricTelemetry.Data.BaseType);
var metricData = (MetricsData)metricTelemetry.Data.BaseData;

if (kind == ActivityKind.Client)
{
Assert.True(metricData.Properties.TryGetValue(StandardMetricConstants.DependencyResultCodeKey, out var resultCode));
Assert.Equal("0", resultCode);
}
else
{
Assert.True(metricData.Properties.TryGetValue(StandardMetricConstants.RequestResultCodeKey, out var resultCode));
Assert.Equal("0", resultCode);
}
}

private void WaitForActivityExport(List<TelemetryItem> traceTelemetryItems)
{
var result = SpinWait.SpinUntil(
Expand Down