diff --git a/docs/metrics/extending-the-sdk/MyExporter.cs b/docs/metrics/extending-the-sdk/MyExporter.cs index 997e73d5c..db956281a 100644 --- a/docs/metrics/extending-the-sdk/MyExporter.cs +++ b/docs/metrics/extending-the-sdk/MyExporter.cs @@ -35,14 +35,23 @@ internal class MyExporter : BaseExporter using var scope = SuppressInstrumentationScope.Begin(); var sb = new StringBuilder(); - foreach (var record in batch) + foreach (var metric in batch) { if (sb.Length > 0) { sb.Append(", "); } - sb.Append($"{record}"); + sb.Append($"{metric.Name}"); + + foreach (ref readonly var metricPoint in metric.GetMetricPoints()) + { + sb.Append($"{metricPoint.StartTime}"); + foreach (var metricPointTag in metricPoint.Tags) + { + sb.Append($"{metricPointTag.Key} {metricPointTag.Value}"); + } + } } Console.WriteLine($"{this.name}.Export([{sb}])"); diff --git a/docs/metrics/extending-the-sdk/README.md b/docs/metrics/extending-the-sdk/README.md index 58a126b27..b1eb86eb3 100644 --- a/docs/metrics/extending-the-sdk/README.md +++ b/docs/metrics/extending-the-sdk/README.md @@ -7,14 +7,68 @@ ## Exporter -TBD +OpenTelemetry .NET SDK has provided the following built-in metric exporters: + +* [Console](../../../src/OpenTelemetry.Exporter.Console/README.md) +* [InMemory](../../../src/OpenTelemetry.Exporter.InMemory/README.md) +* [OpenTelemetryProtocol](../../../src/OpenTelemetry.Exporter.OpenTelemetryProtocol/README.md) +* [Prometheus](../../../src/OpenTelemetry.Exporter.Prometheus/README.md) + +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` (which + belongs to the [OpenTelemetry](../../../src/OpenTelemetry/README.md) package) + and implement the `Export` method. +* Exporters can optionally implement the `OnShutdown` method. +* Exporters should not throw exceptions from `Export` and + `OnShutdown`. +* 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 each `Metric` + can contain 1 or more `MetricPoint`s. +* Exporters should use `Activity.TagObjects` collection instead of + `Activity.Tags` to obtain the full set of attributes (tags). +* Exporters should use `ParentProvider.GetResource()` to get the `Resource` + associated with the provider. + +```csharp +class MyExporter : BaseExporter +{ + public override ExportResult Export(in Batch 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 and metric point start time +, 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 +to the `MeterProvider` as shown in the example [here](./Program.cs). ## Reader -TBD +Not supported. ## Exemplar -TBD +Not supported. ## References