Skip to content
Merged
Show file tree
Hide file tree
Changes from 19 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
1 change: 1 addition & 0 deletions Common.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<NuGetAudit>true</NuGetAudit>
<NuGetAuditMode>all</NuGetAuditMode>
<NuGetAuditLevel>low</NuGetAuditLevel>
<AnalysisLevel>latest-All</AnalysisLevel>
</PropertyGroup>

<!-- StyleCop -->
Expand Down
2 changes: 1 addition & 1 deletion build/_build.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework Condition=" '$(IsLegacyUbuntu)' == '' " >net10.0</TargetFramework>
<TargetFramework Condition=" '$(IsLegacyUbuntu)' != '' " >net9.0</TargetFramework>
<RootNamespace></RootNamespace>
<NoWarn>CS0649;CS0169</NoWarn>
<NoWarn>$(NoWarn);CS0649;CS0169</NoWarn>
<NukeRootDirectory>..\</NukeRootDirectory>
<NukeScriptDirectory>..\</NukeScriptDirectory>
<NukeTelemetryVersion>1</NukeTelemetryVersion>
Expand Down
10 changes: 10 additions & 0 deletions examples/demo/Service/LoggingExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

namespace Examples.Service;

internal static partial class LoggingExtensions
{
[LoggerMessage(Level = LogLevel.Information, Message = "Success! Today is: {Date:MMMM dd, yyyy}")]
public static partial void Success(this ILogger logger, DateTimeOffset date);
}
5 changes: 4 additions & 1 deletion examples/demo/Service/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Diagnostics;
using System.Diagnostics.Metrics;
using Examples.Service;
using Microsoft.Data.SqlClient;

// .NET Diagnostics: create the span factory
Expand All @@ -29,7 +30,9 @@ async Task<string> Handler(ILogger<Program> logger)
activity?.SetTag("bar", "Hello, World!");
activity?.SetTag("baz", (int[])[1, 2, 3]);

#pragma warning disable CA5394 // Do not use insecure randomness. Not related to security, just a demo.
var waitTime = Random.Shared.NextDouble(); // max 1 seconds
#pragma warning restore CA5394 // Do not use insecure randomness. Not related to security, just a demo.
await Task.Delay(TimeSpan.FromSeconds(waitTime)).ConfigureAwait(false);

activity?.SetStatus(ActivityStatusCode.Ok);
Expand All @@ -39,7 +42,7 @@ async Task<string> Handler(ILogger<Program> logger)
}

// .NET ILogger: create a log
logger.LogInformation("Success! Today is: {Date:MMMM dd, yyyy}", DateTimeOffset.UtcNow);
logger.Success(DateTimeOffset.UtcNow);

return "Hello there";
}
Expand Down
1 change: 0 additions & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
<NoWarn>$(NoWarn);NU5104</NoWarn>

<EmbedUntrackedSources Condition=" '$(TargetFramework)' == 'net462' ">true</EmbedUntrackedSources>
<AnalysisLevel>latest-All</AnalysisLevel>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@
// SPDX-License-Identifier: Apache-2.0

using System.Globalization;
#if NET
using System.Text;
#endif

namespace OpenTelemetry.AutoInstrumentation.Configurations;

