Skip to content

Commit

Permalink
adding override to instrumentation options
Browse files Browse the repository at this point in the history
  • Loading branch information
ehornby committed Mar 14, 2023
1 parent 79d8297 commit bb7b7a0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaInstrumentationOptions
OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaInstrumentationOptions.AWSLambdaInstrumentationOptions() -> void
OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaInstrumentationOptions.DisableAwsXRayContextExtraction.get -> bool
OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaInstrumentationOptions.DisableAwsXRayContextExtraction.set -> void
OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaInstrumentationOptions.ServiceNameOverride.get -> string
OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaInstrumentationOptions.ServiceNameOverride.set -> void
OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaWrapper
OpenTelemetry.Instrumentation.AWSLambda.TracerProviderBuilderExtensions
static OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaWrapper.Trace<TInput, TResult>(OpenTelemetry.Trace.TracerProvider tracerProvider, System.Func<TInput, Amazon.Lambda.Core.ILambdaContext, TResult> lambdaHandler, TInput input, Amazon.Lambda.Core.ILambdaContext context, System.Diagnostics.ActivityContext parentContext = default(System.Diagnostics.ActivityContext)) -> TResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ public class AWSLambdaInstrumentationOptions
/// Gets or sets a value indicating whether AWS X-Ray context extraction should be disabled.
/// </summary>
public bool DisableAwsXRayContextExtraction { get; set; }

/// <summary>
/// Gets or sets a value for overriding OpenTelemetry service name if different than Lambda function name.
/// </summary>
public string ServiceNameOverride { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ public static TracerProviderBuilder AddAWSLambdaConfigurations(
builder.AddSource(AWSLambdaWrapper.ActivitySourceName);
builder.SetResourceBuilder(ResourceBuilder
.CreateEmpty()
.AddService(AWSLambdaUtils.GetFunctionName(), null, null, false)
.AddService(
string.IsNullOrWhiteSpace(options.ServiceNameOverride) ? AWSLambdaUtils.GetFunctionName() : options.ServiceNameOverride,
null,
null,
false)
.AddTelemetrySdk()
.AddAttributes(AWSLambdaResourceDetector.Detect()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,27 @@ public void OnFunctionStart_NoSampledAndAwsXRayContextExtractionDisabled_Activit
Assert.NotNull(activity);
}

[Fact]
public void AddAWSLambdaConfigurations_WithServiceNameOverride_ServiceNameSet()
{
var processor = new Mock<BaseProcessor<Activity>>();

using (var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAWSLambdaConfigurations(options =>
{
options.ServiceNameOverride = "test_override";
})
.AddProcessor(processor.Object)
.Build())
{
var result = AWSLambdaWrapper.Trace(tracerProvider, this.sampleHandlers.SampleHandlerSyncInputAndReturn, "TestStream", this.sampleLambdaContext);
var resource = tracerProvider.GetResource();
var resourceAttributes = resource.Attributes.ToDictionary(x => x.Key, x => x.Value);

Assert.Equal("test_override", resourceAttributes["service.name"]);
}
}

private static ActivityContext CreateParentContext()
{
var traceId = ActivityTraceId.CreateFromString(TraceId.AsSpan());
Expand Down

0 comments on commit bb7b7a0

Please sign in to comment.