forked from open-telemetry/opentelemetry-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConsoleActivityExporter.cs
146 lines (131 loc) · 5.67 KB
/
ConsoleActivityExporter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// <copyright file="ConsoleActivityExporter.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.Resources;
using OpenTelemetry.Trace;
namespace OpenTelemetry.Exporter;
public class ConsoleActivityExporter : ConsoleExporter<Activity>
{
public ConsoleActivityExporter(ConsoleExporterOptions options)
: base(options)
{
}
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))
{
this.WriteLine($"Activity.TraceState: {activity.TraceStateString}");
}
if (activity.ParentSpanId != default)
{
this.WriteLine($"Activity.ParentSpanId: {activity.ParentSpanId}");
}
this.WriteLine($"Activity.ActivitySourceName: {activity.Source.Name}");
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 (ConsoleTagTransformer.Instance.TryTransformTag(tag, out var result))
{
this.WriteLine($" {result}");
}
}
}
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())
{
this.WriteLine($" {activityEvent.Name} [{activityEvent.Timestamp}]");
foreach (ref readonly var attribute in activityEvent.EnumerateTagObjects())
{
if (ConsoleTagTransformer.Instance.TryTransformTag(attribute, out var result))
{
this.WriteLine($" {result}");
}
}
}
}
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 (ConsoleTagTransformer.Instance.TryTransformTag(attribute, out var result))
{
this.WriteLine($" {result}");
}
}
}
}
var resource = this.ParentProvider.GetResource();
if (resource != Resource.Empty)
{
this.WriteLine("Resource associated with Activity:");
foreach (var resourceAttribute in resource.Attributes)
{
if (ConsoleTagTransformer.Instance.TryTransformTag(resourceAttribute, out var result))
{
this.WriteLine($" {result}");
}
}
}
this.WriteLine(string.Empty);
}
return ExportResult.Success;
}
}