Skip to content

Commit f19d2a5

Browse files
authored
Added Lambda Changes (#144)
1 parent 73c8137 commit f19d2a5

File tree

4 files changed

+29
-16
lines changed

4 files changed

+29
-16
lines changed

build/Build.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
internal partial class Build : NukeBuild
1414
{
15-
private const string OpenTelemetryAutoInstrumentationDefaultVersion = "v1.7.0";
15+
private const string OpenTelemetryAutoInstrumentationDefaultVersion = "v1.9.0";
1616
private static readonly AbsolutePath TestNuGetPackageApps = NukeBuild.RootDirectory / "test" / "test-applications" / "nuget-package";
1717

1818
[Solution("AWS.Distro.OpenTelemetry.AutoInstrumentation.sln")]

instrument.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ if [ "$ENABLE_PROFILING" = "true" ]; then
214214
export OTEL_TRACES_EXPORTER="none";
215215
fi
216216

217-
# TODO: need to disable all instrumentations except aws sdk and lambda.
217+
export OTEL_DOTNET_AUTO_TRACES_INSTRUMENTATION_ENABLED="false"
218+
export OTEL_DOTNET_AUTO_METRICS_INSTRUMENTATION_ENABLED="false"
218219

219220
else
220221
export OTEL_EXPORTER_OTLP_PROTOCOL="http/protobuf"

src/AWS.Distro.OpenTelemetry.AutoInstrumentation/AwsSpanProcessingUtil.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ internal sealed class AwsSpanProcessingUtil
6969
private static readonly string HttpRouteDataParsingEnabledConfig = "HTTP_ROUTE_DATA_PARSING_ENABLED";
7070
private static readonly string HttpRouteDataParsingEnabled = System.Environment.GetEnvironmentVariable(HttpRouteDataParsingEnabledConfig) ?? "false";
7171

72+
private static readonly string AwsLambdaFunctionNameConfig = "AWS_LAMBDA_FUNCTION_NAME";
73+
private static readonly string? AwsLambdaFunctionName = Environment.GetEnvironmentVariable(AwsLambdaFunctionNameConfig);
74+
7275
internal static List<string> GetDialectKeywords()
7376
{
7477
try
@@ -111,6 +114,14 @@ internal static string GetIngressOperation(Activity span)
111114
{
112115
return InternalOperation;
113116
}
117+
118+
// There is the case where we can run an ASP.NET application in a lambda environment.
119+
// In this case, we need to decide which takes precedence for the aws.local.operation
120+
// between functionName/FunctionHandler or the route template.
121+
else if (IsLambdaEnvironment())
122+
{
123+
return AwsLambdaFunctionName + "/FunctionHandler";
124+
}
114125
else if (span.GetCustomProperty("HttpContextWeakRef") != null)
115126
{
116127
return GetRouteTemplate(span);
@@ -168,7 +179,7 @@ internal static string RouteFallback(Activity span)
168179
}
169180

170181
#if NETFRAMEWORK
171-
// Uses reflection to the get the HttpRequestRouteHelper.GetRouteTemplate to get the
182+
// Uses reflection to the get the HttpRequestRouteHelper.GetRouteTemplate to get the
172183
// route template from NETFRAMEWORK applications.
173184
// https://github.com/open-telemetry/opentelemetry-dotnet-contrib/blob/main/src/OpenTelemetry.Instrumentation.AspNet/Implementation/HttpRequestRouteHelper.cs#L12
174185
internal static string? GetHttpRouteData(HttpContext httpContext)
@@ -285,6 +296,12 @@ internal static bool IsLocalRoot(Activity span)
285296
return span.Parent == null || !span.Parent.Context.IsValid() || span.HasRemoteParent;
286297
}
287298

299+
internal static bool IsLambdaEnvironment()
300+
{
301+
// detect if running in AWS Lambda environment
302+
return AwsLambdaFunctionName != null;
303+
}
304+
288305
// To identify the SQS consumer spans produced by AWS SDK instrumentation
289306
// TODO: Verify this after AWS SDK AutoInstrumentation
290307
// Can also use this instead to check the service name

src/AWS.Distro.OpenTelemetry.AutoInstrumentation/Plugin.cs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ public class Plugin
4848
private static readonly string BackupSamplerEnabledConfig = "BACKUP_SAMPLER_ENABLED";
4949
private static readonly string BackupSamplerEnabled = System.Environment.GetEnvironmentVariable(BackupSamplerEnabledConfig) ?? "true";
5050

51-
private static readonly string AwsLambdaFunctionNameConfig = "AWS_LAMBDA_FUNCTION_NAME";
52-
private static readonly string? AwsLambdaFunctionName = System.Environment.GetEnvironmentVariable(AwsLambdaFunctionNameConfig);
53-
5451
private static readonly string AwsXrayDaemonAddressConfig = "AWS_XRAY_DAEMON_ADDRESS";
5552
private static readonly string? AwsXrayDaemonAddress = System.Environment.GetEnvironmentVariable(AwsXrayDaemonAddressConfig);
5653

@@ -106,7 +103,7 @@ public void TracerProviderInitialized(TracerProvider tracerProvider)
106103

107104
// We want to be adding the exporter as the last processor in the traceProvider since processors
108105
// are executed in the order they were added to the provider.
109-
if (this.IsLambdaEnvironment() && !this.HasCustomTracesEndpoint())
106+
if (AwsSpanProcessingUtil.IsLambdaEnvironment() && !this.HasCustomTracesEndpoint())
110107
{
111108
Resource processResource = tracerProvider.GetResource();
112109

@@ -120,7 +117,7 @@ public void TracerProviderInitialized(TracerProvider tracerProvider)
120117
}
121118

122119
// Disable Application Metrics for Lambda environment
123-
if (!this.IsLambdaEnvironment())
120+
if (!AwsSpanProcessingUtil.IsLambdaEnvironment())
124121
{
125122
string? intervalConfigString = System.Environment.GetEnvironmentVariable(MetricExportIntervalConfig);
126123
int exportInterval = DefaultMetricExportInterval;
@@ -183,7 +180,6 @@ public TracerProviderBuilder BeforeConfigureTracerProvider(TracerProviderBuilder
183180
builder.AddProcessor(processor);
184181
}
185182

186-
// My custom logic here
187183
builder.AddAWSInstrumentation();
188184
#if !NETFRAMEWORK
189185
builder.AddAWSLambdaConfigurations();
@@ -260,6 +256,11 @@ public void ConfigureTracesOptions(HttpClientTraceInstrumentationOptions options
260256
return false;
261257
}
262258

259+
if (request.RequestUri?.AbsolutePath.Contains("/runtime/invocation/") == true)
260+
{
261+
return false;
262+
}
263+
263264
return true;
264265
};
265266

@@ -409,7 +410,7 @@ private ResourceBuilder ResourceBuilderCustomizer(ResourceBuilder builder)
409410

410411
// Resource detectors are disabled if the environment variable is explicitly set to false or if the
411412
// application is in a lambda environment
412-
if (resourceDetectorsEnabled != "true" || this.IsLambdaEnvironment())
413+
if (resourceDetectorsEnabled != "true" || AwsSpanProcessingUtil.IsLambdaEnvironment())
413414
{
414415
return builder;
415416
}
@@ -460,12 +461,6 @@ private OtlpMetricExporter ApplicationSignalsExporterProvider()
460461
return new OtlpMetricExporter(options);
461462
}
462463

463-
private bool IsLambdaEnvironment()
464-
{
465-
// detect if running in AWS Lambda environment
466-
return AwsLambdaFunctionName != null;
467-
}
468-
469464
private bool HasCustomTracesEndpoint()
470465
{
471466
// detect if running in AWS Lambda environment

0 commit comments

Comments
 (0)