From e74e0ed5dd2ae2fd5f99ecdf04c1b28341674121 Mon Sep 17 00:00:00 2001 From: Mikel Blanchard Date: Tue, 6 Jun 2023 13:58:09 -0700 Subject: [PATCH 1/2] [console-exporter] Add extension on LoggerProviderBuilder (#4549) --- .../AssemblyInfo.cs | 1 + src/OpenTelemetry.Api/AssemblyInfo.cs | 1 + .../ConsoleExporterLoggingExtensions.cs | 53 +++++++++++++++++++ .../OpenTelemetry.Exporter.Console.csproj | 12 +++-- src/OpenTelemetry/AssemblyInfo.cs | 1 + 5 files changed, 64 insertions(+), 4 deletions(-) diff --git a/src/OpenTelemetry.Api.ProviderBuilderExtensions/AssemblyInfo.cs b/src/OpenTelemetry.Api.ProviderBuilderExtensions/AssemblyInfo.cs index c8c47e0bd17..dce5df270da 100644 --- a/src/OpenTelemetry.Api.ProviderBuilderExtensions/AssemblyInfo.cs +++ b/src/OpenTelemetry.Api.ProviderBuilderExtensions/AssemblyInfo.cs @@ -17,6 +17,7 @@ using System.Runtime.CompilerServices; [assembly: InternalsVisibleTo("OpenTelemetry" + AssemblyInfo.PublicKey)] +[assembly: InternalsVisibleTo("OpenTelemetry.Exporter.Console" + AssemblyInfo.PublicKey)] [assembly: InternalsVisibleTo("OpenTelemetry.Extensions.Hosting" + AssemblyInfo.PublicKey)] [assembly: InternalsVisibleTo("OpenTelemetry.Tests" + AssemblyInfo.PublicKey)] [assembly: InternalsVisibleTo("OpenTelemetry.Api.ProviderBuilderExtensions.Tests" + AssemblyInfo.PublicKey)] diff --git a/src/OpenTelemetry.Api/AssemblyInfo.cs b/src/OpenTelemetry.Api/AssemblyInfo.cs index 3d46ffb478d..1d0d887c5cd 100644 --- a/src/OpenTelemetry.Api/AssemblyInfo.cs +++ b/src/OpenTelemetry.Api/AssemblyInfo.cs @@ -20,6 +20,7 @@ [assembly: InternalsVisibleTo("OpenTelemetry.Api.ProviderBuilderExtensions" + AssemblyInfo.PublicKey)] [assembly: InternalsVisibleTo("OpenTelemetry.Api.ProviderBuilderExtensions.Tests" + AssemblyInfo.PublicKey)] [assembly: InternalsVisibleTo("OpenTelemetry.Api.Tests" + AssemblyInfo.PublicKey)] +[assembly: InternalsVisibleTo("OpenTelemetry.Exporter.Console" + AssemblyInfo.PublicKey)] [assembly: InternalsVisibleTo("OpenTelemetry.Exporter.InMemory" + AssemblyInfo.PublicKey)] [assembly: InternalsVisibleTo("OpenTelemetry.Extensions.Hosting" + AssemblyInfo.PublicKey)] [assembly: InternalsVisibleTo("OpenTelemetry.Extensions.Hosting.Tests" + AssemblyInfo.PublicKey)] diff --git a/src/OpenTelemetry.Exporter.Console/ConsoleExporterLoggingExtensions.cs b/src/OpenTelemetry.Exporter.Console/ConsoleExporterLoggingExtensions.cs index 50b81a54916..8a2c8af6e41 100644 --- a/src/OpenTelemetry.Exporter.Console/ConsoleExporterLoggingExtensions.cs +++ b/src/OpenTelemetry.Exporter.Console/ConsoleExporterLoggingExtensions.cs @@ -14,6 +14,8 @@ // limitations under the License. // +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; using OpenTelemetry.Exporter; using OpenTelemetry.Internal; @@ -26,6 +28,7 @@ public static class ConsoleExporterLoggingExtensions /// /// options to use. /// The instance of to chain the calls. + /// todo: [Obsolete("Call LoggerProviderBuilder.AddConsoleExporter instead this method will be removed in a future version.")] public static OpenTelemetryLoggerOptions AddConsoleExporter(this OpenTelemetryLoggerOptions loggerOptions) => AddConsoleExporter(loggerOptions, configure: null); @@ -35,6 +38,7 @@ public static OpenTelemetryLoggerOptions AddConsoleExporter(this OpenTelemetryLo /// options to use. /// Callback action for configuring . /// The instance of to chain the calls. + /// todo: [Obsolete("Call LoggerProviderBuilder.AddConsoleExporter instead this method will be removed in a future version.")] public static OpenTelemetryLoggerOptions AddConsoleExporter(this OpenTelemetryLoggerOptions loggerOptions, Action configure) { Guard.ThrowIfNull(loggerOptions); @@ -43,5 +47,54 @@ public static OpenTelemetryLoggerOptions AddConsoleExporter(this OpenTelemetryLo configure?.Invoke(options); return loggerOptions.AddProcessor(new SimpleLogRecordExportProcessor(new ConsoleLogRecordExporter(options))); } + + /// + /// Adds Console exporter with LoggerProviderBuilder. + /// + /// . + /// The supplied instance of to chain the calls. + internal static LoggerProviderBuilder AddConsoleExporter( + this LoggerProviderBuilder loggerProviderBuilder) + => AddConsoleExporter(loggerProviderBuilder, name: null, configure: null); + + /// + /// Adds Console exporter with LoggerProviderBuilder. + /// + /// . + /// Callback action for configuring . + /// The supplied instance of to chain the calls. + internal static LoggerProviderBuilder AddConsoleExporter( + this LoggerProviderBuilder loggerProviderBuilder, + Action configure) + => AddConsoleExporter(loggerProviderBuilder, name: null, configure); + + /// + /// Adds Console exporter with LoggerProviderBuilder. + /// + /// . + /// Name which is used when retrieving options. + /// Callback action for configuring . + /// The supplied instance of to chain the calls. + internal static LoggerProviderBuilder AddConsoleExporter( + this LoggerProviderBuilder loggerProviderBuilder, + string name, + Action configure) + { + Guard.ThrowIfNull(loggerProviderBuilder); + + name ??= Options.DefaultName; + + if (configure != null) + { + loggerProviderBuilder.ConfigureServices(services => services.Configure(name, configure)); + } + + return loggerProviderBuilder.AddProcessor(sp => + { + var options = sp.GetRequiredService>().Get(name); + + return new SimpleLogRecordExportProcessor(new ConsoleLogRecordExporter(options)); + }); + } } } diff --git a/src/OpenTelemetry.Exporter.Console/OpenTelemetry.Exporter.Console.csproj b/src/OpenTelemetry.Exporter.Console/OpenTelemetry.Exporter.Console.csproj index b7afb8a8fab..b8bd2ceaca7 100644 --- a/src/OpenTelemetry.Exporter.Console/OpenTelemetry.Exporter.Console.csproj +++ b/src/OpenTelemetry.Exporter.Console/OpenTelemetry.Exporter.Console.csproj @@ -25,14 +25,18 @@ - - - - + + diff --git a/src/OpenTelemetry/AssemblyInfo.cs b/src/OpenTelemetry/AssemblyInfo.cs index 6b94f59eb69..6214d72ad48 100644 --- a/src/OpenTelemetry/AssemblyInfo.cs +++ b/src/OpenTelemetry/AssemblyInfo.cs @@ -17,6 +17,7 @@ using System.Runtime.CompilerServices; [assembly: InternalsVisibleTo("OpenTelemetry.Tests" + AssemblyInfo.PublicKey)] +[assembly: InternalsVisibleTo("OpenTelemetry.Exporter.Console" + AssemblyInfo.PublicKey)] [assembly: InternalsVisibleTo("OpenTelemetry.Exporter.InMemory" + AssemblyInfo.PublicKey)] [assembly: InternalsVisibleTo("OpenTelemetry.Exporter.Prometheus.AspNetCore" + AssemblyInfo.PublicKey)] [assembly: InternalsVisibleTo("OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests" + AssemblyInfo.PublicKey)] From 6e73bb9acf9b910dd8c7bc9ad63ef7720447ae66 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Tue, 6 Jun 2023 15:36:03 -0700 Subject: [PATCH 2/2] Minor perf improvement aspnetcore (#4551) --- .../Implementation/HttpInListener.cs | 9 ++++++--- .../AspNetCoreInstrumentationBenchmarks.cs | 17 ++++++++--------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs index 537ff5ea42e..d50241a3ba8 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs @@ -214,10 +214,13 @@ public void OnStartActivity(Activity activity, object payload) activity.SetTag(SemanticConventions.AttributeHttpUrl, GetUri(request)); activity.SetTag(SemanticConventions.AttributeHttpFlavor, HttpTagHelper.GetFlavorTagValueFromProtocol(request.Protocol)); - var userAgent = request.Headers["User-Agent"].FirstOrDefault(); - if (!string.IsNullOrEmpty(userAgent)) + if (request.Headers.TryGetValue("User-Agent", out var values)) { - activity.SetTag(SemanticConventions.AttributeHttpUserAgent, userAgent); + var userAgent = values.Count > 0 ? values[0] : null; + if (!string.IsNullOrEmpty(userAgent)) + { + activity.SetTag(SemanticConventions.AttributeHttpUserAgent, userAgent); + } } try diff --git a/test/Benchmarks/Instrumentation/AspNetCoreInstrumentationBenchmarks.cs b/test/Benchmarks/Instrumentation/AspNetCoreInstrumentationBenchmarks.cs index 6aa055d8bf0..4cb306ab727 100644 --- a/test/Benchmarks/Instrumentation/AspNetCoreInstrumentationBenchmarks.cs +++ b/test/Benchmarks/Instrumentation/AspNetCoreInstrumentationBenchmarks.cs @@ -24,8 +24,8 @@ using OpenTelemetry.Trace; /* -BenchmarkDotNet=v0.13.5, OS=Windows 11 (10.0.23424.1000) -Intel Core i7-9700 CPU 3.00GHz, 1 CPU, 8 logical and 8 physical cores +BenchmarkDotNet=v0.13.5, OS=Windows 11 (10.0.22621.1702/22H2/2022Update/SunValley2) +Intel Core i7-8850H CPU 2.60GHz (Coffee Lake), 1 CPU, 12 logical and 6 physical cores .NET SDK=7.0.203 [Host] : .NET 7.0.5 (7.0.523.17405), X64 RyuJIT AVX2 DefaultJob : .NET 7.0.5 (7.0.523.17405), X64 RyuJIT AVX2 @@ -33,10 +33,10 @@ | Method | EnableInstrumentation | Mean | Error | StdDev | Gen0 | Allocated | |--------------------------- |---------------------- |---------:|--------:|--------:|-------:|----------:| -| GetRequestForAspNetCoreApp | None | 226.8 us | 4.00 us | 3.74 us | - | 2.45 KB | -| GetRequestForAspNetCoreApp | Traces | 235.2 us | 4.44 us | 4.15 us | 0.4883 | 3.59 KB | -| GetRequestForAspNetCoreApp | Metrics | 229.1 us | 4.44 us | 4.36 us | - | 2.92 KB | -| GetRequestForAspNetCoreApp | Traces, Metrics | 230.6 us | 4.54 us | 5.23 us | 0.4883 | 3.66 KB | +| GetRequestForAspNetCoreApp | None | 136.8 us | 1.56 us | 1.46 us | 0.4883 | 2.45 KB | +| GetRequestForAspNetCoreApp | Traces | 148.1 us | 0.88 us | 0.82 us | 0.7324 | 3.57 KB | +| GetRequestForAspNetCoreApp | Metrics | 144.4 us | 1.16 us | 1.08 us | 0.4883 | 2.92 KB | +| GetRequestForAspNetCoreApp | Traces, Metrics | 163.0 us | 1.60 us | 1.49 us | 0.7324 | 3.63 KB | Allocation details for .NET 7: @@ -45,7 +45,6 @@ * Casting of the struct `Microsoft.Extensions.Primitives.StringValues` to `IEnumerable` by `HttpRequestHeaderValuesGetter` - `TraceContextPropagator.Extract` = 24 B - `BaggageContextPropagator.Extract` = 24 B - - `request.Headers["User-Agent"].FirstOrDefault()` = 24 B * String creation for `HttpRequest.HostString.Host` = 40 B * `Activity.TagsLinkedList` (this is allocated on the first Activity.SetTag call) = 40 B * Boxing of `Port` number when adding it as a tag = 24 B @@ -61,7 +60,7 @@ - System.Threading.ExecutionContext = 40 B Baseline = 2.45 KB -With Traces = 2.45 + (1162 / 1024) = 2.45 + 1.14 = 3.59 KB +With Traces = 2.45 + (1138 / 1024) = 2.45 + 1.12 = 3.57 KB // Metrics @@ -76,7 +75,7 @@ Baseline = 2.45 KB With Traces and Metrics = Baseline + With Traces + (With Metrics - (Activity creation + `Acitivity.Stop()`)) (they use the same activity) - = 2.45 + (1162 + 64) / 1024 = 2.45 + 1.2 = 3.55 KB (~3.56 KB) + = 2.45 + (1138 + 64) / 1024 = 2.45 + 1.17 = ~3.63KB */ namespace Benchmarks.Instrumentation