Mongo Tooling Metrics

raw JSON →
1.0.8 verified Fri May 01 auth: no python

A slim library which leverages Pydantic to reliably collect type-enforced metrics and store them to MongoDB. Version 1.0.8 supports Python >=3.7,<4.0. Low release cadence.

pip install mongo-tooling-metrics
error ImportError: cannot import name 'MetricCollector' from 'mongo-tooling-metrics'
cause Attempted to import using the package name with hyphens.
fix
Use the correct import: from mongo_tooling_metrics import MetricCollector
error pydantic.error_wrappers.ValidationError: 1 validation error for MetricsConfig mongo_uri str type expected (type=type_error.str)
cause Passed a non-string value (e.g., None) for mongo_uri.
fix
Ensure mongo_uri is a string, e.g., os.environ.get('MONGO_URI', 'mongodb://localhost:27017')
gotcha MetricCollector.send_gauge() and send_counter() accept only int or float values; passing a string will raise a ValidationError silently if using Pydantic v1?
fix Ensure metric values are numeric (int or float).
gotcha If MongoDB connection fails, metrics are silently dropped; no exception is raised by default.
fix Enable logging or check the return value of send methods if available.

Initialize MetricsConfig with MongoDB URI and collection details, then create a MetricCollector and send a gauge metric.

from mongo_tooling_metrics import MetricCollector, MetricsConfig
import os

config = MetricsConfig(
    mongo_uri=os.environ.get('MONGO_URI', 'mongodb://localhost:27017'),
    database_name='metrics',
    collection_name='my_metrics'
)
collector = MetricCollector(config)
collector.send_gauge('my_gauge', 42)
print('Metric sent successfully.')