Skip to content

Commit

Permalink
Support internal and client spans from cosmos db (#2712)
Browse files Browse the repository at this point in the history
* Support internal and client spans from cosmos and suppress nested HTTP for direct mode

* Don't suppress, but support internal spans

* Rename CosmosDB EventSource following SDK changes

* update changelog
  • Loading branch information
lmolkova authored Feb 1, 2023
1 parent 943d54f commit 18be714
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## VNext
- [Upgrade System.Diagnostics.PerformanceCounter to version 6.0.0 to address CVE-2021-24112](https://github.com/microsoft/ApplicationInsights-dotnet/pull/2707)
- [Report internal dependencies from Cosmos SDK as InProc and align with Cosmos SDK EventSource changes](https://github.com/microsoft/ApplicationInsights-dotnet/pull/2712)

## Version 2.22.0-beta1
- Update endpoint redirect header name for QuickPulse module to v2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1397,6 +1397,38 @@ public void AzureCosmosDbSpansAreCollectedWithExtraAttributes()
}
}

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

Activity sendActivity = new Activity("Azure.Cosmos.ReadItems")
.AddTag("kind", "internal")
.AddTag("net.peer.name", "my.documents.azure.com")
.AddTag("db.name", "database")
.AddTag("db.operation", "ReadItems")
.AddTag("db.cosmosdb.container", "container")
.AddTag("az.namespace", "Microsoft.DocumentDB");

listener.StartActivity(sendActivity, null);
sendActivity.AddTag("db.cosmosdb.status_code", "200");
listener.StopActivity(sendActivity, null);

var telemetry = this.sentItems.Last();
DependencyTelemetry dependency = telemetry as DependencyTelemetry;
Assert.AreEqual("container | ReadItems", dependency.Name);
Assert.AreEqual("my.documents.azure.com | database", dependency.Target);
Assert.AreEqual("200", dependency.ResultCode);
Assert.AreEqual("InProc | Microsoft.DocumentDB", dependency.Type);
Assert.AreEqual("database", dependency.Properties["db.name"]);
Assert.AreEqual("ReadItems", dependency.Properties["db.operation"]);
Assert.AreEqual("my.documents.azure.com", dependency.Properties["net.peer.name"]);
}
}

#if !NET452
[TestMethod]
public void AzureCosmosDbSpansAreCollectedWithLogs()
Expand Down Expand Up @@ -1546,7 +1578,7 @@ private class ApplicationInsightsLink
class CosmosDbEventSource : EventSource
{
private CosmosDbEventSource()
: base("Azure.Cosmos_foo")
: base("Azure-Cosmos-Operation-Request-Diagnostics")
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal sealed class AzureSdkDiagnosticListenerSubscriber : DiagnosticSourceLis
public AzureSdkDiagnosticListenerSubscriber(TelemetryConfiguration configuration) : base(configuration)
{
// listen to Cosmos EventSource only - other logs can be sent using ILogger
this.logsListener = new AzureSdkEventListener(this.Client, EventLevel.Informational, "Azure.Cosmos");
this.logsListener = new AzureSdkEventListener(this.Client, EventLevel.Informational, "Azure-Cosmos-Operation-Request-Diagnostics");
this.Client.Context.GetInternalContext().SdkVersion = SdkVersionUtils.GetSdkVersion("rdd" + RddSource.DiagnosticSourceListenerAzure + ":");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ internal class AzureSdkDiagnosticsEventHandler : DiagnosticsEventHandlerBase
// Microsoft.DocumentDB is an Azure Resource Provider namespace. We use it as a dependency span as-is
// and portal will take care about visualizing it properly.
private const string CosmosDBResourceProviderNs = "Microsoft.DocumentDB";
private const string ClientCosmosDbDependencyType = CosmosDBResourceProviderNs;
private const string InternalCosmosDbDependencyType = "InProc | " + CosmosDBResourceProviderNs;
#if NET452
private static readonly DateTimeOffset EpochStart = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
#endif
Expand Down Expand Up @@ -102,8 +104,11 @@ public override void OnEvent(KeyValuePair<string, object> evnt, DiagnosticListen
dependency.SetOperationDetail(evnt.Value.GetType().FullName, evnt.Value);
}
}
else if (dependency.Type == CosmosDBResourceProviderNs)
else if (dependency.Type == ClientCosmosDbDependencyType || dependency.Type == InternalCosmosDbDependencyType)
{
// Internal cosmos spans come from SDK in Gateway mode - they are
// logical operations. AppMap then uses HTTP spans to build cosmos node and
// metrics on the edge
SetCosmosDbProperties(currentActivity, dependency);
}
}
Expand Down

0 comments on commit 18be714

Please sign in to comment.