I’m writing some proof-of-concept code, a prototype to get familiar with exporting metrics using the Open Telemetry libraries. I must have omitted one detail, because I see the initialization of my MeterProvider emitted to the console, but then I don’t see any metrics which I am accumulating.
I’m using this sample as a guide: https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/examples/Console/TestMetrics.cs
Here is my initialization code:
public static MeterProvider GetMeterProvider(string service, string version, string meterName, bool consoleExporter, bool OtlpExporter)
{
MeterProviderBuilder mpb = Sdk.CreateMeterProviderBuilder()
.ConfigureResource(r => r.AddService(service, null, version))
.AddMeter(meterName); // All instruments from this meter are enabled.
if (consoleExporter)
{
mpb.AddConsoleExporter((exporterOptions, metricReaderOptions) =>
{
exporterOptions.Targets = ConsoleExporterOutputTargets.Console;
metricReaderOptions.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds = 1000;
metricReaderOptions.TemporalityPreference = MetricReaderTemporalityPreference.Delta;
});
}
if (OtlpExporter)
{
<...snipped...>
}
return mpb.Build();
}
Here’s the function call. The return value, a MeterProvider, is assigned to a class member variable, to persist for the duration of the class instance. (In the code below, the prefix m_ designates a class member variable.)
m_meterProvider = TelemetryUtils.GetMeterProvider(c_Service, c_Version, "Prototype", true, false);
And here’s what I see:
After creating the MeterProvider, I create
m_meter = new Meter("Telemetry", "8.1");
m_counter = m_meter.CreateCounter<int>("counter", "things", "A count of things");
m_histogram = m_meter.CreateHistogram<int>("histogram");
I think that this might be my issue. I’m creating a Meter but I don’t see how this is associated with the MeterProvider? In the sample code there is a statement:
using var provider = providerBuilder.Build();
but I’m not sure what the function of this statement. In my code, the MeterProvider, which is the output of MeterProviderBuilder.Build() is the functional return of GetMeterProvider(); this is assigned to the class member variable m_meterProvider which persists for the duration of the class. Anyway, moving on…
Here are the measurement collection statements which execute inside of my processing loop. I even threw a ForceFlush statement in there. But no joy. I don’t see anything emitted to the console. What am I missing? Thanks for your advice!
var observableCounter = m_meter.CreateObservableGauge("gauge", () =>
{
return new List<Measurement<int>>()
{
new Measurement<int>(
9999999,
new KeyValuePair<string, object>("tag1", "value1")),
};
});
m_histogram?.Record(Counter);
m_histogram?.Record(
Counter * 100,
new KeyValuePair<string, object>("tag1", "value1"));
m_counter?.Add(Counter);
m_counter?.Add(
Counter * 100,
new KeyValuePair<string, object>("tag2", "value2"));
m_meterProvider.ForceFlush();