Skip to content

Commit

Permalink
Added JaegerLogRecordProcessor to send logs from Microsoft.Extensions…
Browse files Browse the repository at this point in the history
….Logging.ILogger to Jaeger (open-telemetry#1739)
  • Loading branch information
Sergey Ivanov committed Feb 11, 2021
1 parent cde2060 commit c132085
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/OpenTelemetry.Exporter.Jaeger/JaegerLogRecordProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// <copyright file="JaegerLogRecordProcessor.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System.Diagnostics;
using OpenTelemetry.Logs;

namespace OpenTelemetry.Exporter.Jaeger
{
public class JaegerLogRecordProcessor : BaseProcessor<LogRecord>
{
private readonly JaegerLogRecordProcessorOptions options;

public JaegerLogRecordProcessor(JaegerLogRecordProcessorOptions options)
{
this.options = options ?? new JaegerLogRecordProcessorOptions();
}

public override void OnEnd(LogRecord data)
{
if (Activity.Current != null)
{
var tags = new ActivityTagsCollection

This comment has been minimized.

Copy link
@RehanSaeed

RehanSaeed Feb 12, 2021

Sometimes logs have additional properties. Is it possible to make this list dynamic, depending on the log message?

This comment has been minimized.

Copy link
@CodeBlanch

CodeBlanch Feb 12, 2021

@RehanSaeed

Yes I don't think this part will particularly useful: { nameof(data.State), data.State },

Really what we want to do is push properties from the state into tags. But that will be expensive at runtime, so it needs to be behind an option. Similar for scopes? Probably need an IncludeScopes type of option. But I think we should get the basic stuff in (ignoring state & scopes) and then we can do more work in follow-up PRs to add better support for those things.

{
{ nameof(data.CategoryName), data.CategoryName },
{ nameof(data.EventId), data.EventId },
{ nameof(data.Exception), data.Exception },
{ nameof(data.LogLevel), data.LogLevel },
{ nameof(data.SpanId), data.SpanId },
{ nameof(data.State), data.State },
{ nameof(data.Timestamp), data.Timestamp },
{ nameof(data.TraceFlags), data.TraceFlags },
{ nameof(data.TraceId), data.TraceId },
{ nameof(data.TraceState), data.TraceState },
};

var activityEvent = new ActivityEvent(data.CategoryName, default, tags);
Activity.Current.AddEvent(activityEvent);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// <copyright file="JaegerProcessorLoggingOptions.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

namespace OpenTelemetry.Exporter.Jaeger
{
public class JaegerLogRecordProcessorOptions
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// <copyright file="JaegerExporterLoggingExtensions.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System;
using OpenTelemetry.Logs;

namespace OpenTelemetry.Exporter.Jaeger
{
public static class JaegerLoggerProcessorExtensions
{
/// <summary>
/// Adds Jaeger LogRecord Processor as a configuration to the OpenTelemetry ILoggingBuilder.
/// </summary>
/// <param name="loggerOptions"><see cref="OpenTelemetryLoggerOptions"/> options to use.</param>
/// <param name="configure">Exporter configuration options.</param>
/// <returns>The instance of <see cref="OpenTelemetryLoggerOptions"/> to chain the calls.</returns>
/// <exception cref="ArgumentNullException"><paramref name="loggerOptions"/> is <c>null</c>.</exception>
public static OpenTelemetryLoggerOptions AddJaegerLogRecordProcessor(this OpenTelemetryLoggerOptions loggerOptions, Action<JaegerLogRecordProcessorOptions> configure = null)
{
if (loggerOptions == null)
{
throw new ArgumentNullException(nameof(loggerOptions));
}

var options = new JaegerLogRecordProcessorOptions();
configure?.Invoke(options);
return loggerOptions.AddProcessor(new JaegerLogRecordProcessor(options));
}
}
}

0 comments on commit c132085

Please sign in to comment.