Skip to content

Commit a8c2f78

Browse files
authored
Document and fix recorded metric forwarding (#266)
This should address most of #104 - fix target cache lookup for recorded metrics - update documentation with examples Also: allow overriding docker FROM target with DOCKER_IMAGE_BASE env var (helpful for testing and runtime scenarios where /bin/sh is needed)
1 parent dc0e3d6 commit a8c2f78

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
FROM gcr.io/distroless/static:latest
1+
ARG DOCKER_IMAGE_BASE=gcr.io/distroless/static:latest
2+
FROM $DOCKER_IMAGE_BASE
23
LABEL maintainer "Stackdriver Engineering <[email protected]>"
34

45
COPY stackdriver-prometheus-sidecar /bin/stackdriver-prometheus-sidecar

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ PROMU_URL := https://github.com/prometheus/promu/releases/download/v$(PROMU_
4343
PREFIX ?= $(shell pwd)
4444
BIN_DIR ?= $(shell pwd)
4545
# Private repo.
46+
DOCKER_IMAGE_BASE ?= gcr.io/distroless/static:latest
4647
DOCKER_IMAGE_NAME ?= gcr.io/stackdriver-prometheus/stackdriver-prometheus-sidecar
4748
DOCKER_IMAGE_TAG ?= $(subst /,-,$(shell git rev-parse --abbrev-ref HEAD))
4849

@@ -110,7 +111,7 @@ tarball: promu
110111

111112
docker: build-linux-amd64
112113
@echo ">> building docker image"
113-
docker build -t "$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)" .
114+
docker build --build-arg "DOCKER_IMAGE_BASE=$(DOCKER_IMAGE_BASE)" -t "$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)" .
114115

115116
push: test docker
116117
@echo ">> pushing docker image"

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ stackdriver-prometheus-sidecar --include='{__name__!~"cadvisor_.+",job="k8s"}' .
7070

7171
This drops all series which do not have a `job` label `k8s` and all metrics that have a name starting with `cadvisor_`.
7272

73-
For equality filter on metric name you can use the simpler notation, e.g. `--include='metric_name{label="foo"}'`.
73+
For equality filter on metric name you can use the simpler notation, e.g., `--include='metric_name{label="foo"}'`.
7474

7575
The flag may be repeated to provide several sets of filters, in which case the metric will be forwarded if it matches at least one of them. Please note that inclusion filters only apply to Prometheus metrics proxied directly, and do not apply to [aggregated counters](#counter-aggregator).
7676

@@ -95,6 +95,28 @@ static_metadata:
9595
* All `static_metadata` entries must have `type` specified. This specifies the Stackdriver metric type and overrides the metric type chosen by the Prometheus client.
9696
* If `value_type` is specified, it will override the default value type for counters and gauges. All Prometheus metrics have a default type of double.
9797

98+
#### Dealing with recording rules
99+
100+
The default Prometheus naming format for [recording rules](https://prometheus.io/docs/practices/rules/) is `level:metric:operations`, e.g., `instance:requests_total:sum`, but colons are not allowed in Stackdriver metric descriptor names. To forward a recorded Prometheus metric to Stackdriver, you must use the `metric_renames` feature to replace the colon characters:
101+
102+
```yaml
103+
metric_renames:
104+
- from: instance:requests_total:sum
105+
to: recorded_instance_requests_total_sum
106+
```
107+
108+
Additionally, the sidecar assumes that any recorded metrics are gauges. If this is not the case (e.g., for a Prometheus counter metric) you will need to specify that in `static_metadata`:
109+
110+
```yaml
111+
static_metadata:
112+
- metric: recorded_instance_requests_total_sum
113+
type: counter
114+
value_type: int64
115+
help: an arbitrary help string
116+
```
117+
118+
*Warning:* recorded metrics _must_ have minimally an "instance" and "job" label, otherwise they will not be forwarded.
119+
98120
#### Counter Aggregator
99121

100122
Counter Aggregator is an advanced feature of the sidecar that can be used to export a sum of multiple Prometheus counters to Stackdriver as a single CUMULATIVE metric.

targets/cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func targetMatch(targets []*Target, lset labels.Labels) (*Target, bool) {
179179
Outer:
180180
for _, t := range targets {
181181
for _, tl := range t.Labels {
182-
if lset.Get(tl.Name) != tl.Value {
182+
if v := lset.Get(tl.Name); v != "" && v != tl.Value {
183183
continue Outer
184184
}
185185
}

0 commit comments

Comments
 (0)