|
|
||
|---|---|---|
| .. | ||
| MyExporter.cs | ||
| MyExporterExtensions.cs | ||
| Program.cs | ||
| README.md | ||
| extending-the-sdk.csproj | ||
README.md
Extending the OpenTelemetry .NET SDK
Exporter
OpenTelemetry .NET SDK has provided the following built-in metric exporters:
Custom exporters can be implemented to send telemetry data to places which are not covered by the built-in exporters:
- Exporters should derive from
OpenTelemetry.BaseExporter<Metric>(which belongs to the OpenTelemetry package) and implement theExportmethod. - Exporters can optionally implement the
OnShutdownmethod. - Exporters should not throw exceptions from
ExportandOnShutdown. - Exporters are responsible for any retry logic needed by the scenario. The SDK does not implement any retry logic.
- Exporters should avoid generating telemetry and causing live-loop, this can be
done via
OpenTelemetry.SuppressInstrumentationScope. - Exporters receives a batch of
Metric, and eachMetriccan contain 1 or moreMetricPoints. - Exporters should use
ParentProvider.GetResource()to get theResourceassociated with the provider.
class MyExporter : BaseExporter<Metric>
{
public override ExportResult Export(in Batch<Metric> batch)
{
using var scope = SuppressInstrumentationScope.Begin();
foreach (var metric in batch)
{
Console.WriteLine($"Export: {metric.metric}");
foreach (ref readonly var metricPoint in metric.GetMetricPoints())
{
Console.WriteLine($"Export: {metricPoint.StartTime}");
}
}
return ExportResult.Success;
}
}
A demo exporter which simply writes metric name, metric point start time and tags to the console is shown here.
Apart from the exporter itself, you should also provide extension methods as
shown here. This allows users to add the Exporter
to the MeterProvider as shown in the example here.
Reader
Not supported.
Exemplar
Not supported.