Skip to content

Commit a2679d5

Browse files
cijothomasrajkumar-rangarajKielek
authored
Improve Console Metric Exporter Output (#6388)
Co-authored-by: Rajkumar Rangaraj <[email protected]> Co-authored-by: Piotr Kiełkowicz <[email protected]>
1 parent 0ad46df commit a2679d5

File tree

1 file changed

+42
-40
lines changed

1 file changed

+42
-40
lines changed

src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ public ConsoleMetricExporter(ConsoleExporterOptions options)
1717

1818
public override ExportResult Export(in Batch<Metric> batch)
1919
{
20+
// Print Resource information once at the beginning of the batch
21+
var resource = this.ParentProvider.GetResource();
22+
if (resource != Resource.Empty)
23+
{
24+
this.WriteLine("Resource associated with Metrics:");
25+
foreach (var resourceAttribute in resource.Attributes)
26+
{
27+
if (this.TagWriter.TryTransformTag(resourceAttribute.Key, resourceAttribute.Value, out var result))
28+
{
29+
this.WriteLine($"\t{result.Key}: {result.Value}");
30+
}
31+
}
32+
}
33+
2034
foreach (var metric in batch)
2135
{
2236
var msg = new StringBuilder(Environment.NewLine);
@@ -25,7 +39,7 @@ public override ExportResult Export(in Batch<Metric> batch)
2539
#else
2640
msg.Append($"Metric Name: {metric.Name}");
2741
#endif
28-
if (string.IsNullOrEmpty(metric.Description))
42+
if (!string.IsNullOrEmpty(metric.Description))
2943
{
3044
#if NET
3145
msg.Append(CultureInfo.InvariantCulture, $", Description: {metric.Description}");
@@ -34,7 +48,7 @@ public override ExportResult Export(in Batch<Metric> batch)
3448
#endif
3549
}
3650

37-
if (string.IsNullOrEmpty(metric.Unit))
51+
if (!string.IsNullOrEmpty(metric.Unit))
3852
{
3953
#if NET
4054
msg.Append(CultureInfo.InvariantCulture, $", Unit: {metric.Unit}");
@@ -43,8 +57,34 @@ public override ExportResult Export(in Batch<Metric> batch)
4357
#endif
4458
}
4559

60+
#if NET
61+
msg.Append(CultureInfo.InvariantCulture, $", Metric Type: {metric.MetricType}");
62+
#else
63+
msg.Append($", Metric Type: {metric.MetricType}");
64+
#endif
65+
4666
this.WriteLine(msg.ToString());
4767

68+
// Print Instrumentation scope (Meter) information once per metric
69+
this.WriteLine("Instrumentation scope (Meter):");
70+
this.WriteLine($"\tName: {metric.MeterName}");
71+
if (!string.IsNullOrEmpty(metric.MeterVersion))
72+
{
73+
this.WriteLine($"\tVersion: {metric.MeterVersion}");
74+
}
75+
76+
if (metric.MeterTags?.Any() == true)
77+
{
78+
this.WriteLine("\tTags:");
79+
foreach (var meterTag in metric.MeterTags)
80+
{
81+
if (this.TagWriter.TryTransformTag(meterTag, out var result))
82+
{
83+
this.WriteLine($"\t\t{result.Key}: {result.Value}");
84+
}
85+
}
86+
}
87+
4888
foreach (ref readonly var metricPoint in metric.GetMetricPoints())
4989
{
5090
string valueDisplay = string.Empty;
@@ -226,12 +266,6 @@ public override ExportResult Export(in Batch<Metric> batch)
226266
msg.Append(' ');
227267
}
228268

229-
msg.AppendLine();
230-
#if NET
231-
msg.AppendLine(CultureInfo.InvariantCulture, $"Metric Type: {metric.MetricType}");
232-
#else
233-
msg.Append($"Metric Type: {metric.MetricType}");
234-
#endif
235269
msg.AppendLine();
236270
#if NET
237271
msg.Append(CultureInfo.InvariantCulture, $"Value: {valueDisplay}");
@@ -247,38 +281,6 @@ public override ExportResult Export(in Batch<Metric> batch)
247281
}
248282

249283
this.WriteLine(msg.ToString());
250-
251-
this.WriteLine("Instrumentation scope (Meter):");
252-
this.WriteLine($"\tName: {metric.MeterName}");
253-
if (!string.IsNullOrEmpty(metric.MeterVersion))
254-
{
255-
this.WriteLine($"\tVersion: {metric.MeterVersion}");
256-
}
257-
258-
if (metric.MeterTags?.Any() == true)
259-
{
260-
this.WriteLine("\tTags:");
261-
foreach (var meterTag in metric.MeterTags)
262-
{
263-
if (this.TagWriter.TryTransformTag(meterTag, out var result))
264-
{
265-
this.WriteLine($"\t\t{result.Key}: {result.Value}");
266-
}
267-
}
268-
}
269-
270-
var resource = this.ParentProvider.GetResource();
271-
if (resource != Resource.Empty)
272-
{
273-
this.WriteLine("Resource associated with Metric:");
274-
foreach (var resourceAttribute in resource.Attributes)
275-
{
276-
if (this.TagWriter.TryTransformTag(resourceAttribute.Key, resourceAttribute.Value, out var result))
277-
{
278-
this.WriteLine($"\t{result.Key}: {result.Value}");
279-
}
280-
}
281-
}
282284
}
283285
}
284286

0 commit comments

Comments
 (0)