-
Notifications
You must be signed in to change notification settings - Fork 123
Add NLog instrumentation for OpenTelemetry .NET Auto-Instrumentation #4371
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
Open
danifitz
wants to merge
39
commits into
open-telemetry:main
Choose a base branch
from
danifitz:feature/nlog-instrumentation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
12b3643
Add NLog instrumentation for OpenTelemetry .NET Auto-Instrumentation
danifitz dd0eee6
Fix: https://github.com/open-telemetry/opentelemetry-dotnet-instrumen…
danifitz 1b5ee77
Fix https://github.com/open-telemetry/opentelemetry-dotnet-instrument…
danifitz 489fa3b
Fix: https://github.com/open-telemetry/opentelemetry-dotnet-instrumen…
danifitz 638a6aa
Test: https://github.com/open-telemetry/opentelemetry-dotnet-instrume…
danifitz 206be87
feat: refactor NLog instrumentation to use standard NLog Target archi…
danifitz 799ef71
refactor: remove unused NLog.Extensions.Logging from TestApplication.…
danifitz 4401757
refactor: optimize NLog target for async compatibility and performance
danifitz 0fcf644
feat: add hybrid typed layout support for NLog version compatibility
danifitz 4ffa40a
feat: implement NLog v5.3.4+ typed layouts for OpenTelemetryTarget
danifitz f6cd7d5
fix test coverage
danifitz 665fb09
fix: correct NLog bridge EmitLog method call to match OpenTelemetry SDK
danifitz 3a79c85
refactor: simplify NLog target configuration to use environment varia…
danifitz f6957e6
Removed NLog from AssemblyInfo
danifitz b078f85
feat: add NLog instrumentation with duck typing and NLog 6.x support
danifitz 4a0d5a3
Merge branch 'main' into feature/nlog-instrumentation
danifitz bf571f5
fix CHANGELOG
Kielek 16b004e
Fix build errors
Kielek e650142
commit generated file
Kielek c41f73c
Merge branch 'main' into feature/nlog-instrumentation
Kielek e66da6a
Move NLog version to test folder
Kielek 3d5aff8
cleanup solution
Kielek ad1c62f
add NLOG to dictionary
Kielek 54dd6da
typo fixes
Kielek 0602818
remove reference to NLog.Extensions.Logging
Kielek 903ab69
Update tested versions
Kielek 4702a68
Minimal assembly version set to 4.0.0
Kielek 951c355
fix sln file
Kielek 29d4251
remove reference to System.Private.Uri
Kielek 29bde76
Fix compilation for tests app
Kielek 911fbd8
Apply suggestions from code review
lachmatt 60311e1
Merge branch 'main' into feature/nlog-instrumentation
Kielek d912c7e
Merge branch 'main' into feature/nlog-instrumentation
Kielek c6b7bde
Merge branch 'main' into feature/nlog-instrumentation
Kielek fad8ea4
Fix issue occurring in VS
Kielek e3d9274
Add missing settings test case
Kielek e95e972
remove redundant lines
Kielek 62f933d
Sync implementation with available documentation
Kielek 33546ee
user facing documentation
Kielek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| # `NLog` [logs bridge](https://opentelemetry.io/docs/specs/otel/glossary/#log-appender--bridge) | ||
|
|
||
| > [!IMPORTANT] | ||
| > NLog bridge is an experimental feature. | ||
|
|
||
| The `NLog` logs bridge is disabled by default. In order to enable it, | ||
| set `OTEL_DOTNET_AUTO_LOGS_ENABLE_NLOG_BRIDGE` to `true`. | ||
|
|
||
| Bridge is supported for `NLOG` in versions >= 5.0.0 && < 7.0.0 | ||
|
|
||
| If `NLOG` is used as a [logging provider](https://learn.microsoft.com/en-us/dotnet/core/extensions/logging-providers), | ||
| `NLOG` bridge should not be enabled, in order to reduce possibility of | ||
| duplicated logs export. | ||
|
|
||
| ## `NLog` logging events conversion | ||
|
|
||
| `NLog`'s `ILoggingEvent`s are converted to OpenTelemetry log records in | ||
| a following way: | ||
|
|
||
| - `TimeStamp` is set as a `Timestamp` | ||
| - `Level.Name` is set as a `SeverityText` | ||
| - `FormattedMessage` is set as a `Body` if it is available | ||
| - Otherwise, `Message` is set as a `Body` | ||
| - `LoggerName` is set as an `InstrumentationScope.Name` | ||
| - `GetProperties()`, apart from builtin properties prefixed with `nlog:`, `NLog.`, | ||
| are added as attributes | ||
| - `Exception` is used to populate the following properties: `exception.type`, | ||
| `exception.message`, `exception.stacktrace` | ||
| - `Level.Value` is mapped to `SeverityNumber` as outlined in the next section | ||
|
|
||
| ### `NLog` level severity mapping | ||
|
|
||
| `NLog` levels are mapped to OpenTelemetry severity types according to | ||
| following rules based on their numerical values. | ||
|
|
||
| Levels with numerical values of: | ||
|
|
||
| - Equal to `LogLevel.Fatal` is mapped to `LogRecordSeverity.Fatal` | ||
| - Equal to `LogLevel.Error` is mapped to `LogRecordSeverity.Error` | ||
| - Equal to `LogLevel.Warn` is mapped to `LogRecordSeverity.Warn` | ||
| - Equal to `LogLevel.Info` is mapped to `LogRecordSeverity.Info` | ||
| - Equal to `LogLevel.Debug` is mapped to `LogRecordSeverity.Debug` | ||
| - Equal to `LogLevel.Trace` is mapped to `LogRecordSeverity.Trace` | ||
| - Equal to `LogLevel.Off` is mapped to `LogRecordSeverity.Trace` | ||
| - Any other is mapped to `LogRecordSeverity.Info`. | ||
|
|
1 change: 1 addition & 0 deletions
1
src/OpenTelemetry.AutoInstrumentation/.publicApi/net462/PublicAPI.Unshipped.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/OpenTelemetry.AutoInstrumentation/.publicApi/net8.0/PublicAPI.Unshipped.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...OpenTelemetry.AutoInstrumentation/Instrumentations/NLog/AutoInjection/NLogAutoInjector.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using System.Reflection; | ||
| using OpenTelemetry.AutoInstrumentation.DuckTyping; | ||
| using OpenTelemetry.AutoInstrumentation.Logging; | ||
|
|
||
| namespace OpenTelemetry.AutoInstrumentation.Instrumentations.NLog.AutoInjection; | ||
|
|
||
| internal static class NLogAutoInjector | ||
| { | ||
| private static readonly IOtelLogger Logger = OtelLogging.GetLogger(); | ||
| private static int _attempted; | ||
|
|
||
| public static void EnsureConfigured() | ||
| { | ||
| if (Interlocked.Exchange(ref _attempted, 1) != 0) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| try | ||
| { | ||
| var nlogLogManager = Type.GetType("NLog.LogManager, NLog"); | ||
| if (nlogLogManager is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var configurationProperty = nlogLogManager.GetProperty("Configuration", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); | ||
| if (configurationProperty is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var configuration = configurationProperty.GetValue(null); | ||
| if (configuration is null) | ||
| { | ||
| var configurationType = Type.GetType("NLog.Config.LoggingConfiguration, NLog"); | ||
| configuration = Activator.CreateInstance(configurationType!); | ||
| configurationProperty.SetValue(null, configuration); | ||
| } | ||
|
|
||
| // Create the OpenTelemetry target instance and wrap it in a duck proxy | ||
| var otelTarget = new OpenTelemetryTarget(); | ||
| var targetType = Type.GetType("NLog.Targets.TargetWithContext, NLog", false); | ||
| if (targetType is null) | ||
| { | ||
| Logger.Warning("NLog auto-injection skipped: TargetWithContext type not found."); | ||
| return; | ||
| } | ||
|
|
||
| var targetProxy = otelTarget.DuckImplement(targetType); | ||
|
|
||
| // Add target to configuration | ||
| var addTargetMethod = configuration!.GetType().GetMethod("AddTarget", BindingFlags.Instance | BindingFlags.Public); | ||
| addTargetMethod?.Invoke(configuration, new object?[] { "otlp", targetProxy }); | ||
|
|
||
| // Create rule: * -> otlp (minlevel: Trace) | ||
| var loggingRuleType = Type.GetType("NLog.Config.LoggingRule, NLog"); | ||
| var logLevelType = Type.GetType("NLog.LogLevel, NLog"); | ||
| var traceLevel = logLevelType?.GetProperty("Trace", BindingFlags.Static | BindingFlags.Public)?.GetValue(null); | ||
| var rule = Activator.CreateInstance(loggingRuleType!, new object?[] { "*", traceLevel, targetProxy }); | ||
|
|
||
| var loggingRulesProp = configuration.GetType().GetProperty("LoggingRules", BindingFlags.Instance | BindingFlags.Public); | ||
| var rulesList = loggingRulesProp?.GetValue(configuration) as System.Collections.IList; | ||
| rulesList?.Add(rule); | ||
|
|
||
| // Apply configuration | ||
| var reconfigMethod = nlogLogManager.GetMethod("ReconfigExistingLoggers", BindingFlags.Static | BindingFlags.Public); | ||
| reconfigMethod?.Invoke(null, null); | ||
|
|
||
| Logger.Information("NLog OpenTelemetryTarget auto-injected."); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Logger.Warning(ex, "NLog OpenTelemetryTarget auto-injection failed."); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.