From 02ceb1a7642ae57adaee99ba393d05872b0ac47b Mon Sep 17 00:00:00 2001 From: Reiley Yang Date: Wed, 17 Mar 2021 09:30:31 -0700 Subject: [PATCH] Add resource support to logger (#1913) add resource support to logger --- src/OpenTelemetry.Exporter.Console/CHANGELOG.md | 2 ++ .../ConsoleLogRecordExporter.cs | 11 +++++++++++ .../.publicApi/net461/PublicAPI.Shipped.txt | 4 +--- .../.publicApi/net461/PublicAPI.Unshipped.txt | 2 ++ .../netstandard2.0/PublicAPI.Shipped.txt | 2 -- .../netstandard2.0/PublicAPI.Unshipped.txt | 2 ++ src/OpenTelemetry/CHANGELOG.md | 3 +++ .../Logs/OpenTelemetryLoggerOptions.cs | 15 +++++++++++++++ .../Logs/OpenTelemetryLoggerProvider.cs | 17 ++++++++--------- src/OpenTelemetry/ProviderExtensions.cs | 9 +++++++++ 10 files changed, 53 insertions(+), 14 deletions(-) diff --git a/src/OpenTelemetry.Exporter.Console/CHANGELOG.md b/src/OpenTelemetry.Exporter.Console/CHANGELOG.md index 8c82f45d902..d088ab39873 100644 --- a/src/OpenTelemetry.Exporter.Console/CHANGELOG.md +++ b/src/OpenTelemetry.Exporter.Console/CHANGELOG.md @@ -14,6 +14,8 @@ please check the latest changes * LogRecordExporter exports Message, Scope, StateValues from LogRecord. ([#1871](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1871) [#1895](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1895)) +* Added Resource support. + ([#1913](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1913)) ## 1.0.1 diff --git a/src/OpenTelemetry.Exporter.Console/ConsoleLogRecordExporter.cs b/src/OpenTelemetry.Exporter.Console/ConsoleLogRecordExporter.cs index 6a794e28d35..b519bdb3b0f 100644 --- a/src/OpenTelemetry.Exporter.Console/ConsoleLogRecordExporter.cs +++ b/src/OpenTelemetry.Exporter.Console/ConsoleLogRecordExporter.cs @@ -17,6 +17,7 @@ #if NET461 || NETSTANDARD2_0 using System; using OpenTelemetry.Logs; +using OpenTelemetry.Resources; namespace OpenTelemetry.Exporter { @@ -68,6 +69,16 @@ public override ExportResult Export(in Batch batch) static void ProcessScope(object scope, ConsoleLogRecordExporter exporter) => exporter.WriteLine($"{"LogRecord.Scope:".PadRight(RightPaddingLength)}{scope}"); + var resource = this.ParentProvider.GetResource(); + if (resource != Resource.Empty) + { + this.WriteLine("Resource associated with LogRecord:"); + foreach (var resourceAttribute in resource.Attributes) + { + this.WriteLine($" {resourceAttribute.Key}: {resourceAttribute.Value}"); + } + } + this.WriteLine(string.Empty); } diff --git a/src/OpenTelemetry/.publicApi/net461/PublicAPI.Shipped.txt b/src/OpenTelemetry/.publicApi/net461/PublicAPI.Shipped.txt index 45f1c4b9525..2864a04c407 100644 --- a/src/OpenTelemetry/.publicApi/net461/PublicAPI.Shipped.txt +++ b/src/OpenTelemetry/.publicApi/net461/PublicAPI.Shipped.txt @@ -66,7 +66,6 @@ OpenTelemetry.Logs.OpenTelemetryLoggerOptions.AddProcessor(OpenTelemetry.BasePro OpenTelemetry.Logs.OpenTelemetryLoggerOptions.OpenTelemetryLoggerOptions() -> void OpenTelemetry.Logs.OpenTelemetryLoggerProvider OpenTelemetry.Logs.OpenTelemetryLoggerProvider.CreateLogger(string categoryName) -> Microsoft.Extensions.Logging.ILogger -OpenTelemetry.Logs.OpenTelemetryLoggerProvider.Dispose() -> void OpenTelemetry.Logs.OpenTelemetryLoggerProvider.OpenTelemetryLoggerProvider(Microsoft.Extensions.Options.IOptionsMonitor options) -> void OpenTelemetry.ProviderExtensions OpenTelemetry.Resources.Resource @@ -171,5 +170,4 @@ virtual OpenTelemetry.BaseProcessor.Dispose(bool disposing) -> void virtual OpenTelemetry.BaseProcessor.OnEnd(T data) -> void virtual OpenTelemetry.BaseProcessor.OnForceFlush(int timeoutMilliseconds) -> bool virtual OpenTelemetry.BaseProcessor.OnShutdown(int timeoutMilliseconds) -> bool -virtual OpenTelemetry.BaseProcessor.OnStart(T data) -> void -virtual OpenTelemetry.Logs.OpenTelemetryLoggerProvider.Dispose(bool disposing) -> void +virtual OpenTelemetry.BaseProcessor.OnStart(T data) -> void \ No newline at end of file diff --git a/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt index cf073b25a0c..201f62ad314 100644 --- a/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt @@ -7,7 +7,9 @@ OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeScopes.get -> bool OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeScopes.set -> void OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ParseStateValues.get -> bool OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ParseStateValues.set -> void +OpenTelemetry.Logs.OpenTelemetryLoggerOptions.SetResourceBuilder(OpenTelemetry.Resources.ResourceBuilder resourceBuilder) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions OpenTelemetry.Trace.ParentBasedSampler.ParentBasedSampler(OpenTelemetry.Trace.Sampler rootSampler, OpenTelemetry.Trace.Sampler remoteParentSampled = null, OpenTelemetry.Trace.Sampler remoteParentNotSampled = null, OpenTelemetry.Trace.Sampler localParentSampled = null, OpenTelemetry.Trace.Sampler localParentNotSampled = null) -> void +override OpenTelemetry.Logs.OpenTelemetryLoggerProvider.Dispose(bool disposing) -> void static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddLegacySource(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, string operationName) -> OpenTelemetry.Trace.TracerProviderBuilder static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetErrorStatusOnException(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, bool enabled = true) -> OpenTelemetry.Trace.TracerProviderBuilder static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool diff --git a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Shipped.txt b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Shipped.txt index 45f1c4b9525..52371f86734 100644 --- a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Shipped.txt +++ b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Shipped.txt @@ -66,7 +66,6 @@ OpenTelemetry.Logs.OpenTelemetryLoggerOptions.AddProcessor(OpenTelemetry.BasePro OpenTelemetry.Logs.OpenTelemetryLoggerOptions.OpenTelemetryLoggerOptions() -> void OpenTelemetry.Logs.OpenTelemetryLoggerProvider OpenTelemetry.Logs.OpenTelemetryLoggerProvider.CreateLogger(string categoryName) -> Microsoft.Extensions.Logging.ILogger -OpenTelemetry.Logs.OpenTelemetryLoggerProvider.Dispose() -> void OpenTelemetry.Logs.OpenTelemetryLoggerProvider.OpenTelemetryLoggerProvider(Microsoft.Extensions.Options.IOptionsMonitor options) -> void OpenTelemetry.ProviderExtensions OpenTelemetry.Resources.Resource @@ -172,4 +171,3 @@ virtual OpenTelemetry.BaseProcessor.OnEnd(T data) -> void virtual OpenTelemetry.BaseProcessor.OnForceFlush(int timeoutMilliseconds) -> bool virtual OpenTelemetry.BaseProcessor.OnShutdown(int timeoutMilliseconds) -> bool virtual OpenTelemetry.BaseProcessor.OnStart(T data) -> void -virtual OpenTelemetry.Logs.OpenTelemetryLoggerProvider.Dispose(bool disposing) -> void diff --git a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt index cf073b25a0c..201f62ad314 100644 --- a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -7,7 +7,9 @@ OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeScopes.get -> bool OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeScopes.set -> void OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ParseStateValues.get -> bool OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ParseStateValues.set -> void +OpenTelemetry.Logs.OpenTelemetryLoggerOptions.SetResourceBuilder(OpenTelemetry.Resources.ResourceBuilder resourceBuilder) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions OpenTelemetry.Trace.ParentBasedSampler.ParentBasedSampler(OpenTelemetry.Trace.Sampler rootSampler, OpenTelemetry.Trace.Sampler remoteParentSampled = null, OpenTelemetry.Trace.Sampler remoteParentNotSampled = null, OpenTelemetry.Trace.Sampler localParentSampled = null, OpenTelemetry.Trace.Sampler localParentNotSampled = null) -> void +override OpenTelemetry.Logs.OpenTelemetryLoggerProvider.Dispose(bool disposing) -> void static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddLegacySource(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, string operationName) -> OpenTelemetry.Trace.TracerProviderBuilder static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetErrorStatusOnException(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, bool enabled = true) -> OpenTelemetry.Trace.TracerProviderBuilder static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool diff --git a/src/OpenTelemetry/CHANGELOG.md b/src/OpenTelemetry/CHANGELOG.md index c3fc8d255a2..b3f0e5241eb 100644 --- a/src/OpenTelemetry/CHANGELOG.md +++ b/src/OpenTelemetry/CHANGELOG.md @@ -49,6 +49,9 @@ please check the latest changes ([#1869](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1869) & [#1883](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1883)) +* Added `SetResourceBuilder` support to `OpenTelemetryLoggerOptions`. + ([#1913](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1913)) + ## 1.0.1 Released 2021-Feb-10 diff --git a/src/OpenTelemetry/Logs/OpenTelemetryLoggerOptions.cs b/src/OpenTelemetry/Logs/OpenTelemetryLoggerOptions.cs index 6eaf780d9b9..f1e09804c4d 100644 --- a/src/OpenTelemetry/Logs/OpenTelemetryLoggerOptions.cs +++ b/src/OpenTelemetry/Logs/OpenTelemetryLoggerOptions.cs @@ -17,12 +17,14 @@ #if NET461 || NETSTANDARD2_0 using System; using System.Collections.Generic; +using OpenTelemetry.Resources; namespace OpenTelemetry.Logs { public class OpenTelemetryLoggerOptions { internal readonly List> Processors = new List>(); + internal ResourceBuilder ResourceBuilder = ResourceBuilder.CreateDefault(); /// /// Gets or sets a value indicating whether or not log scopes should be @@ -66,6 +68,19 @@ public OpenTelemetryLoggerOptions AddProcessor(BaseProcessor processo return this; } + + /// + /// Sets the from which the Resource associated with + /// this provider is built from. Overwrites currently set ResourceBuilder. + /// + /// from which Resource will be built. + /// Returns for chaining. + public OpenTelemetryLoggerOptions SetResourceBuilder(ResourceBuilder resourceBuilder) + { + this.ResourceBuilder = resourceBuilder ?? throw new ArgumentNullException(nameof(resourceBuilder)); + + return this; + } } } #endif diff --git a/src/OpenTelemetry/Logs/OpenTelemetryLoggerProvider.cs b/src/OpenTelemetry/Logs/OpenTelemetryLoggerProvider.cs index d979c475fec..6fe39c7aca4 100644 --- a/src/OpenTelemetry/Logs/OpenTelemetryLoggerProvider.cs +++ b/src/OpenTelemetry/Logs/OpenTelemetryLoggerProvider.cs @@ -19,14 +19,16 @@ using System.Collections; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; +using OpenTelemetry.Resources; namespace OpenTelemetry.Logs { [ProviderAlias("OpenTelemetry")] - public class OpenTelemetryLoggerProvider : ILoggerProvider, ISupportExternalScope + public class OpenTelemetryLoggerProvider : BaseProvider, ILoggerProvider, ISupportExternalScope { internal readonly OpenTelemetryLoggerOptions Options; internal BaseProcessor Processor; + internal Resource Resource; private readonly Hashtable loggers = new Hashtable(); private bool disposed; private IExternalScopeProvider scopeProvider; @@ -47,6 +49,8 @@ internal OpenTelemetryLoggerProvider(OpenTelemetryLoggerOptions options) { this.Options = options ?? throw new ArgumentNullException(nameof(options)); + this.Resource = options.ResourceBuilder.Build(); + foreach (var processor in options.Processors) { this.AddProcessor(processor); @@ -91,13 +95,6 @@ public ILogger CreateLogger(string categoryName) return logger; } - /// - public void Dispose() - { - this.Dispose(true); - GC.SuppressFinalize(this); - } - internal OpenTelemetryLoggerProvider AddProcessor(BaseProcessor processor) { if (processor == null) @@ -105,6 +102,8 @@ internal OpenTelemetryLoggerProvider AddProcessor(BaseProcessor proce throw new ArgumentNullException(nameof(processor)); } + processor.SetParentProvider(this); + if (this.Processor == null) { this.Processor = processor; @@ -125,7 +124,7 @@ internal OpenTelemetryLoggerProvider AddProcessor(BaseProcessor proce return this; } - protected virtual void Dispose(bool disposing) + protected override void Dispose(bool disposing) { if (this.disposed) { diff --git a/src/OpenTelemetry/ProviderExtensions.cs b/src/OpenTelemetry/ProviderExtensions.cs index 4f792c151af..828ae279be1 100644 --- a/src/OpenTelemetry/ProviderExtensions.cs +++ b/src/OpenTelemetry/ProviderExtensions.cs @@ -14,6 +14,9 @@ // limitations under the License. // +#if NET461 || NETSTANDARD2_0 +using OpenTelemetry.Logs; +#endif using OpenTelemetry.Resources; using OpenTelemetry.Trace; @@ -35,6 +38,12 @@ public static Resource GetResource(this BaseProvider baseProvider) { return tracerProviderSdk.Resource; } +#if NET461 || NETSTANDARD2_0 + else if (baseProvider is OpenTelemetryLoggerProvider otelLoggerProvider) + { + return otelLoggerProvider.Resource; + } +#endif return Resource.Empty; }