internal static class ConfigurationExtensions
{
#if NET
public static IReadOnlyList<TEnum> ParseEnabledEnumList<TEnum>(this Configuration source, bool enabledByDefault, CompositeFormat enabledConfigurationTemplate)
#else
public static IReadOnlyList<TEnum> ParseEnabledEnumList<TEnum>(this Configuration source, bool enabledByDefault, string enabledConfigurationTemplate)
#endif
where TEnum : struct, Enum, IConvertible
{
#if NET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

using System.Diagnostics;
using System.Diagnostics.Metrics;
#if NET
using System.Text;
#endif
using OpenTelemetry.Logs;

namespace OpenTelemetry.AutoInstrumentation.Configurations;
Expand Down Expand Up @@ -47,12 +50,7 @@ internal partial class ConfigurationKeys
public const string ResourceDetectorEnabled = "OTEL_DOTNET_AUTO_RESOURCE_DETECTOR_ENABLED";

/// <summary>
/// Configuration key template for enabling resource detectors.
/// </summary>
public const string EnabledResourceDetectorTemplate = "OTEL_DOTNET_AUTO_{0}_RESOURCE_DETECTOR_ENABLED";

/// <summary>
/// Configuration key template for resource attributes.
/// Configuration key for resource attributes.
/// </summary>
public const string ResourceAttributes = "OTEL_RESOURCE_ATTRIBUTES";

Expand All @@ -61,6 +59,15 @@ internal partial class ConfigurationKeys
/// </summary>
public const string ServiceName = "OTEL_SERVICE_NAME";

/// <summary>
/// Configuration key template for enabling resource detectors.
/// </summary>
#if NET
public static readonly CompositeFormat EnabledResourceDetectorTemplate = CompositeFormat.Parse("OTEL_DOTNET_AUTO_{0}_RESOURCE_DETECTOR_ENABLED");
#else
public const string EnabledResourceDetectorTemplate = "OTEL_DOTNET_AUTO_{0}_RESOURCE_DETECTOR_ENABLED";
#endif

/// <summary>
/// Configuration keys for file based configuration.
/// </summary>
Expand Down Expand Up @@ -104,11 +111,6 @@ public static class Traces
/// </summary>
public const string TracesInstrumentationEnabled = "OTEL_DOTNET_AUTO_TRACES_INSTRUMENTATION_ENABLED";

/// <summary>
/// Configuration key template for disabled trace instrumentations.
/// </summary>
public const string EnabledTracesInstrumentationTemplate = "OTEL_DOTNET_AUTO_TRACES_{0}_INSTRUMENTATION_ENABLED";

/// <summary>
/// Configuration key for additional <see cref="ActivitySource"/> names to be added to the tracer at the startup.
/// </summary>
Expand All @@ -119,6 +121,15 @@ public static class Traces
/// </summary>
public const string AdditionalLegacySources = "OTEL_DOTNET_AUTO_TRACES_ADDITIONAL_LEGACY_SOURCES";

/// <summary>
/// Configuration key template for disabled trace instrumentations.
/// </summary>
#if NET
public static readonly CompositeFormat EnabledTracesInstrumentationTemplate = CompositeFormat.Parse("OTEL_DOTNET_AUTO_TRACES_{0}_INSTRUMENTATION_ENABLED");
#else
public const string EnabledTracesInstrumentationTemplate = "OTEL_DOTNET_AUTO_TRACES_{0}_INSTRUMENTATION_ENABLED";
#endif

/// <summary>
/// Configuration keys for instrumentation options.
/// </summary>
Expand Down Expand Up @@ -207,14 +218,18 @@ public static class Metrics
public const string MetricsInstrumentationEnabled = "OTEL_DOTNET_AUTO_METRICS_INSTRUMENTATION_ENABLED";

/// <summary>
/// Configuration key template for enabled metric instrumentations.
/// Configuration key for additional <see cref="Meter"/> names to be added to the meter at the startup.
/// </summary>
public const string EnabledMetricsInstrumentationTemplate = "OTEL_DOTNET_AUTO_METRICS_{0}_INSTRUMENTATION_ENABLED";
public const string AdditionalSources = "OTEL_DOTNET_AUTO_METRICS_ADDITIONAL_SOURCES";

/// <summary>
/// Configuration key for additional <see cref="Meter"/> names to be added to the meter at the startup.
/// Configuration key template for enabled metric instrumentations.
/// </summary>
public const string AdditionalSources = "OTEL_DOTNET_AUTO_METRICS_ADDITIONAL_SOURCES";
#if NET
public static readonly CompositeFormat EnabledMetricsInstrumentationTemplate = CompositeFormat.Parse("OTEL_DOTNET_AUTO_METRICS_{0}_INSTRUMENTATION_ENABLED");
#else
public const string EnabledMetricsInstrumentationTemplate = "OTEL_DOTNET_AUTO_METRICS_{0}_INSTRUMENTATION_ENABLED";
#endif
}

/// <summary>
Expand Down Expand Up @@ -259,7 +274,11 @@ public static class Logs
/// <summary>
/// Configuration key template for enabled log instrumentations.
/// </summary>
#if NET
public static readonly CompositeFormat EnabledLogsInstrumentationTemplate = CompositeFormat.Parse("OTEL_DOTNET_AUTO_LOGS_{0}_INSTRUMENTATION_ENABLED");
#else
public const string EnabledLogsInstrumentationTemplate = "OTEL_DOTNET_AUTO_LOGS_{0}_INSTRUMENTATION_ENABLED";
#endif
}

