Skip to main content

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_ID
  • AWS_SECRET_ACCESS_KEY
  • AWS_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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spacearth_metrics-1.0.1.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

spacearth_metrics-1.0.1-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file spacearth_metrics-1.0.1.tar.gz.

File metadata

  • Download URL: spacearth_metrics-1.0.1.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

Hashes for spacearth_metrics-1.0.1.tar.gz
Algorithm Hash digest
SHA256 6bd46dcb9bfd505edbef50e66d10fcb8e154f0f8c75502b396f4743e55b4f768
MD5 1fb39ee00bfb3570589485349e002751
BLAKE2b-256 3209dc94a823db46df56c96e4c7b1ede38b204a8ce5266a4a418743b7e25d85b

See more details on using hashes here.

Provenance

The following attestation bundles were made for spacearth_metrics-1.0.1.tar.gz:

Publisher: publish.yml on Spacearth-NAV/metrics-lib

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file spacearth_metrics-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for spacearth_metrics-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2bd54fa1c1557c4d2f120a9da088de5608ab4d0111ae745c7bb9d9e7afd1d866
MD5 5a869286adcfc79b87215d2fe3b7e913
BLAKE2b-256 d211bb441c3799078fa86846539c47d6d97cc0fd18df71e5f388133ff80ce96f

See more details on using hashes here.

Provenance

The following attestation bundles were made for spacearth_metrics-1.0.1-py3-none-any.whl:

Publisher: publish.yml on Spacearth-NAV/metrics-lib

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page