Skip to content

Commit

Permalink
Read Azure SDK Success status (#2200)
Browse files Browse the repository at this point in the history
* Read Azure SDK Success status
  • Loading branch information
pakrym authored Jun 2, 2021
1 parent b1e5d51 commit 53a4422
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,41 @@ public void AzureClientSpansAreCollectedForHttpNotSuccessResponse()
{
module.Initialize(this.configuration);

Activity httpActivity = new Activity("Azure.SomeClient.Http.Request")
.AddTag("http.method", "PATCH")
.AddTag("http.url", "http://host/path?query#fragment")
.AddTag("otel.status_code", "ERROR");

var payload = new HttpRequestMessage();
listener.StartActivity(httpActivity, payload);
httpActivity.AddTag("http.status_code", "503");

listener.StopActivity(httpActivity, payload);

var telemetry = this.sentItems.Last() as DependencyTelemetry;

Assert.IsNotNull(telemetry);
Assert.AreEqual("PATCH /path", telemetry.Name);
Assert.AreEqual("host", telemetry.Target);
Assert.AreEqual("http://host/path?query#fragment", telemetry.Data);
Assert.AreEqual("503", telemetry.ResultCode);
Assert.AreEqual("Http", telemetry.Type);
Assert.IsFalse(telemetry.Success.Value);

Assert.IsNull(telemetry.Context.Operation.ParentId);
Assert.AreEqual(httpActivity.TraceId.ToHexString(), telemetry.Context.Operation.Id);
Assert.AreEqual(httpActivity.SpanId.ToHexString(), telemetry.Id);
}
}

[TestMethod]
public void AzureClientSpansAreCollectedForHttpNotSuccessResponseAndNoStatusCode()
{
using (var listener = new DiagnosticListener("Azure.SomeClient"))
using (var module = new DependencyTrackingTelemetryModule())
{
module.Initialize(this.configuration);

Activity httpActivity = new Activity("Azure.SomeClient.Http.Request")
.AddTag("http.method", "PATCH")
.AddTag("http.url", "http://host/path?query#fragment");
Expand All @@ -926,6 +961,30 @@ public void AzureClientSpansAreCollectedForHttpNotSuccessResponse()
}
}

[TestMethod]
public void AzureClientSpansAreCollectedAndHttpStatusCodeIsIgnoredWithExplicitStatusCode()
{
using (var listener = new DiagnosticListener("Azure.SomeClient"))
using (var module = new DependencyTrackingTelemetryModule())
{
module.Initialize(this.configuration);

Activity httpActivity = new Activity("Azure.SomeClient.Http.Request")
.AddTag("http.method", "PATCH")
.AddTag("http.url", "http://host/path?query#fragment")
.AddTag("otel.status_code", "UNSET");

var payload = new HttpRequestMessage();
listener.StartActivity(httpActivity, payload);
httpActivity.AddTag("http.status_code", "503");

listener.StopActivity(httpActivity, payload);

var telemetry = this.sentItems.Last() as DependencyTelemetry;
Assert.IsTrue(telemetry.Success.Value);
}
}

[TestMethod]
public void AzureClientSpansAreCollectedForHttpException()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ private static void SetHttpProperties(Activity activity, DependencyTelemetry dep
string method = null;
string url = null;
string status = null;
bool failed = false;
bool hasExplicitStatus = false;

foreach (var tag in activity.Tags)
{
Expand All @@ -276,6 +278,11 @@ private static void SetHttpProperties(Activity activity, DependencyTelemetry dep
{
status = tag.Value;
}
else if (tag.Key == "otel.status_code")
{
hasExplicitStatus = true;
failed = string.Equals(tag.Value, "ERROR", StringComparison.OrdinalIgnoreCase);
}
}

// TODO: could be optimized to avoid full URI parsing and allocation
Expand All @@ -290,9 +297,16 @@ private static void SetHttpProperties(Activity activity, DependencyTelemetry dep
dependency.Target = DependencyTargetNameHelper.GetDependencyTargetName(parsedUrl);
dependency.ResultCode = status;

if (int.TryParse(status, out var statusCode))
if (!hasExplicitStatus)
{
if (int.TryParse(status, out var statusCode))
{
dependency.Success = (statusCode > 0) && (statusCode < 400);
}
}
else if (failed)
{
dependency.Success = (statusCode > 0) && (statusCode < 400);
dependency.Success = false;
}
}

Expand Down

0 comments on commit 53a4422

Please sign in to comment.