Skip to content

Commit 056f406

Browse files
authored
chore(hybrid-agent): DynamoDB datastore segment data support (#3332)
1 parent dd90243 commit 056f406

File tree

2 files changed

+131
-1
lines changed

2 files changed

+131
-1
lines changed

src/Agent/NewRelic/Agent/Core/OpenTelemetryBridge/ActivityBridgeSegmentHelpers.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using NewRelic.Agent.Extensions.Parsing;
1818
using NewRelic.Agent.Extensions.Providers.Wrapper;
1919
using NewRelic.Agent.Extensions.SystemExtensions;
20+
using static Grpc.Core.ServerServiceDefinition;
2021

2122
namespace NewRelic.Agent.Core.OpenTelemetryBridge;
2223

@@ -474,7 +475,8 @@ private static void ProcessClientDatabaseTags(ISegment segment, IAgent agent, dy
474475
"redis" => DatastoreVendor.Redis,
475476
"azure.cosmosdb" => DatastoreVendor.CosmosDB,
476477
"elasticsearch" => DatastoreVendor.Elasticsearch,
477-
"aws.dynamodb" => DatastoreVendor.DynamoDB,
478+
"dynamodb" => DatastoreVendor.DynamoDB, // OpenTelemetry.Instrumentation.AWS produces "dynamodb" for db.system
479+
"aws.dynamodb" => DatastoreVendor.DynamoDB, // the otel spec says "aws.dynamodb" is the correct value for DynamoDB
478480
_ => DatastoreVendor.Other
479481
};
480482

@@ -484,6 +486,7 @@ private static void ProcessClientDatabaseTags(ISegment segment, IAgent agent, dy
484486
ISegmentData segmentData = vendor switch
485487
{
486488
DatastoreVendor.Elasticsearch => GetElasticSearchDatastoreSegmentData(agent, tags, vendor, activityLogPrefix),
489+
DatastoreVendor.DynamoDB => GetDynamoDbDatastoreSegmentData(agent, activity, activityLogPrefix, tags, segment),
487490
_ => GetDefaultDatastoreSegmentData(agent, activity, activityLogPrefix, tags, vendor)
488491
};
489492

@@ -568,6 +571,40 @@ private static ISegmentData GetElasticSearchDatastoreSegmentData(IAgent agent, D
568571
return new DatastoreSegmentData(agent.GetExperimentalApi().DatabaseService, parsedSqlStatement, string.Empty, connectionInfo);
569572
}
570573

574+
private static ISegmentData GetDynamoDbDatastoreSegmentData(IAgent agent, dynamic activity, string activityLogPrefix, Dictionary<string, object> tags, ISegment segment)
575+
{
576+
tags.TryGetAndRemoveTag<string>(["aws.dynamodb.table_names"], out var tableNames);
577+
if (string.IsNullOrEmpty(tableNames))
578+
{
579+
Log.Finest($"DynamoDB {activityLogPrefix} is missing required tag for table names. Not creating a DatastoreSegmentData.");
580+
return null;
581+
}
582+
583+
// DynamoDB operation is in rpc.method
584+
tags.TryGetAndRemoveTag<string>(["rpc.method"], out var dbOperation);
585+
if (string.IsNullOrEmpty(dbOperation))
586+
{
587+
Log.Finest($"DynamoDB {activityLogPrefix} is missing required tag for operation. Not creating a DatastoreSegmentData.");
588+
return null;
589+
}
590+
591+
// Add DynamoDB specific attributes to the segment
592+
segment.AddCloudSdkAttribute("aws.operation", dbOperation);
593+
594+
// spec says region is in cloud.region but it's not there currently
595+
if (tags.TryGetAndRemoveTag<string>(["cloud.region"], out var awsRegion))
596+
segment.AddCloudSdkAttribute("aws.region", awsRegion);
597+
598+
// requestId is in aws.request_id
599+
if (tags.TryGetAndRemoveTag<string>(["aws.request_id"], out var requestId))
600+
segment.AddCloudSdkAttribute("aws.requestId", requestId);
601+
602+
var parsedSqlStatement = new ParsedSqlStatement(DatastoreVendor.DynamoDB, tableNames, dbOperation);
603+
604+
Log.Finest($"Created DatastoreSegmentData for DynamoDB {activityLogPrefix}");
605+
return new DatastoreSegmentData(agent.GetExperimentalApi().DatabaseService, parsedSqlStatement, string.Empty, null);
606+
}
607+
571608
public static void AddExceptionEventInformationToSegment(this ISegment segment, object originalActivity, IErrorService errorService)
572609
{
573610
// Exceptions recorded during an activity may be added as events on the activity. Not every way of recording

tests/Agent/UnitTests/CompositeTests/HybridAgent/TestCaseDefinitions/HybridAgentTests.json

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2516,5 +2516,98 @@
25162516
}
25172517
]
25182518
}
2519+
},
2520+
{
2521+
"testDescription": "Client Database - AWS DynamoDB PutItem",
2522+
"operations": [
2523+
{
2524+
"command": "DoWorkInTransaction",
2525+
"parameters": {
2526+
"transactionName": "Foo"
2527+
},
2528+
"childOperations": [
2529+
{
2530+
"command": "DoWorkInSpan",
2531+
"parameters": {
2532+
"spanName": "DynamoDB.PutItem",
2533+
"spanKind": "Client"
2534+
},
2535+
"childOperations": [
2536+
{
2537+
"command": "AddOTelAttribute",
2538+
"parameters": {
2539+
"name": "rpc.method",
2540+
"value": "PutItem"
2541+
}
2542+
},
2543+
{
2544+
"command": "AddOTelAttribute",
2545+
"parameters": {
2546+
"name": "rpc.system",
2547+
"value": "aws-api"
2548+
}
2549+
},
2550+
{
2551+
"command": "AddOTelAttribute",
2552+
"parameters": {
2553+
"name": "rpc.service",
2554+
"value": "DynamoDB"
2555+
}
2556+
},
2557+
{
2558+
"command": "AddOTelAttribute",
2559+
"parameters": {
2560+
"name": "db.system",
2561+
"value": "dynamodb"
2562+
}
2563+
},
2564+
{
2565+
"command": "AddOTelAttribute",
2566+
"parameters": {
2567+
"name": "aws.dynamodb.table_names",
2568+
"value": "Movies"
2569+
}
2570+
},
2571+
{
2572+
"command": "AddOTelAttribute",
2573+
"parameters": {
2574+
"name": "aws.request_id",
2575+
"value": "f9926109-56fc-412f-a3b2-d27742d5f5c1"
2576+
}
2577+
},
2578+
{
2579+
"command": "AddOTelAttribute",
2580+
"parameters": {
2581+
"name": "cloud.region",
2582+
"value": "us-east-1"
2583+
}
2584+
}
2585+
]
2586+
}
2587+
]
2588+
}
2589+
],
2590+
"agentOutput": {
2591+
"transactions": [
2592+
{
2593+
"name": "Foo"
2594+
}
2595+
],
2596+
"spans": [
2597+
{
2598+
"name": "Datastore/statement/DynamoDB/Movies/PutItem",
2599+
"parentName": "Foo",
2600+
"category": "datastore",
2601+
"attributes": {
2602+
"db.collection": "Movies",
2603+
"db.operation": "PutItem",
2604+
"db.system": "dynamodb",
2605+
"aws.operation": "PutItem",
2606+
"aws.requestId": "f9926109-56fc-412f-a3b2-d27742d5f5c1",
2607+
"aws.region": "us-east-1"
2608+
}
2609+
}
2610+
]
2611+
}
25192612
}
25202613
]

0 commit comments

Comments
 (0)