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

Open Telemetry Console Exporter Json output #5588

Closed
wants to merge 11 commits into from
169 changes: 61 additions & 108 deletions src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
// SPDX-License-Identifier: Apache-2.0

using System.Diagnostics;
using System.Text.Json;
using System.Text.Json.Serialization;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

namespace OpenTelemetry.Exporter;

Expand All @@ -18,119 +19,71 @@ public override ExportResult Export(in Batch<Activity> batch)
{
foreach (var activity in batch)
{
this.WriteLine($"Activity.TraceId: {activity.TraceId}");
this.WriteLine($"Activity.SpanId: {activity.SpanId}");
this.WriteLine($"Activity.TraceFlags: {activity.ActivityTraceFlags}");
if (!string.IsNullOrEmpty(activity.TraceStateString))
var output = new
{
this.WriteLine($"Activity.TraceState: {activity.TraceStateString}");
}

if (activity.ParentSpanId != default)
{
this.WriteLine($"Activity.ParentSpanId: {activity.ParentSpanId}");
}

this.WriteLine($"Activity.ActivitySourceName: {activity.Source.Name}");
if (!string.IsNullOrEmpty(activity.Source.Version))
{
this.WriteLine($"Activity.ActivitySourceVersion: {activity.Source.Version}");
}

this.WriteLine($"Activity.DisplayName: {activity.DisplayName}");
this.WriteLine($"Activity.Kind: {activity.Kind}");
this.WriteLine($"Activity.StartTime: {activity.StartTimeUtc:yyyy-MM-ddTHH:mm:ss.fffffffZ}");
this.WriteLine($"Activity.Duration: {activity.Duration}");
var statusCode = string.Empty;
var statusDesc = string.Empty;

if (activity.TagObjects.Any())
{
this.WriteLine("Activity.Tags:");
foreach (ref readonly var tag in activity.EnumerateTagObjects())
{
if (tag.Key == SpanAttributeConstants.StatusCodeKey)
{
statusCode = tag.Value as string;
continue;
}

if (tag.Key == SpanAttributeConstants.StatusDescriptionKey)
{
statusDesc = tag.Value as string;
continue;
}

if (this.TagWriter.TryTransformTag(tag, out var result))
{
this.WriteLine($" {result.Key}: {result.Value}");
}
}
}

if (activity.Status != ActivityStatusCode.Unset)
{
this.WriteLine($"StatusCode: {activity.Status}");
if (!string.IsNullOrEmpty(activity.StatusDescription))
{
this.WriteLine($"Activity.StatusDescription: {activity.StatusDescription}");
}
}
else if (!string.IsNullOrEmpty(statusCode))
{
this.WriteLine($" StatusCode: {statusCode}");
if (!string.IsNullOrEmpty(statusDesc))
{
this.WriteLine($" Activity.StatusDescription: {statusDesc}");
}
}

if (activity.Events.Any())
{
this.WriteLine("Activity.Events:");
foreach (ref readonly var activityEvent in activity.EnumerateEvents())
Activity = new
{
this.WriteLine($" {activityEvent.Name} [{activityEvent.Timestamp}]");
foreach (ref readonly var attribute in activityEvent.EnumerateTagObjects())
{
if (this.TagWriter.TryTransformTag(attribute, out var result))
TraceId = activity.TraceId.ToString(),
SpanId = activity.SpanId.ToString(),
TraceFlags = activity.ActivityTraceFlags,
TraceState = string.IsNullOrEmpty(activity.TraceStateString) ? null : activity.TraceStateString,
ParentSpanId = activity.ParentSpanId == default ? default : activity.ParentSpanId.ToString(),
ActivitySourceName = activity.Source.Name,
ActivitySourceVersion =
string.IsNullOrEmpty(activity.Source.Version) ? null : activity.Source.Version,
DisplayName = activity.DisplayName,
Kind = activity.Kind,
StartTime = activity.StartTimeUtc.ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ"),
Duration = activity.Duration,
Tags = activity.TagObjects.Select(tag => new { Key = tag.Key, Value = tag.Value }).ToList(),
StatusCode = activity.Status != ActivityStatusCode.Unset ? activity.Status.ToString() : null,
StatusDescription =
!string.IsNullOrEmpty(activity.StatusDescription) ? activity.StatusDescription : null,
Events = activity.Events.Any()
? null
: activity.Events.Select(e => new
{
this.WriteLine($" {result.Key}: {result.Value}");
}
}
}
}

if (activity.Links.Any())
{
this.WriteLine("Activity.Links:");
foreach (ref readonly var activityLink in activity.EnumerateLinks())
{
this.WriteLine($" {activityLink.Context.TraceId} {activityLink.Context.SpanId}");
foreach (ref readonly var attribute in activityLink.EnumerateTagObjects())
{
if (this.TagWriter.TryTransformTag(attribute, out var result))
Name = e.Name,
Timestamp = e.Timestamp,
Attributes = e.Tags.Select(a => new { Key = a.Key, Value = a.Value }).ToList(),
}).ToList(),
Links = activity.Links.Any()
? null
: activity.Links.Select(l =>
{
this.WriteLine($" {result.Key}: {result.Value}");
}
}
}
}

var resource = this.ParentProvider.GetResource();
if (resource != Resource.Empty)
if (l.Tags != null)
{
return new
{
TraceId = l.Context.TraceId,
SpanId = l.Context.SpanId,
Attributes = l.Tags.Select(a => new { Key = a.Key, Value = a.Value }).ToList(),
};
}

return null;
}).ToList(),
Resource = this.ParentProvider.GetResource() != Resource.Empty
? this.ParentProvider.GetResource().Attributes.Select(a => new { Key = a.Key, Value = a.Value })
.ToList()
: null,
},
};

var options = new JsonSerializerOptions
{
this.WriteLine("Resource associated with Activity:");
foreach (var resourceAttribute in resource.Attributes)
{
if (this.TagWriter.TryTransformTag(resourceAttribute, out var result))
{
this.WriteLine($" {result.Key}: {result.Value}");
}
}
}
WriteIndented = true,
#if NETSTANDARD || NETFRAMEWORK
IgnoreNullValues = true,
#else
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
#endif
Converters = { new JsonStringEnumConverter(), },
};

var json = JsonSerializer.Serialize(output, options);
this.WriteLine(json);

this.WriteLine(string.Empty);
}

return ExportResult.Success;
Expand Down