/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions test/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)test-keypair.snk</AssemblyOriginatorKeyFile>

<!-- CA1707: Identifiers should not contain underscores. It is typical pattern for test methods. -->
<NoWarn>$(NoWarn);CA1707</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand Down
36 changes: 24 additions & 12 deletions test/IntegrationTests/AspNetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ public class AspNetTests
{
private const string ServiceName = "TestApplication.AspNet.NetFramework";

private static readonly HttpClient Client = new()
{
DefaultRequestHeaders =
{
{ "Custom-Request-Test-Header1", "Test-Value1" },
{ "Custom-Request-Test-Header2", "Test-Value2" },
{ "Custom-Request-Test-Header3", "Test-Value3" }
}
};

public AspNetTests(ITestOutputHelper output)
{
Output = output;
Expand Down Expand Up @@ -72,8 +82,10 @@ public async Task SubmitsTraces(AppPoolMode appPoolMode, Gac useGac)
};
var webPort = TcpPortProvider.GetOpenPort();
var imageName = GetTestImageName(appPoolMode, useGac);
#pragma warning disable CA2007 // Do not directly await a Task. https://github.com/dotnet/roslyn-analyzers/issues/7185
await using var container = await IISContainerTestHelper.StartContainerAsync(imageName, webPort, environmentVariables, Output);
await CallTestApplicationEndpoint(webPort);
#pragma warning restore CA2007 // Do not directly await a Task. https://github.com/dotnet/roslyn-analyzers/issues/7185
await CallTestApplicationEndpoint(webPort).ConfigureAwait(true);

collector.AssertExpectations();
}
Expand Down Expand Up @@ -144,8 +156,10 @@ public async Task SubmitTracesCapturesHttpHeaders(AppPoolMode appPoolMode, Gac u
};
var webPort = TcpPortProvider.GetOpenPort();
var imageName = GetTestImageName(appPoolMode, useGac);
#pragma warning disable CA2007 // Do not directly await a Task. https://github.com/dotnet/roslyn-analyzers/issues/7185
await using var container = await IISContainerTestHelper.StartContainerAsync(imageName, webPort, environmentVariables, Output);
await CallTestApplicationEndpoint(webPort);
#pragma warning restore CA2007 // Do not directly await a Task. https://github.com/dotnet/roslyn-analyzers/issues/7185
await CallTestApplicationEndpoint(webPort).ConfigureAwait(true);

collector.AssertExpectations();
}
Expand Down Expand Up @@ -176,8 +190,10 @@ public async Task TracesResource()
};

var webPort = TcpPortProvider.GetOpenPort();
#pragma warning disable CA2007 // Do not directly await a Task. https://github.com/dotnet/roslyn-analyzers/issues/7185
await using var container = await IISContainerTestHelper.StartContainerAsync("testapplication-aspnet-netframework-integrated", webPort, environmentVariables, Output);
await CallTestApplicationEndpoint(webPort);
#pragma warning restore CA2007 // Do not directly await a Task. https://github.com/dotnet/roslyn-analyzers/issues/7185
await CallTestApplicationEndpoint(webPort).ConfigureAwait(true);

