Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add branch and some aliases to build_info #52

Merged
merged 1 commit into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/).

Expand Down
1 change: 1 addition & 0 deletions src/autometrics/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions src/autometrics/tracker/opentelemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -129,14 +126,15 @@ 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(
1.0,
attributes={
"commit": commit,
"version": version,
"branch": branch,
},
)

Expand Down
7 changes: 4 additions & 3 deletions src/autometrics/tracker/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
OBJECTIVE_LATENCY_THRESHOLD_PROMETHEUS,
COMMIT_KEY,
VERSION_KEY,
BRANCH_KEY,
)

from .exemplar import get_exemplar
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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."""
Expand Down
14 changes: 12 additions & 2 deletions src/autometrics/tracker/test_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
Expand All @@ -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)
Expand All @@ -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)
5 changes: 3 additions & 2 deletions src/autometrics/tracker/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down