Extend ConsoleExporter to add support for Logs#1438
Conversation
…orter; Added net451 as a traget framework for OpenTelemetry.Exporter.Console
Codecov Report
@@ Coverage Diff @@
## master #1438 +/- ##
==========================================
- Coverage 81.70% 81.68% -0.02%
==========================================
Files 229 229
Lines 6095 6095
==========================================
- Hits 4980 4979 -1
- Misses 1115 1116 +1
|
|
Please add an entry to the |
| foreach (var item in batch) | ||
| { | ||
| var logRecord = item as LogRecord; | ||
| Console.WriteLine($"{"LogRecord.TraceId:".PadRight(rightPaddingLength)}{logRecord.TraceId}"); |
There was a problem hiding this comment.
nit: the output is a bit different from the Activity one (e.g. here we have the LogRecord.Something).
Might be nice to make them consistent. Not blocking though.
There was a problem hiding this comment.
It's LogRecord.propertyName right now, what you suggest it be? Should we use "Activity" instead of "LogRecord"?
There was a problem hiding this comment.
Would you help to update the example to use the console exporter?
We can use it to drive the output format discussion.
| public override ExportResult Export(in Batch<T> batch) | ||
| { | ||
| foreach (var activity in batch) | ||
| if (typeof(T) == typeof(Activity)) |
There was a problem hiding this comment.
nit: Would prob be more efficient to check the type in ctor and then set a field instead of checking every batch.
| namespace OpenTelemetry.Exporter | ||
| { | ||
| public class ConsoleExporter : BaseExporter<Activity> | ||
| public class ConsoleExporter<T> : BaseExporter<T> |
There was a problem hiding this comment.
So now I can do ConsoleExporter<some_custom_type> and it will emit nothing? I don't see a lot of value in having the generic, TBH. Why don't we just have ConsoleLogExporter : BaseExporter<LogRecord> & ConsoleActivityExporter : BaseExporter<Activity>? There's really not that much code being duplicated.
There was a problem hiding this comment.
OR how about something like...
public class ConsoleExporter<T> : BaseExporter<T>
{
public ConsoleExporter(Action<T> writeToConsole) { ... }
public override ExportResult Export(in Batch<T> batch)
{
foreach (var item in batch) {
this.writeToConsole(item);
}
}
}
public class ConsoleLogExporter : ConsoleExporter<LogRecord>
{
public ConsoleLogExporter () : base(WriteLogToConsole) { }
private static void WriteLogToConsole(LogRecord logRecord) {
var rightPaddingLength = 30;
Console.WriteLine($"{"LogRecord.TraceId:".PadRight(rightPaddingLength)}{logRecord.TraceId}");
Console.WriteLine($"{"LogRecord.SpanId:".PadRight(rightPaddingLength)}{logRecord.SpanId}");
Console.WriteLine($"{"LogRecord.Timestamp:".PadRight(rightPaddingLength)}{logRecord.Timestamp:yyyy-MM-ddTHH:mm:ss.fffffffZ}");
Console.WriteLine($"{"LogRecord.EventId:".PadRight(rightPaddingLength)}{logRecord.EventId}");
Console.WriteLine($"{"LogRecord.CategoryName:".PadRight(rightPaddingLength)}{logRecord.CategoryName}");
Console.WriteLine($"{"LogRecord.LogLevel:".PadRight(rightPaddingLength)}{logRecord.LogLevel}");
Console.WriteLine($"{"LogRecord.TraceFlags:".PadRight(rightPaddingLength)}{logRecord.TraceFlags}");
Console.WriteLine($"{"LogRecord.State:".PadRight(rightPaddingLength)}{logRecord.State}");
if (logRecord.Exception is { })
{
Console.WriteLine($"{"LogRecord.Exception:".PadRight(rightPaddingLength)}{logRecord.Exception?.Message}");
}
Console.WriteLine();
}
}There was a problem hiding this comment.
I don't think you can add an Exporter of a type anything other than Activity or LogRecord. To add an exporter for traces, you have to use the TracerProviderBuilder AddProcessor(BaseProcessor processor) method of TracerProviderBuilder class. Here, both SimpleExportProcessor and BatchExportProcessor will enforce you to only use Activity as the type.
Similarly, to add an exporter to Logs, you have to use the extension method OpenTelemetryLoggerOptions AddProcessor(BaseProcessor processor). Here, both SimpleExportProcessor and BatchExportProcessor will enforce you to only use LogRecord as the type.
There was a problem hiding this comment.
That's a good point. I still feel like it is misusing generics and we could improve it though. If it was internal, no prob, but it is part of the public API so we'll have to live with it forever.
@cijothomas @reyang Any thoughts?
There was a problem hiding this comment.
No strong opinion from my side.
I agree that having a switch-case (or if-else) in generic method (e.g. if T is Activity do this, if T is LogRecord do that) is not very idiomatic / performant.
|
why remove output as json format, I think this is the useful function here, recently i want to put the log to splunks failed without josn output here. |
Changes