Skip to content

Commit

Permalink
fix(meshmetric): add missing timestamp in mapper (backport of #10966) (
Browse files Browse the repository at this point in the history
…#10980)

fix(meshmetric): add missing timestamp in mapper (#10966)

* fix(meshmetric): add missing timestamp



* fix(meshmetric): use times in UTC



---------

Signed-off-by: slonka <slonka@users.noreply.github.com>
Co-authored-by: Krzysztof Słonka <slonka@users.noreply.github.com>
  • Loading branch information
kumahq[bot] and slonka authored Jul 25, 2024
1 parent c4795f0 commit 0978f93
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 88 deletions.
31 changes: 22 additions & 9 deletions app/kuma-dp/pkg/dataplane/metrics/metrics_format_mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package metrics

import (
"math"
"time"

io_prometheus_client "github.com/prometheus/client_model/go"
"go.opentelemetry.io/otel/attribute"
Expand All @@ -10,7 +11,7 @@ import (
"github.com/kumahq/kuma/pkg/util/pointer"
)

func FromPrometheusMetrics(appMetrics map[string]*io_prometheus_client.MetricFamily, mesh string, dataplane string, service string, extraLabels map[string]string) []metricdata.Metrics {
func FromPrometheusMetrics(appMetrics map[string]*io_prometheus_client.MetricFamily, mesh string, dataplane string, service string, extraLabels map[string]string, requestTime time.Time) []metricdata.Metrics {
extraAttributes := extraAttributesFrom(mesh, dataplane, service, extraLabels)

var openTelemetryMetrics []metricdata.Metrics
Expand All @@ -23,22 +24,22 @@ func FromPrometheusMetrics(appMetrics map[string]*io_prometheus_client.MetricFam
switch prometheusMetric.GetType() {
case io_prometheus_client.MetricType_GAUGE:
otelMetric.Data = metricdata.Gauge[float64]{
DataPoints: gaugeDataPoints(prometheusMetric.Metric, extraAttributes),
DataPoints: gaugeDataPoints(prometheusMetric.Metric, extraAttributes, requestTime),
}
case io_prometheus_client.MetricType_SUMMARY:
otelMetric.Data = metricdata.Summary{
DataPoints: summaryDataPoints(prometheusMetric.Metric, extraAttributes),
DataPoints: summaryDataPoints(prometheusMetric.Metric, extraAttributes, requestTime),
}
case io_prometheus_client.MetricType_COUNTER:
otelMetric.Data = metricdata.Sum[float64]{
IsMonotonic: true,
Temporality: metricdata.CumulativeTemporality,
DataPoints: counterDataPoints(prometheusMetric.Metric, extraAttributes),
DataPoints: counterDataPoints(prometheusMetric.Metric, extraAttributes, requestTime),
}
case io_prometheus_client.MetricType_HISTOGRAM:
otelMetric.Data = metricdata.Histogram[float64]{
Temporality: metricdata.CumulativeTemporality,
DataPoints: histogramDataPoints(prometheusMetric.Metric, extraAttributes),
DataPoints: histogramDataPoints(prometheusMetric.Metric, extraAttributes, requestTime),
}
default:
log.Info("got unsupported metric type", "type", prometheusMetric.Type)
Expand All @@ -49,24 +50,26 @@ func FromPrometheusMetrics(appMetrics map[string]*io_prometheus_client.MetricFam
return openTelemetryMetrics
}

func gaugeDataPoints(prometheusData []*io_prometheus_client.Metric, extraAttributes []attribute.KeyValue) []metricdata.DataPoint[float64] {
func gaugeDataPoints(prometheusData []*io_prometheus_client.Metric, extraAttributes []attribute.KeyValue, requestTime time.Time) []metricdata.DataPoint[float64] {
var dataPoints []metricdata.DataPoint[float64]
for _, metric := range prometheusData {
attributes := createOpenTelemetryAttributes(metric.Label, extraAttributes)
dataPoints = append(dataPoints, metricdata.DataPoint[float64]{
Attributes: attributes,
Time: getTimeOrFallback(metric.TimestampMs, requestTime),
Value: metric.Gauge.GetValue(),
})
}
return dataPoints
}

func summaryDataPoints(prometheusData []*io_prometheus_client.Metric, extraAttributes []attribute.KeyValue) []metricdata.SummaryDataPoint {
func summaryDataPoints(prometheusData []*io_prometheus_client.Metric, extraAttributes []attribute.KeyValue, requestTime time.Time) []metricdata.SummaryDataPoint {
var dataPoints []metricdata.SummaryDataPoint
for _, metric := range prometheusData {
attributes := createOpenTelemetryAttributes(metric.Label, extraAttributes)
dataPoints = append(dataPoints, metricdata.SummaryDataPoint{
Attributes: attributes,
Time: getTimeOrFallback(metric.TimestampMs, requestTime),
QuantileValues: toOpenTelemetryQuantile(metric.Summary.Quantile),
Sum: pointer.Deref(metric.Summary.SampleSum),
Count: pointer.Deref(metric.Summary.SampleCount),
Expand All @@ -75,19 +78,20 @@ func summaryDataPoints(prometheusData []*io_prometheus_client.Metric, extraAttri
return dataPoints
}

func counterDataPoints(prometheusData []*io_prometheus_client.Metric, extraAttributes []attribute.KeyValue) []metricdata.DataPoint[float64] {
func counterDataPoints(prometheusData []*io_prometheus_client.Metric, extraAttributes []attribute.KeyValue, requestTime time.Time) []metricdata.DataPoint[float64] {
var dataPoints []metricdata.DataPoint[float64]
for _, metric := range prometheusData {
attributes := createOpenTelemetryAttributes(metric.Label, extraAttributes)
dataPoints = append(dataPoints, metricdata.DataPoint[float64]{
Attributes: attributes,
Time: getTimeOrFallback(metric.TimestampMs, requestTime),
Value: metric.Counter.GetValue(),
})
}
return dataPoints
}

func histogramDataPoints(prometheusData []*io_prometheus_client.Metric, extraAttributes []attribute.KeyValue) []metricdata.HistogramDataPoint[float64] {
func histogramDataPoints(prometheusData []*io_prometheus_client.Metric, extraAttributes []attribute.KeyValue, requestTime time.Time) []metricdata.HistogramDataPoint[float64] {
var dataPoints []metricdata.HistogramDataPoint[float64]
for _, metric := range prometheusData {
attributes := createOpenTelemetryAttributes(metric.Label, extraAttributes)
Expand All @@ -102,6 +106,7 @@ func histogramDataPoints(prometheusData []*io_prometheus_client.Metric, extraAtt
}

dataPoints = append(dataPoints, metricdata.HistogramDataPoint[float64]{
Time: getTimeOrFallback(metric.TimestampMs, requestTime),
Attributes: attributes,
Count: metric.Histogram.GetSampleCount(),
Sum: metric.Histogram.GetSampleSum(),
Expand All @@ -112,6 +117,14 @@ func histogramDataPoints(prometheusData []*io_prometheus_client.Metric, extraAtt
return dataPoints
}

func getTimeOrFallback(timestampMs *int64, fallback time.Time) time.Time {
if timestampMs != nil {
return time.UnixMilli(*timestampMs).UTC()
} else {
return fallback
}
}

func createOpenTelemetryAttributes(labels []*io_prometheus_client.LabelPair, extraAttributes []attribute.KeyValue) attribute.Set {
var attributes []attribute.KeyValue
for _, label := range labels {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path"
"sort"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand All @@ -23,7 +24,7 @@ var _ = Describe("Metrics format mapper", func() {
// when
metrics, err := AggregatedOtelMutator()(input)
Expect(err).ToNot(HaveOccurred())
openTelemetryMetrics := FromPrometheusMetrics(metrics, "default", "dpp-1", "test-service", map[string]string{"extraLabel": "test"})
openTelemetryMetrics := FromPrometheusMetrics(metrics, "default", "dpp-1", "test-service", map[string]string{"extraLabel": "test"}, time.Date(2024, 1, 1, 1, 1, 1, 1, time.UTC))

// then
sort.SliceStable(openTelemetryMetrics, func(i, j int) bool {
Expand Down
4 changes: 3 additions & 1 deletion app/kuma-dp/pkg/dataplane/metrics/metrics_producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"net/url"
"sync"
"time"

"go.opentelemetry.io/otel/sdk/instrumentation"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
Expand Down Expand Up @@ -100,6 +101,7 @@ func (ap *AggregatedProducer) fetchStats(ctx context.Context, app ApplicationToS
return nil
}
defer resp.Body.Close()
requestTime := time.Now().UTC()

metricsFromApplication, err := app.MeshMetricMutator(resp.Body)
if err != nil {
Expand All @@ -110,7 +112,7 @@ func (ap *AggregatedProducer) fetchStats(ctx context.Context, app ApplicationToS
Scope: instrumentation.Scope{
Name: app.Name,
},
Metrics: FromPrometheusMetrics(metricsFromApplication, ap.mesh, ap.dataplane, ap.service, app.ExtraLabels),
Metrics: FromPrometheusMetrics(metricsFromApplication, ap.mesh, ap.dataplane, ap.service, app.ExtraLabels, requestTime),
}
}

Expand Down
50 changes: 25 additions & 25 deletions app/kuma-dp/pkg/dataplane/metrics/testdata/otel/counter.golden.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
}
],
"StartTime": "0001-01-01T00:00:00Z",
"Time": "0001-01-01T00:00:00Z",
"Time": "2014-03-17T14:26:03Z",
"Value": 11
},
{
Expand Down Expand Up @@ -86,7 +86,7 @@
}
],
"StartTime": "0001-01-01T00:00:00Z",
"Time": "0001-01-01T00:00:00Z",
"Time": "2024-01-01T01:01:01.000000001Z",
"Value": 12
},
{
Expand Down Expand Up @@ -128,7 +128,7 @@
}
],
"StartTime": "0001-01-01T00:00:00Z",
"Time": "0001-01-01T00:00:00Z",
"Time": "2024-01-01T01:01:01.000000001Z",
"Value": 13
},
{
Expand Down Expand Up @@ -170,7 +170,7 @@
}
],
"StartTime": "0001-01-01T00:00:00Z",
"Time": "0001-01-01T00:00:00Z",
"Time": "2024-01-01T01:01:01.000000001Z",
"Value": 101
},
{
Expand Down Expand Up @@ -212,7 +212,7 @@
}
],
"StartTime": "0001-01-01T00:00:00Z",
"Time": "0001-01-01T00:00:00Z",
"Time": "2024-01-01T01:01:01.000000001Z",
"Value": 102
},
{
Expand Down Expand Up @@ -254,7 +254,7 @@
}
],
"StartTime": "0001-01-01T00:00:00Z",
"Time": "0001-01-01T00:00:00Z",
"Time": "2024-01-01T01:01:01.000000001Z",
"Value": 103
},
{
Expand Down Expand Up @@ -296,7 +296,7 @@
}
],
"StartTime": "0001-01-01T00:00:00Z",
"Time": "0001-01-01T00:00:00Z",
"Time": "2024-01-01T01:01:01.000000001Z",
"Value": 14
},
{
Expand Down Expand Up @@ -338,7 +338,7 @@
}
],
"StartTime": "0001-01-01T00:00:00Z",
"Time": "0001-01-01T00:00:00Z",
"Time": "2024-01-01T01:01:01.000000001Z",
"Value": 15
},
{
Expand Down Expand Up @@ -380,7 +380,7 @@
}
],
"StartTime": "0001-01-01T00:00:00Z",
"Time": "0001-01-01T00:00:00Z",
"Time": "2024-01-01T01:01:01.000000001Z",
"Value": 16
},
{
Expand Down Expand Up @@ -422,7 +422,7 @@
}
],
"StartTime": "0001-01-01T00:00:00Z",
"Time": "0001-01-01T00:00:00Z",
"Time": "2024-01-01T01:01:01.000000001Z",
"Value": 17
},
{
Expand Down Expand Up @@ -464,7 +464,7 @@
}
],
"StartTime": "0001-01-01T00:00:00Z",
"Time": "0001-01-01T00:00:00Z",
"Time": "2024-01-01T01:01:01.000000001Z",
"Value": 18
},
{
Expand Down Expand Up @@ -506,7 +506,7 @@
}
],
"StartTime": "0001-01-01T00:00:00Z",
"Time": "0001-01-01T00:00:00Z",
"Time": "2024-01-01T01:01:01.000000001Z",
"Value": 19
}
],
Expand Down Expand Up @@ -559,7 +559,7 @@
}
],
"StartTime": "0001-01-01T00:00:00Z",
"Time": "0001-01-01T00:00:00Z",
"Time": "2024-01-01T01:01:01.000000001Z",
"Value": 1
},
{
Expand Down Expand Up @@ -601,7 +601,7 @@
}
],
"StartTime": "0001-01-01T00:00:00Z",
"Time": "0001-01-01T00:00:00Z",
"Time": "2024-01-01T01:01:01.000000001Z",
"Value": 2
},
{
Expand Down Expand Up @@ -643,7 +643,7 @@
}
],
"StartTime": "0001-01-01T00:00:00Z",
"Time": "0001-01-01T00:00:00Z",
"Time": "2024-01-01T01:01:01.000000001Z",
"Value": 3
},
{
Expand Down Expand Up @@ -685,7 +685,7 @@
}
],
"StartTime": "0001-01-01T00:00:00Z",
"Time": "0001-01-01T00:00:00Z",
"Time": "2024-01-01T01:01:01.000000001Z",
"Value": 201
},
{
Expand Down Expand Up @@ -727,7 +727,7 @@
}
],
"StartTime": "0001-01-01T00:00:00Z",
"Time": "0001-01-01T00:00:00Z",
"Time": "2024-01-01T01:01:01.000000001Z",
"Value": 202
},
{
Expand Down Expand Up @@ -769,7 +769,7 @@
}
],
"StartTime": "0001-01-01T00:00:00Z",
"Time": "0001-01-01T00:00:00Z",
"Time": "2024-01-01T01:01:01.000000001Z",
"Value": 203
},
{
Expand Down Expand Up @@ -811,7 +811,7 @@
}
],
"StartTime": "0001-01-01T00:00:00Z",
"Time": "0001-01-01T00:00:00Z",
"Time": "2024-01-01T01:01:01.000000001Z",
"Value": 4
},
{
Expand Down Expand Up @@ -853,7 +853,7 @@
}
],
"StartTime": "0001-01-01T00:00:00Z",
"Time": "0001-01-01T00:00:00Z",
"Time": "2024-01-01T01:01:01.000000001Z",
"Value": 5
},
{
Expand Down Expand Up @@ -895,7 +895,7 @@
}
],
"StartTime": "0001-01-01T00:00:00Z",
"Time": "0001-01-01T00:00:00Z",
"Time": "2024-01-01T01:01:01.000000001Z",
"Value": 6
},
{
Expand Down Expand Up @@ -937,7 +937,7 @@
}
],
"StartTime": "0001-01-01T00:00:00Z",
"Time": "0001-01-01T00:00:00Z",
"Time": "2024-01-01T01:01:01.000000001Z",
"Value": 7
},
{
Expand Down Expand Up @@ -979,7 +979,7 @@
}
],
"StartTime": "0001-01-01T00:00:00Z",
"Time": "0001-01-01T00:00:00Z",
"Time": "2024-01-01T01:01:01.000000001Z",
"Value": 8
},
{
Expand Down Expand Up @@ -1021,7 +1021,7 @@
}
],
"StartTime": "0001-01-01T00:00:00Z",
"Time": "0001-01-01T00:00:00Z",
"Time": "2024-01-01T01:01:01.000000001Z",
"Value": 9
}
],
Expand Down Expand Up @@ -1074,7 +1074,7 @@
}
],
"StartTime": "0001-01-01T00:00:00Z",
"Time": "0001-01-01T00:00:00Z",
"Time": "2024-01-01T01:01:01.000000001Z",
"Value": 10
}
],
Expand Down
2 changes: 1 addition & 1 deletion app/kuma-dp/pkg/dataplane/metrics/testdata/otel/counter.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# TYPE envoy_cluster_assignment_stale counter
envoy_cluster_assignment_stale{envoy_cluster_name="access_log_sink"} 11
envoy_cluster_assignment_stale{envoy_cluster_name="access_log_sink"} 11 1395066363000
envoy_cluster_assignment_stale{envoy_cluster_name="ads_cluster"} 12
envoy_cluster_assignment_stale{envoy_cluster_name="demo-client"} 13
envoy_cluster_assignment_stale{envoy_cluster_name="echo-server_kuma-test_svc_8080-94bcdea309bceaff"} 101
Expand Down
Loading

0 comments on commit 0978f93

Please sign in to comment.