From 9dc3ef28b28b81a035dbf9354a452fb74f472b00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=82=E3=81=A7?= Date: Wed, 14 Jun 2023 14:58:01 +0200 Subject: [PATCH] Add branch and some aliases to build_info --- CHANGELOG.md | 4 ++-- README.md | 11 ++++++++--- src/autometrics/constants.py | 1 + src/autometrics/tracker/opentelemetry.py | 6 ++---- src/autometrics/tracker/prometheus.py | 7 ++++--- src/autometrics/tracker/test_tracker.py | 14 ++++++++++++-- src/autometrics/tracker/tracker.py | 5 +++-- 7 files changed, 32 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f0245d4..d139039 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Changed -- +- `build_info` is extended with support for branch labels and now picks up the commit label from `COMMIT_SHA` env var (#52) ### Deprecated @@ -30,7 +30,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Security -- Update requests, starlette, fastapi dependencies used by the examples +- Update requests, starlette, fastapi dependencies used by the examples ## [0.5](/~https://github.com/autometrics-dev/autometrics-py/releases/tag/0.5) - 2023-05-11 diff --git a/README.md b/README.md index eeb88aa..56e04e8 100644 --- a/README.md +++ b/README.md @@ -128,9 +128,14 @@ Configure the package that autometrics will use to produce metrics with the `AUT Autometrics makes it easy to identify if a specific version or commit introduced errors or increased latencies. -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. - -The `version` is read from the `AUTOMETRICS_VERSION` environment variable, and the `commit` value uses the environment variable `AUTOMETRICS_COMMIT`. +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. +Configure the labels by setting the following environment variables: + +| Label | Run-Time Environment Variables | Default value | +| --------- | ------------------------------------- | ------------- | +| `version` | `AUTOMETRICS_VERSION` | `""` | +| `commit` | `AUTOMETRICS_COMMIT` or `COMMIT_SHA` | `""` | +| `branch` | `AUTOMETRICS_BRANCH` or `BRANCH_NAME` | `""` | This follows the method outlined in [Exposing the software version to Prometheus](https://www.robustperception.io/exposing-the-software-version-to-prometheus/). diff --git a/src/autometrics/constants.py b/src/autometrics/constants.py index 7399ee0..644a8c2 100644 --- a/src/autometrics/constants.py +++ b/src/autometrics/constants.py @@ -20,6 +20,7 @@ OBJECTIVE_LATENCY_THRESHOLD = "objective.latency_threshold" VERSION_KEY = "version" COMMIT_KEY = "commit" +BRANCH_KEY = "branch" # The values are updated to use underscores instead of periods to avoid issues with prometheus. # A similar thing is done in the rust library, which supports multiple exporters diff --git a/src/autometrics/tracker/opentelemetry.py b/src/autometrics/tracker/opentelemetry.py index 0f06c25..6273c77 100644 --- a/src/autometrics/tracker/opentelemetry.py +++ b/src/autometrics/tracker/opentelemetry.py @@ -10,9 +10,6 @@ set_meter_provider, ) from opentelemetry.sdk.metrics import MeterProvider -from opentelemetry.sdk.metrics.export import ( - MetricReader, -) from opentelemetry.sdk.metrics.view import View, ExplicitBucketHistogramAggregation from opentelemetry.exporter.prometheus import PrometheusMetricReader @@ -129,7 +126,7 @@ def __histogram( }, ) - def set_build_info(self, commit: str, version: str): + def set_build_info(self, commit: str, version: str, branch: str): if not self._has_set_build_info: self._has_set_build_info = True self.__up_down_counter_instance.add( @@ -137,6 +134,7 @@ def set_build_info(self, commit: str, version: str): attributes={ "commit": commit, "version": version, + "branch": branch, }, ) diff --git a/src/autometrics/tracker/prometheus.py b/src/autometrics/tracker/prometheus.py index 971b9af..685b56a 100644 --- a/src/autometrics/tracker/prometheus.py +++ b/src/autometrics/tracker/prometheus.py @@ -15,6 +15,7 @@ OBJECTIVE_LATENCY_THRESHOLD_PROMETHEUS, COMMIT_KEY, VERSION_KEY, + BRANCH_KEY, ) from .exemplar import get_exemplar @@ -49,7 +50,7 @@ class PrometheusTracker: ], ) prom_gauge = Gauge( - BUILD_INFO_NAME, BUILD_INFO_DESCRIPTION, [COMMIT_KEY, VERSION_KEY] + BUILD_INFO_NAME, BUILD_INFO_DESCRIPTION, [COMMIT_KEY, VERSION_KEY, BRANCH_KEY] ) def __init__(self) -> None: @@ -108,10 +109,10 @@ def _histogram( threshold, ).observe(duration, exemplar) - def set_build_info(self, commit: str, version: str): + def set_build_info(self, commit: str, version: str, branch: str): if not self._has_set_build_info: self._has_set_build_info = True - self.prom_gauge.labels(commit, version).set(1) + self.prom_gauge.labels(commit, version, branch).set(1) # def start(self, function: str = None, module: str = None): # """Start tracking metrics for a function call.""" diff --git a/src/autometrics/tracker/test_tracker.py b/src/autometrics/tracker/test_tracker.py index a51728e..29508b4 100644 --- a/src/autometrics/tracker/test_tracker.py +++ b/src/autometrics/tracker/test_tracker.py @@ -32,9 +32,11 @@ def test_init_prometheus_tracker_set_build_info(monkeypatch): commit = "d6abce3" version = "1.0.1" + branch = "main" monkeypatch.setenv("AUTOMETRICS_COMMIT", commit) monkeypatch.setenv("AUTOMETRICS_VERSION", version) + monkeypatch.setenv("AUTOMETRICS_BRANCH", branch) prom_tracker = init_tracker(TrackerType.PROMETHEUS) assert isinstance(prom_tracker, PrometheusTracker) @@ -43,11 +45,14 @@ def test_init_prometheus_tracker_set_build_info(monkeypatch): assert blob is not None data = blob.decode("utf-8") - prom_build_info = f"""build_info{{commit="{commit}",version="{version}"}} 1.0""" + prom_build_info = ( + f"""build_info{{branch="{branch}",commit="{commit}",version="{version}"}} 1.0""" + ) assert prom_build_info in data monkeypatch.delenv("AUTOMETRICS_VERSION", raising=False) monkeypatch.delenv("AUTOMETRICS_COMMIT", raising=False) + monkeypatch.delenv("AUTOMETRICS_BRANCH", raising=False) def test_init_otel_tracker_set_build_info(monkeypatch): @@ -61,9 +66,11 @@ def test_init_otel_tracker_set_build_info(monkeypatch): commit = "a29a178" version = "0.0.1" + branch = "main" monkeypatch.setenv("AUTOMETRICS_COMMIT", commit) monkeypatch.setenv("AUTOMETRICS_VERSION", version) + monkeypatch.setenv("AUTOMETRICS_BRANCH", branch) otel_tracker = init_tracker(TrackerType.OPENTELEMETRY) assert isinstance(otel_tracker, OpenTelemetryTracker) @@ -72,8 +79,11 @@ def test_init_otel_tracker_set_build_info(monkeypatch): assert blob is not None data = blob.decode("utf-8") - prom_build_info = f"""build_info{{commit="{commit}",version="{version}"}} 1.0""" + prom_build_info = ( + f"""build_info{{branch="{branch}",commit="{commit}",version="{version}"}} 1.0""" + ) assert prom_build_info in data monkeypatch.delenv("AUTOMETRICS_VERSION", raising=False) monkeypatch.delenv("AUTOMETRICS_COMMIT", raising=False) + monkeypatch.delenv("AUTOMETRICS_BRANCH", raising=False) diff --git a/src/autometrics/tracker/tracker.py b/src/autometrics/tracker/tracker.py index f4bd05b..4f7a694 100644 --- a/src/autometrics/tracker/tracker.py +++ b/src/autometrics/tracker/tracker.py @@ -16,7 +16,7 @@ class Result(Enum): class TrackMetrics(Protocol): """Protocol for tracking metrics.""" - def set_build_info(self, commit: str, version: str): + def set_build_info(self, commit: str, version: str, branch: str): """Observe the build info. Should only be called once per tracker instance""" def finish( @@ -55,8 +55,9 @@ def init_tracker(tracker_type: TrackerType) -> TrackMetrics: # NOTE - Only set the build info when the tracker is initialized tracker_instance.set_build_info( - commit=os.getenv("AUTOMETRICS_COMMIT") or "", + commit=os.getenv("AUTOMETRICS_COMMIT") or os.getenv("COMMIT_SHA") or "", version=os.getenv("AUTOMETRICS_VERSION") or "", + branch=os.getenv("AUTOMETRICS_BRANCH") or os.getenv("BRANCH_NAME") or "", ) return tracker_instance