Skip to content

Commit 16c6d22

Browse files
authored
Merge pull request #52 from autometrics-dev/improve-build-info
Add branch and some aliases to build_info
2 parents fd1aebd + 9dc3ef2 commit 16c6d22

File tree

7 files changed

+32
-16
lines changed

7 files changed

+32
-16
lines changed

CHANGELOG.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1616

1717
### Changed
1818

19-
-
19+
- `build_info` is extended with support for branch labels and now picks up the commit label from `COMMIT_SHA` env var (#52)
2020

2121
### Deprecated
2222

@@ -30,7 +30,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
3030

3131
### Security
3232

33-
- Update requests, starlette, fastapi dependencies used by the examples
33+
- Update requests, starlette, fastapi dependencies used by the examples
3434

3535
## [0.5](https://github.com/autometrics-dev/autometrics-py/releases/tag/0.5) - 2023-05-11
3636

README.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,14 @@ Configure the package that autometrics will use to produce metrics with the `AUT
128128
129129
Autometrics makes it easy to identify if a specific version or commit introduced errors or increased latencies.
130130

131-
It uses a separate metric (`build_info`) to track the version and, optionally, git commit of your service. It then writes queries that group metrics by the `version` and `commit` labels so you can spot correlations between those and potential issues.
132-
133-
The `version` is read from the `AUTOMETRICS_VERSION` environment variable, and the `commit` value uses the environment variable `AUTOMETRICS_COMMIT`.
131+
It uses a separate metric (`build_info`) to track the version and, optionally, git commit of your service. It then writes queries that group metrics by the `version`, `commit` and `branch` labels so you can spot correlations between those and potential issues.
132+
Configure the labels by setting the following environment variables:
133+
134+
| Label | Run-Time Environment Variables | Default value |
135+
| --------- | ------------------------------------- | ------------- |
136+
| `version` | `AUTOMETRICS_VERSION` | `""` |
137+
| `commit` | `AUTOMETRICS_COMMIT` or `COMMIT_SHA` | `""` |
138+
| `branch` | `AUTOMETRICS_BRANCH` or `BRANCH_NAME` | `""` |
134139

135140
This follows the method outlined in [Exposing the software version to Prometheus](https://www.robustperception.io/exposing-the-software-version-to-prometheus/).
136141

src/autometrics/constants.py

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
OBJECTIVE_LATENCY_THRESHOLD = "objective.latency_threshold"
2121
VERSION_KEY = "version"
2222
COMMIT_KEY = "commit"
23+
BRANCH_KEY = "branch"
2324

2425
# The values are updated to use underscores instead of periods to avoid issues with prometheus.
2526
# A similar thing is done in the rust library, which supports multiple exporters

src/autometrics/tracker/opentelemetry.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
set_meter_provider,
1111
)
1212
from opentelemetry.sdk.metrics import MeterProvider
13-
from opentelemetry.sdk.metrics.export import (
14-
MetricReader,
15-
)
1613
from opentelemetry.sdk.metrics.view import View, ExplicitBucketHistogramAggregation
1714
from opentelemetry.exporter.prometheus import PrometheusMetricReader
1815

@@ -129,14 +126,15 @@ def __histogram(
129126
},
130127
)
131128

132-
def set_build_info(self, commit: str, version: str):
129+
def set_build_info(self, commit: str, version: str, branch: str):
133130
if not self._has_set_build_info:
134131
self._has_set_build_info = True
135132
self.__up_down_counter_instance.add(
136133
1.0,
137134
attributes={
138135
"commit": commit,
139136
"version": version,
137+
"branch": branch,
140138
},
141139
)
142140

src/autometrics/tracker/prometheus.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
OBJECTIVE_LATENCY_THRESHOLD_PROMETHEUS,
1616
COMMIT_KEY,
1717
VERSION_KEY,
18+
BRANCH_KEY,
1819
)
1920

2021
from .exemplar import get_exemplar
@@ -49,7 +50,7 @@ class PrometheusTracker:
4950
],
5051
)
5152
prom_gauge = Gauge(
52-
BUILD_INFO_NAME, BUILD_INFO_DESCRIPTION, [COMMIT_KEY, VERSION_KEY]
53+
BUILD_INFO_NAME, BUILD_INFO_DESCRIPTION, [COMMIT_KEY, VERSION_KEY, BRANCH_KEY]
5354
)
5455

5556
def __init__(self) -> None:
@@ -108,10 +109,10 @@ def _histogram(
108109
threshold,
109110
).observe(duration, exemplar)
110111

111-
def set_build_info(self, commit: str, version: str):
112+
def set_build_info(self, commit: str, version: str, branch: str):
112113
if not self._has_set_build_info:
113114
self._has_set_build_info = True
114-
self.prom_gauge.labels(commit, version).set(1)
115+
self.prom_gauge.labels(commit, version, branch).set(1)
115116

116117
# def start(self, function: str = None, module: str = None):
117118
# """Start tracking metrics for a function call."""

src/autometrics/tracker/test_tracker.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ def test_init_prometheus_tracker_set_build_info(monkeypatch):
3232

3333
commit = "d6abce3"
3434
version = "1.0.1"
35+
branch = "main"
3536

3637
monkeypatch.setenv("AUTOMETRICS_COMMIT", commit)
3738
monkeypatch.setenv("AUTOMETRICS_VERSION", version)
39+
monkeypatch.setenv("AUTOMETRICS_BRANCH", branch)
3840

3941
prom_tracker = init_tracker(TrackerType.PROMETHEUS)
4042
assert isinstance(prom_tracker, PrometheusTracker)
@@ -43,11 +45,14 @@ def test_init_prometheus_tracker_set_build_info(monkeypatch):
4345
assert blob is not None
4446
data = blob.decode("utf-8")
4547

46-
prom_build_info = f"""build_info{{commit="{commit}",version="{version}"}} 1.0"""
48+
prom_build_info = (
49+
f"""build_info{{branch="{branch}",commit="{commit}",version="{version}"}} 1.0"""
50+
)
4751
assert prom_build_info in data
4852

4953
monkeypatch.delenv("AUTOMETRICS_VERSION", raising=False)
5054
monkeypatch.delenv("AUTOMETRICS_COMMIT", raising=False)
55+
monkeypatch.delenv("AUTOMETRICS_BRANCH", raising=False)
5156

5257

5358
def test_init_otel_tracker_set_build_info(monkeypatch):
@@ -61,9 +66,11 @@ def test_init_otel_tracker_set_build_info(monkeypatch):
6166

6267
commit = "a29a178"
6368
version = "0.0.1"
69+
branch = "main"
6470

6571
monkeypatch.setenv("AUTOMETRICS_COMMIT", commit)
6672
monkeypatch.setenv("AUTOMETRICS_VERSION", version)
73+
monkeypatch.setenv("AUTOMETRICS_BRANCH", branch)
6774

6875
otel_tracker = init_tracker(TrackerType.OPENTELEMETRY)
6976
assert isinstance(otel_tracker, OpenTelemetryTracker)
@@ -72,8 +79,11 @@ def test_init_otel_tracker_set_build_info(monkeypatch):
7279
assert blob is not None
7380
data = blob.decode("utf-8")
7481

75-
prom_build_info = f"""build_info{{commit="{commit}",version="{version}"}} 1.0"""
82+
prom_build_info = (
83+
f"""build_info{{branch="{branch}",commit="{commit}",version="{version}"}} 1.0"""
84+
)
7685
assert prom_build_info in data
7786

7887
monkeypatch.delenv("AUTOMETRICS_VERSION", raising=False)
7988
monkeypatch.delenv("AUTOMETRICS_COMMIT", raising=False)
89+
monkeypatch.delenv("AUTOMETRICS_BRANCH", raising=False)

src/autometrics/tracker/tracker.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Result(Enum):
1616
class TrackMetrics(Protocol):
1717
"""Protocol for tracking metrics."""
1818

19-
def set_build_info(self, commit: str, version: str):
19+
def set_build_info(self, commit: str, version: str, branch: str):
2020
"""Observe the build info. Should only be called once per tracker instance"""
2121

2222
def finish(
@@ -55,8 +55,9 @@ def init_tracker(tracker_type: TrackerType) -> TrackMetrics:
5555

5656
# NOTE - Only set the build info when the tracker is initialized
5757
tracker_instance.set_build_info(
58-
commit=os.getenv("AUTOMETRICS_COMMIT") or "",
58+
commit=os.getenv("AUTOMETRICS_COMMIT") or os.getenv("COMMIT_SHA") or "",
5959
version=os.getenv("AUTOMETRICS_VERSION") or "",
60+
branch=os.getenv("AUTOMETRICS_BRANCH") or os.getenv("BRANCH_NAME") or "",
6061
)
6162

6263
return tracker_instance

0 commit comments

Comments
 (0)