collector.ResourceExpector.AssertExpectations();
}
Expand Down Expand Up @@ -205,8 +221,10 @@ public async Task SubmitMetrics()
["OTEL_DOTNET_AUTO_METRICS_ASPNET_INSTRUMENTATION_ENABLED"] = "true" // Helps to reduce noise by enabling only AspNet metrics.
};
var webPort = TcpPortProvider.GetOpenPort();
#pragma warning disable CA2007 // Do not directly await a Task. https://github.com/dotnet/roslyn-analyzers/issues/7185
await using var container = await IISContainerTestHelper.StartContainerAsync("testapplication-aspnet-netframework-integrated", webPort, environmentVariables, Output);
await CallTestApplicationEndpoint(webPort);
#pragma warning restore CA2007 // Do not directly await a Task. https://github.com/dotnet/roslyn-analyzers/issues/7185
await CallTestApplicationEndpoint(webPort).ConfigureAwait(true);

collector.AssertExpectations();
}
Expand All @@ -225,18 +243,12 @@ private static string GetTestImageName(AppPoolMode appPoolMode, Gac useGac)

private async Task CallTestApplicationEndpoint(int webPort)
{
var client = new HttpClient();

client.DefaultRequestHeaders.Add("Custom-Request-Test-Header1", "Test-Value1");
client.DefaultRequestHeaders.Add("Custom-Request-Test-Header2", "Test-Value2");
client.DefaultRequestHeaders.Add("Custom-Request-Test-Header3", "Test-Value3");

var response = await client.GetAsync(new Uri($"http://localhost:{webPort}")).ConfigureAwait(false);
var response = await Client.GetAsync(new Uri($"http://localhost:{webPort}")).ConfigureAwait(false);
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
Output.WriteLine("MVC Response:");
Output.WriteLine(content);

response = await client.GetAsync(new Uri($"http://localhost:{webPort}/api/values")).ConfigureAwait(false);
response = await Client.GetAsync(new Uri($"http://localhost:{webPort}/api/values")).ConfigureAwait(false);
content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
Output.WriteLine("WebApi Response:");
Output.WriteLine(content);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
namespace IntegrationTests;

[CollectionDefinition(Name)]
public class AzureCollection : ICollectionFixture<AzureFixture>
public class AzureCollectionFixture : ICollectionFixture<AzureFixture>
{
public const string Name = nameof(AzureCollection);
public const string Name = nameof(AzureCollectionFixture);
}

public class AzureFixture : IAsyncLifetime
Expand Down
2 changes: 1 addition & 1 deletion test/IntegrationTests/AzureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace IntegrationTests;

[Collection(AzureCollection.Name)]
[Collection(AzureCollectionFixture.Name)]
public class AzureTests : TestHelper
{
private readonly AzureFixture _azure;
Expand Down
4 changes: 3 additions & 1 deletion test/IntegrationTests/ConsoleThreadSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

namespace IntegrationTests;

internal class ConsoleThreadSample
#pragma warning disable CA1812 // Avoid uninstantiated internal classes. This class is instantiated by deserializer.
internal sealed class ConsoleThreadSample
#pragma warning disable CA1812 // Avoid uninstantiated internal classes. This class is instantiated by deserializer.
{
public long TimestampNanoseconds { get; set; }

Expand Down
4 changes: 4 additions & 0 deletions test/IntegrationTests/ContinuousProfilerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ private static bool ContainStackTraceForClassHierarchy(Profile profile, string e

var stackTrace = string.Join("\n", frames);

#if NET
return stackTrace.Contains(expectedStackTrace, StringComparison.Ordinal);
#else
return stackTrace.Contains(expectedStackTrace);
#endif
}
}
2 changes: 1 addition & 1 deletion test/IntegrationTests/CustomSdkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace IntegrationTests;

[Collection(RedisCollection.Name)]
[Collection(RedisCollectionFixture.Name)]
public class CustomSdkTests : TestHelper
{
private readonly RedisFixture _redis;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace IntegrationTests;

[Collection(MySqlCollection.Name)]
[Collection(MySqlCollectionFixture.Name)]
public class EntityFrameworkCorePomeloMySqlTests : TestHelper
{
private readonly MySqlFixture _mySql;
Expand Down
Loading
Loading