Gather metrics from Python services using either AWS CloudWatch or Prometheus.
Project description
Metrics library
This repo contains a metrics library implemented in Go and Python. The goal of the metric library is to be transparent inside the deployment if metrics are disabled.
When using the Amazon CloudWatch metric server, configure the following environment variables:
AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEYAWS_DEFAULT_REGION
Refer to the AWS SDK configuration documentation for more info.
Installation
For python:
pip install spacearth-metrics
For go:
go get github.com/Spacearth-NAV/metrics-lib
Usage
For the python library:
import time
from spacearth.metrics import MetricServer
# create the server, add some fixed labels
metric_server = MetricServer.create_server("aws", "custom", {"environment": "development"})
# increment counters
metric_server.add_observation("requests_received", 1, labels={"endpoint": "/login"})
# measure times
t_start = time.time()
# do something
t_end = time.time()
metric_server.measure_time("processing_time", t_end - t_start, labels={"step": "something"})
# keep track of resources
def on_connection(conn):
metric_server.increment_value("connections")
try:
while conn.connected:
# handle connection
pass
finally:
metric_server.decrement_value("connections")
For the go library:
package main
import (
"log/slog"
"net/http"
"time"
"github.com/Spacearth-NAV/metrics-lib/go"
)
var metricsServer metrics.Server
func main() {
// you can specify a logger: the interface is compatible with the slog package
metrics.SetLogger(slog.Default())
// instantiate the metric server
var err error
metricsServer, err = metrics.NewServer(metrics.AWS, "custom", metrics.Label{"environment", "development"})
if err != nil {
panic(err)
}
http.HandleFunc("GET /info", connectionCallback)
}
func connectionCallback(w http.ResponseWriter, r *http.Request) {
// count active connections
metricsServer.IncrementValue("active_connections", 1, metrics.Label{"endpoint", "info"})
defer metricsServer.DecrementValue("active_connections", 1, metrics.Label{"endpoint", "info"})
// count requests
metricsServer.AddObservation("received_requests", 1, metrics.Label{"endpoint", "info"})
// measure time
start := time.Now()
/* do something */
end := time.Now()
metricsServer.MeasureTime("processing_time", end.Sub(start), metrics.Label{"endpoint", "info"})
w.WriteHeader(http.StatusOK)
}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file spacearth_metrics-1.0.0.tar.gz.
File metadata
- Download URL: spacearth_metrics-1.0.0.tar.gz
- Upload date:
- Size: 9.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ac525faf9b12ed202151373923a3a2ed8f5f6a61a2f28e4d210d3049b8f68926
|
|
| MD5 |
cad98bdbbf5e39f05c516293be7c1c1a
|
|
| BLAKE2b-256 |
2cb951658b1aedac82d7caa9d597ac6510fe9d31d779e675846593fdd48a6817
|
Provenance
The following attestation bundles were made for spacearth_metrics-1.0.0.tar.gz:
Publisher:
publish.yml on Spacearth-NAV/metrics-lib
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
spacearth_metrics-1.0.0.tar.gz -
Subject digest:
ac525faf9b12ed202151373923a3a2ed8f5f6a61a2f28e4d210d3049b8f68926 - Sigstore transparency entry: 764333509
- Sigstore integration time:
-
Permalink:
Spacearth-NAV/metrics-lib@9c4aa589d728be75b4cee5c5cc97875dea13fde8 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/Spacearth-NAV
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@9c4aa589d728be75b4cee5c5cc97875dea13fde8 -
Trigger Event:
push
-
Statement type:
File details
Details for the file spacearth_metrics-1.0.0-py3-none-any.whl.
File metadata
- Download URL: spacearth_metrics-1.0.0-py3-none-any.whl
- Upload date:
- Size: 12.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
76daad4cef549a5a8a7e9b38d54678a90a2b6abd2d0d628d38ba11b099585508
|
|
| MD5 |
d357d5c5e70a1353e4d44676b52515e1
|
|
| BLAKE2b-256 |
de3e376e625063ea858942c2be2c19ee5d7df96899f47a2b6efa82e5f4733400
|
Provenance
The following attestation bundles were made for spacearth_metrics-1.0.0-py3-none-any.whl:
Publisher:
publish.yml on Spacearth-NAV/metrics-lib
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
spacearth_metrics-1.0.0-py3-none-any.whl -
Subject digest:
76daad4cef549a5a8a7e9b38d54678a90a2b6abd2d0d628d38ba11b099585508 - Sigstore transparency entry: 764333515
- Sigstore integration time:
-
Permalink:
Spacearth-NAV/metrics-lib@9c4aa589d728be75b4cee5c5cc97875dea13fde8 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/Spacearth-NAV
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@9c4aa589d728be75b4cee5c5cc97875dea13fde8 -
Trigger Event:
push
-
Statement type: