Skip to content

Commit

Permalink
Add db.system attribute to the AWS SDK instrumentation. (#418)
Browse files Browse the repository at this point in the history
* Added db.system attribute to the AWS SDK instrumentation (#415)

* Aligned code formatting (#415)

* - set dynamo db related tag in appropriate method (#415)

Co-authored-by: Cijo Thomas <cithomas@microsoft.com>
Co-authored-by: Prashant Srivastava <50466688+srprash@users.noreply.github.com>
  • Loading branch information
3 people committed Jun 30, 2022
1 parent 0edc79f commit c55fd8d
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@ internal static class AWSSemanticConventions

public const string AttributeHttpStatusCode = "http.status_code";
public const string AttributeHttpResponseContentLength = "http.response_content_length";

public const string AttributeValueDynamoDb = "dynamodb";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// <copyright file="AWSServiceHelper.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System;
using System.Collections.Generic;
using Amazon.Runtime;

namespace OpenTelemetry.Contrib.Instrumentation.AWS.Implementation
{
internal class AWSServiceHelper
{
internal static IReadOnlyDictionary<string, string> ServiceParameterMap = new Dictionary<string, string>()
{
{ DynamoDbService, "TableName" },
{ SQSService, "QueueUrl" },
};

internal static IReadOnlyDictionary<string, string> ParameterAttributeMap = new Dictionary<string, string>()
{
{ "TableName", AWSSemanticConventions.AttributeAWSDynamoTableName },
{ "QueueUrl", AWSSemanticConventions.AttributeAWSSQSQueueUrl },
};

private const string DynamoDbService = "DynamoDBv2";
private const string SQSService = "SQS";

internal static string GetAWSServiceName(IRequestContext requestContext)
=> Utils.RemoveAmazonPrefixFromServiceName(requestContext.Request.ServiceName);

internal static string GetAWSOperationName(IRequestContext requestContext)
{
string completeRequestName = requestContext.OriginalRequest.GetType().Name;
string suffix = "Request";
var operationName = Utils.RemoveSuffix(completeRequestName, suffix);
return operationName;
}

internal static bool IsDynamoDbService(string service)
=> DynamoDbService.Equals(service, StringComparison.OrdinalIgnoreCase);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,6 @@ internal class AWSTracingPipelineHandler : PipelineHandler
carrier[name] = value;
};

private static readonly Dictionary<string, string> ServiceParameterMap = new Dictionary<string, string>()
{
{ "DynamoDBv2", "TableName" },
{ "SQS", "QueueUrl" },
};

private static readonly Dictionary<string, string> ParameterAttributeMap = new Dictionary<string, string>()
{
{ "TableName", AWSSemanticConventions.AttributeAWSDynamoTableName },
{ "QueueUrl", AWSSemanticConventions.AttributeAWSSQSQueueUrl },
};

private static readonly ActivitySource AWSSDKActivitySource = new ActivitySource(ActivitySourceName);

private readonly AWSClientInstrumentationOptions options;
Expand Down Expand Up @@ -117,8 +105,8 @@ private Activity ProcessBeginRequest(IExecutionContext executionContext)
Activity activity = null;

var requestContext = executionContext.RequestContext;
var service = this.GetAWSServiceName(requestContext);
var operation = this.GetAWSOperationName(requestContext);
var service = AWSServiceHelper.GetAWSServiceName(requestContext);
var operation = AWSServiceHelper.GetAWSOperationName(requestContext);

activity = AWSSDKActivitySource.StartActivity(service + "." + operation, ActivityKind.Client);

Expand Down Expand Up @@ -192,37 +180,26 @@ private void ProcessException(Activity activity, Exception ex)
}
}

private string GetAWSServiceName(IRequestContext requestContext)
{
string serviceName = string.Empty;
serviceName = Utils.RemoveAmazonPrefixFromServiceName(requestContext.Request.ServiceName);
return serviceName;
}

private string GetAWSOperationName(IRequestContext requestContext)
{
string operationName = string.Empty;
string completeRequestName = requestContext.OriginalRequest.GetType().Name;
string suffix = "Request";
operationName = Utils.RemoveSuffix(completeRequestName, suffix);
return operationName;
}

private void AddRequestSpecificInformation(Activity activity, IRequestContext requestContext, string service)
{
AmazonWebServiceRequest request = requestContext.OriginalRequest;

if (ServiceParameterMap.TryGetValue(service, out string parameter))
if (AWSServiceHelper.ServiceParameterMap.TryGetValue(service, out string parameter))
{
AmazonWebServiceRequest request = requestContext.OriginalRequest;

var property = request.GetType().GetProperty(parameter);
if (property != null)
{
if (ParameterAttributeMap.TryGetValue(parameter, out string attribute))
if (AWSServiceHelper.ParameterAttributeMap.TryGetValue(parameter, out string attribute))
{
activity.SetTag(attribute, property.GetValue(request));
}
}
}

if (AWSServiceHelper.IsDynamoDbService(service))
{
activity.SetTag(SemanticConventions.AttributeDbSystem, AWSSemanticConventions.AttributeValueDynamoDb);
}
}

private void AddStatusCodeToActivity(Activity activity, int status_code)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net452;netstandard2.0</TargetFrameworks>
Expand All @@ -13,5 +13,6 @@

<ItemGroup>
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Internal\Guard.cs" Link="Includes\Guard.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Contrib.Shared\Api\SemanticConventions.cs" Link="Includes\SemanticConventions.cs"/>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ private void ValidateDynamoActivityTags(Activity ddb_activity)
Assert.Equal("Scan", Utils.GetTagValue(ddb_activity, "aws.operation"));
Assert.Equal("us-east-1", Utils.GetTagValue(ddb_activity, "aws.region"));
Assert.Equal("SampleProduct", Utils.GetTagValue(ddb_activity, "aws.table_name"));
Assert.Equal("dynamodb", Utils.GetTagValue(ddb_activity, "db.system"));
}

private void ValidateSqsActivityTags(Activity sqs_activity)
Expand Down

0 comments on commit c55fd8d

Please sign in to comment.