Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion opentelemetry-dotnet-contrib.sln
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{824BD1DE
build\Common.props = build\Common.props
build\Common.targets = build\Common.targets
build\debug.snk = build\debug.snk
build\docker-compose.net10.0.yml = build\docker-compose.net10.0.yml
build\docker-compose.net8.0.yml = build\docker-compose.net8.0.yml
build\docker-compose.net9.0.yml = build\docker-compose.net9.0.yml
build\docker-compose.net10.0.yml = build\docker-compose.net10.0.yml
build\opentelemetry-icon-color.png = build\opentelemetry-icon-color.png
build\OpenTelemetryContrib.prod.ruleset = build\OpenTelemetryContrib.prod.ruleset
build\OpenTelemetryContrib.test.ruleset = build\OpenTelemetryContrib.test.ruleset
Expand Down Expand Up @@ -367,6 +367,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Shared", "Shared", "{70CA77
test\Shared\ConsoleCommand.cs = test\Shared\ConsoleCommand.cs
test\Shared\CustomTextMapPropagator.cs = test\Shared\CustomTextMapPropagator.cs
test\Shared\EnabledOnDockerPlatformTheoryAttribute.cs = test\Shared\EnabledOnDockerPlatformTheoryAttribute.cs
test\Shared\EnvironmentVariableScope.cs = test\Shared\EnvironmentVariableScope.cs
test\Shared\EventSourceTestHelper.cs = test\Shared\EventSourceTestHelper.cs
test\Shared\InMemoryEventListener.cs = test\Shared\InMemoryEventListener.cs
test\Shared\PerfTracepointListener.cs = test\Shared\PerfTracepointListener.cs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,15 @@ public void VerifyFlags()
[MemberData(nameof(TestCases))]
public void VerifyGetSemanticConventionOptIn_UsingEnvironmentVariable(string input, string expectedValue)
{
try
using (EnvironmentVariableScope.Create(SemanticConventionOptInKeyName, input))
{
Environment.SetEnvironmentVariable(SemanticConventionOptInKeyName, input);

#if NET
var expected = Enum.Parse<DatabaseSemanticConvention>(expectedValue);
#else
var expected = Enum.Parse(typeof(DatabaseSemanticConvention), expectedValue);
#endif
Assert.Equal(expected, GetSemanticConventionOptIn(new ConfigurationBuilder().AddEnvironmentVariables().Build()));
}
finally
{
Environment.SetEnvironmentVariable(SemanticConventionOptInKeyName, null);
}
}

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<Compile Include="$(RepoRoot)\src\Shared\SqlParameterProcessor.cs" Link="Includes\SqlParameterProcessor.cs" />
<Compile Include="$(RepoRoot)\src\Shared\SqlProcessor.cs" Link="Includes\SqlProcessor.cs" />
<Compile Include="$(RepoRoot)\src\Shared\SqlStatementInfo.cs" Link="Includes\SqlStatementInfo.cs" />
<Compile Include="$(RepoRoot)\test\Shared\EnvironmentVariableScope.cs" Link="Includes\EnvironmentVariableScope.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
28 changes: 13 additions & 15 deletions test/OpenTelemetry.Contrib.Shared.Tests/RequestDataHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace OpenTelemetry.Internal.Tests;

public class RequestDataHelperTests : IDisposable
public class RequestDataHelperTests
{
public static IEnumerable<object[]> MappingVersionProtocolToVersionData =>
[
Expand Down Expand Up @@ -50,19 +50,23 @@ public void MethodMappingWorksForKnownMethods(string method, string expected)
[InlineData("invalid", "_OTHER")]
public void MethodMappingWorksForEnvironmentVariables(string method, string expected)
{
Environment.SetEnvironmentVariable("OTEL_INSTRUMENTATION_HTTP_KNOWN_METHODS", "GET,POST");
var requestHelper = new RequestDataHelper(configureByHttpKnownMethodsEnvironmentalVariable: true);
var actual = requestHelper.GetNormalizedHttpMethod(method);
Assert.Equal(expected, actual);
using (EnvironmentVariableScope.Create("OTEL_INSTRUMENTATION_HTTP_KNOWN_METHODS", "GET,POST"))
{
var requestHelper = new RequestDataHelper(configureByHttpKnownMethodsEnvironmentalVariable: true);
var actual = requestHelper.GetNormalizedHttpMethod(method);
Assert.Equal(expected, actual);
}
}

[Fact]
public void MethodMappingWorksIfEnvironmentalVariableConfigurationIsDisabled()
{
Environment.SetEnvironmentVariable("OTEL_INSTRUMENTATION_HTTP_KNOWN_METHODS", "GET,POST");
var requestHelper = new RequestDataHelper(configureByHttpKnownMethodsEnvironmentalVariable: false);
var actual = requestHelper.GetNormalizedHttpMethod("CONNECT");
Assert.Equal("CONNECT", actual);
using (EnvironmentVariableScope.Create("OTEL_INSTRUMENTATION_HTTP_KNOWN_METHODS", "GET,POST"))
{
var requestHelper = new RequestDataHelper(configureByHttpKnownMethodsEnvironmentalVariable: false);
var actual = requestHelper.GetNormalizedHttpMethod("CONNECT");
Assert.Equal("CONNECT", actual);
}
}

[Theory]
Expand All @@ -83,10 +87,4 @@ public void MappingVersionProtocolToVersion(Version protocolVersion, string expe
var actual = RequestDataHelper.GetHttpProtocolVersion(protocolVersion);
Assert.Equal(expected, actual);
}

public void Dispose()
{
// Clean up after tests that set environment variables.
Environment.SetEnvironmentVariable("OTEL_INSTRUMENTATION_HTTP_KNOWN_METHODS", null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,25 @@ public class AWSLambdaWrapperTests : IDisposable

private readonly SampleHandlers sampleHandlers;
private readonly SampleLambdaContext sampleLambdaContext;
private readonly IDisposable environmentScope;

public AWSLambdaWrapperTests()
{
this.sampleHandlers = new SampleHandlers();
this.sampleLambdaContext = new SampleLambdaContext();
Environment.SetEnvironmentVariable("_X_AMZN_TRACE_ID", $"Root=1-5759e988-bd862e3fe1be46a994272793;Parent={XRayParentId};Sampled=1");
Environment.SetEnvironmentVariable("AWS_REGION", "us-east-1");
Environment.SetEnvironmentVariable("AWS_LAMBDA_FUNCTION_NAME", "testfunction");
Environment.SetEnvironmentVariable("AWS_LAMBDA_FUNCTION_VERSION", "latest");
Environment.SetEnvironmentVariable("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "128");
Environment.SetEnvironmentVariable("AWS_LAMBDA_LOG_STREAM_NAME", "2025/07/21/[$LATEST]7b176c212e954e62adfb9b5451cb5374");
this.environmentScope = EnvironmentVariableScope.Create(
("_X_AMZN_TRACE_ID", $"Root=1-5759e988-bd862e3fe1be46a994272793;Parent={XRayParentId};Sampled=1"),
("AWS_REGION", "us-east-1"),
("AWS_LAMBDA_FUNCTION_NAME", "testfunction"),
("AWS_LAMBDA_FUNCTION_VERSION", "latest"),
("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "128"),
("AWS_LAMBDA_LOG_STREAM_NAME", "2025/07/21/[$LATEST]7b176c212e954e62adfb9b5451cb5374"));
}

public void Dispose()
{
this.environmentScope.Dispose();

// reset Semantic Convention to default
Sdk.CreateTracerProviderBuilder()
.AddAWSLambdaConfigurations();
Expand Down Expand Up @@ -187,59 +191,63 @@ public void TestLambdaHandlerException(bool setCustomParent)
[Fact]
public void TestLambdaHandlerNotSampled()
{
Environment.SetEnvironmentVariable("_X_AMZN_TRACE_ID", "Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=0");
using (EnvironmentVariableScope.Create("_X_AMZN_TRACE_ID", "Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=0"))
{
var exportedItems = new List<Activity>();

var exportedItems = new List<Activity>();
using (var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAWSLambdaConfigurations(opt =>
{
opt.SemanticConventionVersion = SemanticConventionVersion.Latest;
})
.AddInMemoryExporter(exportedItems)
.Build()!)
{
var result = AWSLambdaWrapper.Trace(tracerProvider, this.sampleHandlers.SampleHandlerSyncInputAndReturn, "TestStream", this.sampleLambdaContext);
var resource = tracerProvider.GetResource();
this.AssertResourceAttributes(resource);
}

using (var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAWSLambdaConfigurations(opt =>
{
opt.SemanticConventionVersion = SemanticConventionVersion.Latest;
})
.AddInMemoryExporter(exportedItems)
.Build()!)
{
var result = AWSLambdaWrapper.Trace(tracerProvider, this.sampleHandlers.SampleHandlerSyncInputAndReturn, "TestStream", this.sampleLambdaContext);
var resource = tracerProvider.GetResource();
this.AssertResourceAttributes(resource);
Assert.Empty(exportedItems);
}

Assert.Empty(exportedItems);
}

[Fact]
public void OnFunctionStart_NoParent_ActivityCreated()
{
Environment.SetEnvironmentVariable("_X_AMZN_TRACE_ID", null);

Activity? activity = null;
using (var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAWSLambdaConfigurations(opt =>
{
opt.SemanticConventionVersion = SemanticConventionVersion.Latest;
})
.Build())
using (EnvironmentVariableScope.Create("_X_AMZN_TRACE_ID", null))
{
activity = AWSLambdaWrapper.OnFunctionStart("test-input", new SampleLambdaContext());
}
Activity? activity = null;
using (var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAWSLambdaConfigurations(opt =>
{
opt.SemanticConventionVersion = SemanticConventionVersion.Latest;
})
.Build())
{
activity = AWSLambdaWrapper.OnFunctionStart("test-input", new SampleLambdaContext());
}

Assert.NotNull(activity);
Assert.NotNull(activity);
}
}

[Fact]
public void OnFunctionStart_NoSampledAndAwsXRayContextExtractionDisabled_ActivityCreated()
{
Environment.SetEnvironmentVariable("_X_AMZN_TRACE_ID", $"Root=1-5759e988-bd862e3fe1be46a994272793;Parent={XRayParentId};Sampled=0");
Activity? activity = null;

using (var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAWSLambdaConfigurations(c => c.DisableAwsXRayContextExtraction = true)
.Build())
using (EnvironmentVariableScope.Create("_X_AMZN_TRACE_ID", $"Root=1-5759e988-bd862e3fe1be46a994272793;Parent={XRayParentId};Sampled=0"))
{
activity = AWSLambdaWrapper.OnFunctionStart("test-input", new SampleLambdaContext());
}
Activity? activity = null;

Assert.NotNull(activity);
using (var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAWSLambdaConfigurations(c => c.DisableAwsXRayContextExtraction = true)
.Build())
{
activity = AWSLambdaWrapper.OnFunctionStart("test-input", new SampleLambdaContext());
}

Assert.NotNull(activity);
}
}

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

<ItemGroup>
<Compile Include="$(RepoRoot)\src\Shared\ActivityHelperExtensions.cs" Link="Includes\ActivityHelperExtensions.cs" />
<Compile Include="$(RepoRoot)\test\Shared\EnvironmentVariableScope.cs" Link="Includes\EnvironmentVariableScope.cs" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,8 @@ public void AspNetRequestsAreCollectedSuccessfully(
bool recordException = false,
string? expectedErrorType = null)
{
try
using (EnvironmentVariableScope.Create("OTEL_DOTNET_EXPERIMENTAL_ASPNET_DISABLE_URL_QUERY_REDACTION", disableQueryRedaction == QueryRedactionDisableBehavior.DisableViaEnvVar ? "true" : null))
{
if (disableQueryRedaction == QueryRedactionDisableBehavior.DisableViaEnvVar)
{
Environment.SetEnvironmentVariable("OTEL_DOTNET_EXPERIMENTAL_ASPNET_DISABLE_URL_QUERY_REDACTION", "true");
}

HttpContext.Current = RouteTestHelper.BuildHttpContext(url, routeType, routeTemplate, requestMethod);

typeof(HttpRequest).GetField("_wr", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(HttpContext.Current.Request, new TestHttpWorkerRequest());
Expand Down Expand Up @@ -228,10 +223,6 @@ public void AspNetRequestsAreCollectedSuccessfully(
Assert.True(string.IsNullOrEmpty(span.StatusDescription));
}
}
finally
{
Environment.SetEnvironmentVariable("OTEL_DOTNET_EXPERIMENTAL_ASPNET_DISABLE_URL_QUERY_REDACTION", null);
}
}

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

<ItemGroup>
<Compile Include="$(RepoRoot)\src\Shared\ActivityHelperExtensions.cs" Link="Includes\ActivityHelperExtensions.cs" />
<Compile Include="$(RepoRoot)\test\Shared\EnvironmentVariableScope.cs" Link="Includes\EnvironmentVariableScope.cs" />
<Compile Include="$(RepoRoot)\test\Shared\EventSourceTestHelper.cs" Link="Includes\EventSourceTestHelper.cs" />
<Compile Include="$(RepoRoot)\test\Shared\TestActivityProcessor.cs" Link="Includes\TestActivityProcessor.cs" />
<Compile Include="$(RepoRoot)\test\Shared\TestEventListener.cs" Link="Includes\TestEventListener.cs" />
Expand Down
Loading
Loading