Improve log message for unsupported Instrument (#3124)

This commit is contained in:
Cijo Thomas 2022-03-31 10:11:25 -07:00 committed by GitHub
parent 1241c99f2c
commit c78769cbea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 5 deletions

View File

@ -54,8 +54,8 @@ class MyExporter : BaseExporter<Metric>
}
```
A demo exporter which simply writes metric name and metric point start time
, tags to the console is shown [here](./MyExporter.cs).
A demo exporter which simply writes metric name, metric point start time
and tags to the console is shown [here](./MyExporter.cs).
Apart from the exporter itself, you should also provide extension methods as
shown [here](./MyExporterExtensions.cs). This allows users to add the Exporter

View File

@ -207,7 +207,7 @@ namespace OpenTelemetry.Metrics
}
}
OpenTelemetrySdkEventSource.Log.MeterProviderSdkEvent($"Succeeded publishing Instrument = \"{instrument.Name}\" of Meter = \"{instrument.Meter.Name}\".");
OpenTelemetrySdkEventSource.Log.MeterProviderSdkEvent($"Completed publishing Instrument = \"{instrument.Name}\" of Meter = \"{instrument.Meter.Name}\".");
}
catch (Exception)
{
@ -272,7 +272,7 @@ namespace OpenTelemetry.Metrics
}
}
OpenTelemetrySdkEventSource.Log.MeterProviderSdkEvent($"Succeeded publishing Instrument = \"{instrument.Name}\" of Meter = \"{instrument.Meter.Name}\".");
OpenTelemetrySdkEventSource.Log.MeterProviderSdkEvent($"Completed publishing Instrument = \"{instrument.Name}\" of Meter = \"{instrument.Meter.Name}\".");
}
catch (Exception)
{

View File

@ -67,7 +67,21 @@ namespace OpenTelemetry.Metrics
}
else
{
var metric = new Metric(instrumentIdentity, this.Temporality, this.maxMetricPointsPerMetricStream);
Metric metric = null;
try
{
metric = new Metric(instrumentIdentity, this.Temporality, this.maxMetricPointsPerMetricStream);
}
catch (NotSupportedException nse)
{
// TODO: This allocates string even if none listening.
// Could be improved with separate Event.
// Also the message could call out what Instruments
// and types (eg: int, long etc) are supported.
OpenTelemetrySdkEventSource.Log.MetricInstrumentIgnored(metricName, instrument.Meter.Name, "Unsupported instrument. Details: " + nse.Message, "Switch to a supported instrument type.");
return null;
}
this.instrumentIdentityToMetric[instrumentIdentity] = metric;
this.metrics[index] = metric;
this.metricStreamNames.Add(metricStreamName);

View File

@ -18,7 +18,9 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Metrics;
using System.Linq;
using System.Threading;
using OpenTelemetry.Internal;
using OpenTelemetry.Tests;
using Xunit;
using Xunit.Abstractions;
@ -1306,6 +1308,30 @@ namespace OpenTelemetry.Metrics.Tests
counter.Add(10, new KeyValuePair<string, object>("key", "value"));
}
[Fact]
public void UnsupportedMetricInstrument()
{
using var meter = new Meter($"{Utils.GetCurrentMethodName()}");
var exportedItems = new List<Metric>();
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddMeter(meter.Name)
.AddInMemoryExporter(exportedItems)
.Build();
using (var inMemoryEventListener = new InMemoryEventListener(OpenTelemetrySdkEventSource.Log))
{
var counter = meter.CreateCounter<decimal>("counter");
counter.Add(1);
// This validates that we log InstrumentIgnored event
// and not something else.
Assert.Single(inMemoryEventListener.Events.Where((e) => e.EventId == 33));
}
meterProvider.ForceFlush(MaxTimeToAllowForFlush);
Assert.Empty(exportedItems);
}
private static void ValidateMetricPointTags(List<KeyValuePair<string, object>> expectedTags, ReadOnlyTagCollection actualTags)
{
int tagIndex = 0;