Skip to content

Commit 20f76ff

Browse files
authored
Handle untyped/unknown metric types (#56)
* Update to Prometheus 2.5.0 Signed-off-by: Fabian Reinartz <freinartz@google.com> * Update pkg/textparse to v2.5 Signed-off-by: Fabian Reinartz <freinartz@google.com> * Handle Prometheus 2.5 unknown metric type Signed-off-by: Fabian Reinartz <freinartz@google.com> * Revert to 2.4.3 until upstream is fixed Signed-off-by: Fabian Reinartz <freinartz@google.com>
1 parent a9247e9 commit 20f76ff

File tree

21 files changed

+1399
-183
lines changed

21 files changed

+1399
-183
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ can be used as a reference for setup:
4949

5050
The matrix below lists the versions of Prometheus Server and other dependencies that have been qualified to work with releases of `stackdriver-prometheus-sidecar`.
5151

52-
| sidecar version | prometheus version |
53-
| --- | --- |
54-
| 0.2.x | 2.4.3 |
52+
| sidecar version | **Prometheus 2.4.3** | **Prometheus 2.5.x** |
53+
|------------|------------------|-------------------|
54+
| **0.2.x** || - |
55+
| **master** |||
5556

5657
## Source Code Headers
5758

cmd/stackdriver-prometheus-sidecar/main.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,8 +613,11 @@ func parseConfigFile(filename string) (map[string]string, []scrape.MetricMetadat
613613
var staticMetadata []scrape.MetricMetadata
614614
for _, sm := range fc.StaticMetadata {
615615
switch sm.Type {
616-
case textparse.MetricTypeCounter, textparse.MetricTypeGauge,
617-
textparse.MetricTypeHistogram, textparse.MetricTypeSummary, textparse.MetricTypeUntyped:
616+
case metadata.MetricTypeUntyped:
617+
// Convert "untyped" to the "unknown" type used internally as of Prometheus 2.5.
618+
sm.Type = textparse.MetricTypeUnknown
619+
case textparse.MetricTypeCounter, textparse.MetricTypeGauge, textparse.MetricTypeHistogram,
620+
textparse.MetricTypeSummary, textparse.MetricTypeUnknown:
618621
default:
619622
return nil, nil, errors.Errorf("invalid metric type %q", sm.Type)
620623
}

metadata/cache.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ type Cache struct {
4646
// the target metadata endpoint.
4747
const DefaultEndpointPath = "/api/v1/targets/metadata"
4848

49+
// The old metric type value for textparse.MetricTypeUnknown that is used in
50+
// Prometheus 2.4 and earlier.
51+
const MetricTypeUntyped = "untyped"
52+
4953
// NewCache returns a new cache that gets populated by the metadata endpoint
5054
// at the given URL.
5155
// It uses the default endpoint path if no specific path is provided.
@@ -176,6 +180,10 @@ func (c *Cache) fetchMetric(ctx context.Context, job, instance, metric string) (
176180
}
177181
d := apiResp.Data[0]
178182

183+
// Convert legacy "untyped" type used before Prometheus 2.5.
184+
if d.Type == MetricTypeUntyped {
185+
d.Type = textparse.MetricTypeUnknown
186+
}
179187
return &metadataEntry{
180188
MetricMetadata: scrape.MetricMetadata{
181189
Metric: metric,
@@ -212,6 +220,10 @@ func (c *Cache) fetchBatch(ctx context.Context, job, instance string) (map[strin
212220
result := make(map[string]*metadataEntry, len(apiResp.Data)+len(internalMetrics))
213221

214222
for _, md := range apiResp.Data {
223+
// Convert legacy "untyped" type used before Prometheus 2.5.
224+
if md.Type == MetricTypeUntyped {
225+
md.Type = textparse.MetricTypeUnknown
226+
}
215227
result[md.Metric] = &metadataEntry{
216228
MetricMetadata: scrape.MetricMetadata{
217229
Metric: md.Metric,

metadata/cache_test.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ func TestCache_Get(t *testing.T) {
3535
{Metric: "metric2", Type: textparse.MetricTypeGauge, Help: "help_metric2"},
3636
{Metric: "metric3", Type: textparse.MetricTypeHistogram, Help: "help_metric3"},
3737
{Metric: "metric4", Type: textparse.MetricTypeSummary, Help: "help_metric4"},
38-
{Metric: "metric5", Type: textparse.MetricTypeUntyped, Help: "help_metric5"},
38+
{Metric: "metric5", Type: textparse.MetricTypeUnknown, Help: "help_metric5"},
39+
{Metric: "metric6", Type: MetricTypeUntyped, Help: "help_metric6"},
3940
}
4041
var handler func(qMetric, qMatch string) *apiResponse
4142

@@ -129,6 +130,22 @@ func TestCache_Get(t *testing.T) {
129130
}
130131
expect(metrics[4], md)
131132

133+
// Test "untyped" metric type from Prometheus 2.4.
134+
handler = func(qMetric, qMatch string) *apiResponse {
135+
if qMetric != "metric6" {
136+
t.Fatalf("unexpected metric %q in request", qMetric)
137+
}
138+
if qMatch != `{job="prometheus",instance="localhost:9090"}` {
139+
t.Fatalf("unexpected matcher %q in request", qMatch)
140+
}
141+
return &apiResponse{Status: "success", Data: metrics[5:6]}
142+
}
143+
md, err = c.Get(ctx, "prometheus", "localhost:9090", "metric6")
144+
if err != nil {
145+
t.Fatal(err)
146+
}
147+
expect(apiMetadata{Metric: "metric6", Type: textparse.MetricTypeUnknown, Help: "help_metric6"}, md)
148+
132149
// The scrape layer's metrics should not fire off requests.
133150
md, err = c.Get(ctx, "prometheus", "localhost:9090", "up")
134151
if err != nil {

retrieval/series_cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ func (c *seriesCache) refresh(ctx context.Context, ref uint64) error {
407407
case textparse.MetricTypeCounter:
408408
ts.MetricKind = metric_pb.MetricDescriptor_CUMULATIVE
409409
ts.ValueType = metric_pb.MetricDescriptor_DOUBLE
410-
case textparse.MetricTypeGauge, textparse.MetricTypeUntyped:
410+
case textparse.MetricTypeGauge, textparse.MetricTypeUnknown:
411411
ts.MetricKind = metric_pb.MetricDescriptor_GAUGE
412412
ts.ValueType = metric_pb.MetricDescriptor_DOUBLE
413413
case textparse.MetricTypeSummary:

retrieval/transform.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (b *sampleBuilder) next(ctx context.Context, samples []tsdb.RefSample) (*mo
7070
point.Interval.StartTime = getTimestamp(resetTimestamp)
7171
point.Value = &monitoring_pb.TypedValue{&monitoring_pb.TypedValue_DoubleValue{v}}
7272

73-
case textparse.MetricTypeGauge, textparse.MetricTypeUntyped:
73+
case textparse.MetricTypeGauge, textparse.MetricTypeUnknown:
7474
point.Value = &monitoring_pb.TypedValue{&monitoring_pb.TypedValue_DoubleValue{sample.V}}
7575

7676
case textparse.MetricTypeSummary:

retrieval/transform_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func TestSampleBuilder(t *testing.T) {
9494
metadata: metadataMap{
9595
"job1/instance1/metric1": &scrape.MetricMetadata{Type: textparse.MetricTypeGauge, Metric: "metric1"},
9696
"job1/instance1/metric2": &scrape.MetricMetadata{Type: textparse.MetricTypeCounter, Metric: "metric2"},
97-
"job1/instance1/labelnum_ok": &scrape.MetricMetadata{Type: textparse.MetricTypeUntyped, Metric: "labelnum_ok"},
97+
"job1/instance1/labelnum_ok": &scrape.MetricMetadata{Type: textparse.MetricTypeUnknown, Metric: "labelnum_ok"},
9898
"job1/instance1/labelnum_bad": &scrape.MetricMetadata{Type: textparse.MetricTypeGauge, Metric: "labelnum_bad"},
9999
},
100100
input: []tsdb.RefSample{
-32 KB
Binary file not shown.

vendor/github.com/prometheus/prometheus/pkg/textparse/interface.go

Lines changed: 91 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/prometheus/prometheus/pkg/textparse/openmetricslex.l

Lines changed: 71 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)