Skip to content

Commit

Permalink
Add resource support to logger (#1913)
Browse files Browse the repository at this point in the history
add resource support to logger
  • Loading branch information
reyang authored Mar 17, 2021
1 parent 9c08bd7 commit 02ceb1a
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/OpenTelemetry.Exporter.Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions src/OpenTelemetry.Exporter.Console/ConsoleLogRecordExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#if NET461 || NETSTANDARD2_0
using System;
using OpenTelemetry.Logs;
using OpenTelemetry.Resources;

namespace OpenTelemetry.Exporter
{
Expand Down Expand Up @@ -68,6 +69,16 @@ public override ExportResult Export(in Batch<LogRecord> 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);
}

Expand Down
4 changes: 1 addition & 3 deletions src/OpenTelemetry/.publicApi/net461/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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<OpenTelemetry.Logs.OpenTelemetryLoggerOptions> options) -> void
OpenTelemetry.ProviderExtensions
OpenTelemetry.Resources.Resource
Expand Down Expand Up @@ -171,5 +170,4 @@ virtual OpenTelemetry.BaseProcessor<T>.Dispose(bool disposing) -> void
virtual OpenTelemetry.BaseProcessor<T>.OnEnd(T data) -> void
virtual OpenTelemetry.BaseProcessor<T>.OnForceFlush(int timeoutMilliseconds) -> bool
virtual OpenTelemetry.BaseProcessor<T>.OnShutdown(int timeoutMilliseconds) -> bool
virtual OpenTelemetry.BaseProcessor<T>.OnStart(T data) -> void
virtual OpenTelemetry.Logs.OpenTelemetryLoggerProvider.Dispose(bool disposing) -> void
virtual OpenTelemetry.BaseProcessor<T>.OnStart(T data) -> void
2 changes: 2 additions & 0 deletions src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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<OpenTelemetry.Logs.OpenTelemetryLoggerOptions> options) -> void
OpenTelemetry.ProviderExtensions
OpenTelemetry.Resources.Resource
Expand Down Expand Up @@ -172,4 +171,3 @@ virtual OpenTelemetry.BaseProcessor<T>.OnEnd(T data) -> void
virtual OpenTelemetry.BaseProcessor<T>.OnForceFlush(int timeoutMilliseconds) -> bool
virtual OpenTelemetry.BaseProcessor<T>.OnShutdown(int timeoutMilliseconds) -> bool
virtual OpenTelemetry.BaseProcessor<T>.OnStart(T data) -> void
virtual OpenTelemetry.Logs.OpenTelemetryLoggerProvider.Dispose(bool disposing) -> void
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions src/OpenTelemetry/Logs/OpenTelemetryLoggerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BaseProcessor<LogRecord>> Processors = new List<BaseProcessor<LogRecord>>();
internal ResourceBuilder ResourceBuilder = ResourceBuilder.CreateDefault();

/// <summary>
/// Gets or sets a value indicating whether or not log scopes should be
Expand Down Expand Up @@ -66,6 +68,19 @@ public OpenTelemetryLoggerOptions AddProcessor(BaseProcessor<LogRecord> processo

return this;
}

/// <summary>
/// Sets the <see cref="ResourceBuilder"/> from which the Resource associated with
/// this provider is built from. Overwrites currently set ResourceBuilder.
/// </summary>
/// <param name="resourceBuilder"><see cref="ResourceBuilder"/> from which Resource will be built.</param>
/// <returns>Returns <see cref="OpenTelemetryLoggerOptions"/> for chaining.</returns>
public OpenTelemetryLoggerOptions SetResourceBuilder(ResourceBuilder resourceBuilder)
{
this.ResourceBuilder = resourceBuilder ?? throw new ArgumentNullException(nameof(resourceBuilder));

return this;
}
}
}
#endif
17 changes: 8 additions & 9 deletions src/OpenTelemetry/Logs/OpenTelemetryLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LogRecord> Processor;
internal Resource Resource;
private readonly Hashtable loggers = new Hashtable();
private bool disposed;
private IExternalScopeProvider scopeProvider;
Expand All @@ -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);
Expand Down Expand Up @@ -91,20 +95,15 @@ public ILogger CreateLogger(string categoryName)
return logger;
}

/// <inheritdoc/>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}

internal OpenTelemetryLoggerProvider AddProcessor(BaseProcessor<LogRecord> processor)
{
if (processor == null)
{
throw new ArgumentNullException(nameof(processor));
}

processor.SetParentProvider(this);

if (this.Processor == null)
{
this.Processor = processor;
Expand All @@ -125,7 +124,7 @@ internal OpenTelemetryLoggerProvider AddProcessor(BaseProcessor<LogRecord> proce
return this;
}

protected virtual void Dispose(bool disposing)
protected override void Dispose(bool disposing)
{
if (this.disposed)
{
Expand Down
9 changes: 9 additions & 0 deletions src/OpenTelemetry/ProviderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
// limitations under the License.
// </copyright>

#if NET461 || NETSTANDARD2_0
using OpenTelemetry.Logs;
#endif
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

Expand All @@ -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;
}
Expand Down

0 comments on commit 02ceb1a

Please sign in to comment.