-
Notifications
You must be signed in to change notification settings - Fork 773
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
OTLP LogExporter to special case {OriginalFormat} to populate body #3182
Merged
cijothomas
merged 5 commits into
open-telemetry:main
from
cijothomas:cijothomas/otlplog_specialcaseoriginalformat
Apr 14, 2022
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0fc9655
OTLP LogExporter to special case {OriginalFormat} to populate body
cijothomas 45fdb87
Merge branch 'main' into cijothomas/otlplog_specialcaseoriginalformat
cijothomas 2c13344
Update src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md
cijothomas 798c587
add link to PR
cijothomas e9ec70c
Merge branch 'cijothomas/otlplog_specialcaseoriginalformat' of https:…
cijothomas 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 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 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 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 |
---|---|---|
|
@@ -225,28 +225,82 @@ public void CheckToOtlpLogRecordSeverityLevelAndText(LogLevel logLevel) | |
} | ||
} | ||
|
||
[Fact] | ||
public void CheckToOtlpLogRecordFormattedMessage() | ||
[Theory] | ||
[InlineData(true)] | ||
[InlineData(false)] | ||
public void CheckToOtlpLogRecordBodyIsPopulated(bool includeFormattedMessage) | ||
{ | ||
var logRecords = new List<LogRecord>(); | ||
using var loggerFactory = LoggerFactory.Create(builder => | ||
{ | ||
builder.AddOpenTelemetry(options => | ||
{ | ||
options.AddInMemoryExporter(logRecords); | ||
options.IncludeFormattedMessage = true; | ||
options.IncludeFormattedMessage = includeFormattedMessage; | ||
options.ParseStateValues = true; | ||
alanwest marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
}); | ||
|
||
var logger = loggerFactory.CreateLogger("OtlpLogExporterTests"); | ||
|
||
// Scenario 1 - Using ExtensionMethods on ILogger.Log | ||
logger.LogInformation("OpenTelemetry {Greeting} {Subject}!", "Hello", "World"); | ||
Assert.Single(logRecords); | ||
|
||
var logRecord = logRecords[0]; | ||
var otlpLogRecord = logRecord.ToOtlpLog(); | ||
|
||
Assert.NotNull(otlpLogRecord); | ||
Assert.Equal(logRecord.FormattedMessage, otlpLogRecord.Body.StringValue); | ||
if (includeFormattedMessage) | ||
{ | ||
Assert.Equal(logRecord.FormattedMessage, otlpLogRecord.Body.StringValue); | ||
} | ||
else | ||
{ | ||
Assert.Equal("OpenTelemetry {Greeting} {Subject}!", otlpLogRecord.Body.StringValue); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the "special" treatment of {OriginalFormat} key. |
||
} | ||
|
||
logRecords.Clear(); | ||
|
||
// Scenario 2 - Using the raw ILogger.Log Method | ||
logger.Log(LogLevel.Information, default, "state", exception: null, (st, ex) => "Formatted Message"); | ||
Assert.Single(logRecords); | ||
|
||
logRecord = logRecords[0]; | ||
otlpLogRecord = logRecord.ToOtlpLog(); | ||
|
||
Assert.NotNull(otlpLogRecord); | ||
if (includeFormattedMessage) | ||
{ | ||
Assert.Equal(logRecord.FormattedMessage, otlpLogRecord.Body.StringValue); | ||
} | ||
else | ||
{ | ||
Assert.Null(otlpLogRecord.Body); | ||
} | ||
|
||
logRecords.Clear(); | ||
|
||
// Scenario 3 - Using the raw ILogger.Log Method, but with null | ||
// formatter. | ||
logger.Log(LogLevel.Information, default, "state", exception: null, formatter: null); | ||
Assert.Single(logRecords); | ||
|
||
logRecord = logRecords[0]; | ||
otlpLogRecord = logRecord.ToOtlpLog(); | ||
|
||
Assert.NotNull(otlpLogRecord); | ||
|
||
// There is no formatter, so no way to populate Body. | ||
// Exporter won't even attempt to do ToString() on State. | ||
if (includeFormattedMessage) | ||
{ | ||
Assert.Null(otlpLogRecord.Body); | ||
} | ||
else | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. intentionally making the same Assert on If..Else, to make it easy for readers :) |
||
{ | ||
Assert.Null(otlpLogRecord.Body); | ||
} | ||
} | ||
|
||
[Fact] | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.