Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resource support to logger #1913

Merged
merged 8 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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 = logRecord.Resource;
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
5 changes: 5 additions & 0 deletions src/OpenTelemetry/Logs/LogRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Extensions.Logging;
using OpenTelemetry.Resources;

namespace OpenTelemetry.Logs
{
Expand All @@ -37,6 +38,7 @@ internal LogRecord(
EventId eventId,
string formattedMessage,
object state,
Resource resource,
Exception exception,
IReadOnlyList<KeyValuePair<string, object>> stateValues)
{
Expand All @@ -58,6 +60,7 @@ internal LogRecord(
this.FormattedMessage = formattedMessage;
this.State = state;
this.StateValues = stateValues;
this.Resource = resource;
this.Exception = exception;
}

Expand All @@ -79,6 +82,8 @@ internal LogRecord(

public string FormattedMessage { get; }

public Resource Resource { get; }

public object State { get; }

public IReadOnlyList<KeyValuePair<string, object>> StateValues { get; }
Expand Down
1 change: 1 addition & 0 deletions src/OpenTelemetry/Logs/OpenTelemetryLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
eventId,
options.IncludeFormattedMessage ? formatter(state, exception) : null,
options.ParseStateValues ? null : (object)state,
this.provider.Resource,
exception,
options.ParseStateValues ? this.ParseState(state) : null);

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
4 changes: 4 additions & 0 deletions src/OpenTelemetry/Logs/OpenTelemetryLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System.Collections;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OpenTelemetry.Resources;

namespace OpenTelemetry.Logs
{
Expand All @@ -27,6 +28,7 @@ public class OpenTelemetryLoggerProvider : ILoggerProvider, ISupportExternalScop
{
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();
reyang marked this conversation as resolved.
Show resolved Hide resolved

foreach (var processor in options.Processors)
{
this.AddProcessor(processor);
Expand Down