Skip to content

Commit 8d34a55

Browse files
authored
Better mapping of cosmos telemetry (#2906)
1 parent 4341890 commit 8d34a55

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

agent/azure-monitor-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/implementation/AiSemanticAttributes.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ public final class AiSemanticAttributes {
9191
AttributeKey.stringKey("message_bus.destination");
9292
public static final AttributeKey<Long> AZURE_SDK_ENQUEUED_TIME =
9393
AttributeKey.longKey("x-opt-enqueued-time");
94+
public static final AttributeKey<String> AZURE_SDK_DB_TYPE = AttributeKey.stringKey("db.type");
95+
public static final AttributeKey<String> AZURE_SDK_DB_INSTANCE =
96+
AttributeKey.stringKey("db.instance");
97+
public static final AttributeKey<String> AZURE_SDK_DB_URL = AttributeKey.stringKey("db.url");
9498

9599
public static final AttributeKey<Long> KAFKA_RECORD_QUEUE_TIME_MS =
96100
AttributeKey.longKey("kafka.record.queue_time_ms");

agent/azure-monitor-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/implementation/SpanDataMapper.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ public final class SpanDataMapper {
5656
SemanticAttributes.DbSystemValues.HSQLDB,
5757
SemanticAttributes.DbSystemValues.H2));
5858

59+
// this is needed until Azure SDK moves to latest OTel semantic conventions
60+
private static final String COSMOS = "Cosmos";
61+
5962
private static final Mappings MAPPINGS;
6063

6164
// TODO (trask) add to generated ContextTagKeys class
@@ -164,6 +167,8 @@ private TelemetryItem exportRemoteDependency(SpanData span, boolean inProc, long
164167
telemetryBuilder.setSuccess(getSuccess(span));
165168

166169
if (inProc) {
170+
// TODO (trask) need to handle Cosmos INTERNAL spans
171+
// see https://github.com/microsoft/ApplicationInsights-Java/pull/2906/files#r1104981386
167172
telemetryBuilder.setType("InProc");
168173
} else {
169174
applySemanticConventions(telemetryBuilder, span);
@@ -220,6 +225,10 @@ private static void applySemanticConventions(
220225
return;
221226
}
222227
String dbSystem = attributes.get(SemanticAttributes.DB_SYSTEM);
228+
if (dbSystem == null) {
229+
// special case needed until Azure SDK moves to latest OTel semantic conventions
230+
dbSystem = attributes.get(AiSemanticAttributes.AZURE_SDK_DB_TYPE);
231+
}
223232
if (dbSystem != null) {
224233
applyDatabaseClientSpan(telemetryBuilder, dbSystem, attributes);
225234
return;
@@ -386,16 +395,31 @@ private static void applyDatabaseClientSpan(
386395
} else {
387396
type = "SQL";
388397
}
398+
} else if (dbSystem.equals(COSMOS)) {
399+
// this has special icon in portal (documentdb was the old name for cosmos)
400+
type = "Microsoft.DocumentDb";
389401
} else {
390402
type = dbSystem;
391403
}
392404
telemetryBuilder.setType(type);
393405
telemetryBuilder.setData(dbStatement);
394-
String target =
395-
nullAwareConcat(
396-
getTargetOrDefault(attributes, getDefaultPortForDbSystem(dbSystem), dbSystem),
397-
attributes.get(SemanticAttributes.DB_NAME),
398-
" | ");
406+
407+
String target;
408+
String dbName;
409+
if (dbSystem.equals(COSMOS)) {
410+
// special case needed until Azure SDK moves to latest OTel semantic conventions
411+
String dbUrl = attributes.get(AiSemanticAttributes.AZURE_SDK_DB_URL);
412+
if (dbUrl != null) {
413+
target = UrlParser.getTarget(dbUrl);
414+
} else {
415+
target = null;
416+
}
417+
dbName = attributes.get(AiSemanticAttributes.AZURE_SDK_DB_INSTANCE);
418+
} else {
419+
target = getTargetOrDefault(attributes, getDefaultPortForDbSystem(dbSystem), dbSystem);
420+
dbName = attributes.get(SemanticAttributes.DB_NAME);
421+
}
422+
target = nullAwareConcat(target, dbName, " | ");
399423
if (target == null) {
400424
target = dbSystem;
401425
}

0 commit comments

Comments
 (0)