Improve Console Metric Exporter Output (#6388)

Co-authored-by: Rajkumar Rangaraj <rajrang@microsoft.com>
Co-authored-by: Piotr Kiełkowicz <pkiekowicz@splunk.com>
This commit is contained in:
Cijo Thomas 2025-07-21 03:50:00 -07:00 committed by GitHub
parent 0ad46df6cf
commit a2679d5a6d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 42 additions and 40 deletions

View File

@ -17,6 +17,20 @@ public class ConsoleMetricExporter : ConsoleExporter<Metric>
public override ExportResult Export(in Batch<Metric> batch)
{
// Print Resource information once at the beginning of the batch
var resource = this.ParentProvider.GetResource();
if (resource != Resource.Empty)
{
this.WriteLine("Resource associated with Metrics:");
foreach (var resourceAttribute in resource.Attributes)
{
if (this.TagWriter.TryTransformTag(resourceAttribute.Key, resourceAttribute.Value, out var result))
{
this.WriteLine($"\t{result.Key}: {result.Value}");
}
}
}
foreach (var metric in batch)
{
var msg = new StringBuilder(Environment.NewLine);
@ -25,7 +39,7 @@ public class ConsoleMetricExporter : ConsoleExporter<Metric>
#else
msg.Append($"Metric Name: {metric.Name}");
#endif
if (string.IsNullOrEmpty(metric.Description))
if (!string.IsNullOrEmpty(metric.Description))
{
#if NET
msg.Append(CultureInfo.InvariantCulture, $", Description: {metric.Description}");
@ -34,7 +48,7 @@ public class ConsoleMetricExporter : ConsoleExporter<Metric>
#endif
}
if (string.IsNullOrEmpty(metric.Unit))
if (!string.IsNullOrEmpty(metric.Unit))
{
#if NET
msg.Append(CultureInfo.InvariantCulture, $", Unit: {metric.Unit}");
@ -43,8 +57,34 @@ public class ConsoleMetricExporter : ConsoleExporter<Metric>
#endif
}
#if NET
msg.Append(CultureInfo.InvariantCulture, $", Metric Type: {metric.MetricType}");
#else
msg.Append($", Metric Type: {metric.MetricType}");
#endif
this.WriteLine(msg.ToString());
// Print Instrumentation scope (Meter) information once per metric
this.WriteLine("Instrumentation scope (Meter):");
this.WriteLine($"\tName: {metric.MeterName}");
if (!string.IsNullOrEmpty(metric.MeterVersion))
{
this.WriteLine($"\tVersion: {metric.MeterVersion}");
}
if (metric.MeterTags?.Any() == true)
{
this.WriteLine("\tTags:");
foreach (var meterTag in metric.MeterTags)
{
if (this.TagWriter.TryTransformTag(meterTag, out var result))
{
this.WriteLine($"\t\t{result.Key}: {result.Value}");
}
}
}
foreach (ref readonly var metricPoint in metric.GetMetricPoints())
{
string valueDisplay = string.Empty;
@ -226,12 +266,6 @@ public class ConsoleMetricExporter : ConsoleExporter<Metric>
msg.Append(' ');
}
msg.AppendLine();
#if NET
msg.AppendLine(CultureInfo.InvariantCulture, $"Metric Type: {metric.MetricType}");
#else
msg.Append($"Metric Type: {metric.MetricType}");
#endif
msg.AppendLine();
#if NET
msg.Append(CultureInfo.InvariantCulture, $"Value: {valueDisplay}");
@ -247,38 +281,6 @@ public class ConsoleMetricExporter : ConsoleExporter<Metric>
}
this.WriteLine(msg.ToString());
this.WriteLine("Instrumentation scope (Meter):");
this.WriteLine($"\tName: {metric.MeterName}");
if (!string.IsNullOrEmpty(metric.MeterVersion))
{
this.WriteLine($"\tVersion: {metric.MeterVersion}");
}
if (metric.MeterTags?.Any() == true)
{
this.WriteLine("\tTags:");
foreach (var meterTag in metric.MeterTags)
{
if (this.TagWriter.TryTransformTag(meterTag, out var result))
{
this.WriteLine($"\t\t{result.Key}: {result.Value}");
}
}
}
var resource = this.ParentProvider.GetResource();
if (resource != Resource.Empty)
{
this.WriteLine("Resource associated with Metric:");
foreach (var resourceAttribute in resource.Attributes)
{
if (this.TagWriter.TryTransformTag(resourceAttribute.Key, resourceAttribute.Value, out var result))
{
this.WriteLine($"\t{result.Key}: {result.Value}");
}
}
}
}
}