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.0.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.0-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

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

Hashes for spacearth_metrics-1.0.0.tar.gz
Algorithm Hash digest
SHA256 ac525faf9b12ed202151373923a3a2ed8f5f6a61a2f28e4d210d3049b8f68926
MD5 cad98bdbbf5e39f05c516293be7c1c1a
BLAKE2b-256 2cb951658b1aedac82d7caa9d597ac6510fe9d31d779e675846593fdd48a6817

See more details on using hashes here.

Provenance

The following attestation bundles were made for spacearth_metrics-1.0.0.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.0-py3-none-any.whl.

File metadata

File hashes

Hashes for spacearth_metrics-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 76daad4cef549a5a8a7e9b38d54678a90a2b6abd2d0d628d38ba11b099585508
MD5 d357d5c5e70a1353e4d44676b52515e1
BLAKE2b-256 de3e376e625063ea858942c2be2c19ee5d7df96899f47a2b6efa82e5f4733400

See more details on using hashes here.

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

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