export method
- Iterable<
MetricFamilySamples> families
Exports families to the configured endpoint as a single OTLP
ExportMetricsServiceRequest.
Throws OTelExportException when the request fails or the collector responds with a non-2xx status. Callers that export periodically should catch and log; a failed export must never take down metric recording.
Implementation
Future<void> export(Iterable<MetricFamilySamples> families) async {
if (_closed) {
throw OTelExportException('Exporter is closed');
}
final body = jsonEncode(buildMetricsPayload(families));
final http.Response response;
try {
response = await _httpClient
.post(
_endpoint,
headers: {'Content-Type': 'application/json', ..._headers},
body: body,
)
.timeout(_requestTimeout);
} on TimeoutException {
throw OTelExportException(
'OTLP export to $_endpoint timed out after $_requestTimeout',
);
} on Object catch (e) {
throw OTelExportException('OTLP export to $_endpoint failed: $e');
}
if (response.statusCode < 200 || response.statusCode >= 300) {
throw OTelExportException(
'OTLP export to $_endpoint was rejected',
statusCode: response.statusCode,
);
}
_logger.debug(
'Exported ${body.length} bytes of OTLP metrics to $_endpoint '
'(HTTP ${response.statusCode})',
);
}