buildMetricsPayload method

Map<String, dynamic> buildMetricsPayload(
  1. Iterable<MetricFamilySamples> families, {
  2. DateTime? timestamp,
  3. DateTime? startTimestamp,
})

Builds the OTLP ExportMetricsServiceRequest JSON document for families.

timestamp overrides the data point timestamp (defaults to now); startTimestamp overrides the cumulative start timestamp (defaults to exporter construction time). Exposed for testing and for callers that want to control the reported time window.

Implementation

Map<String, dynamic> buildMetricsPayload(
  Iterable<MetricFamilySamples> families, {
  DateTime? timestamp,
  DateTime? startTimestamp,
}) {
  final timeUnixNano =
      (timestamp ?? DateTime.now()).microsecondsSinceEpoch * 1000;
  final startTimeUnixNano = startTimestamp != null
      ? startTimestamp.microsecondsSinceEpoch * 1000
      : _startTimeUnixNano;

  final metrics = <Map<String, dynamic>>[];
  for (final family in families) {
    final metric = _convertFamily(family, startTimeUnixNano, timeUnixNano);
    if (metric != null) {
      metrics.add(metric);
    }
  }

  return {
    'resourceMetrics': [
      {
        'resource': {
          'attributes': [_attribute('service.name', _serviceName)],
        },
        'scopeMetrics': [
          {
            'scope': {'name': 'dart_ipfs'},
            'metrics': metrics,
          },
        ],
      },
    ],
  };
}