Skip to main content

Dynatrace Metric Utilities

Project description

dynatrace-metric-utils-python

Python utility for interacting with the Dynatrace v2 metrics API .

Installation

To install the latest version from PyPI run:

pip install dynatrace-metric-utils

Usage

Using this library to create metric lines is a two-step process. First, create lines using the DynatraceMetricsFactory. Then, serialize them using a DynatraceMetricsSerializer. Furthermore, this library contains constants for use in projects consuming this library. This repository also contains an example script demonstrating the use of the DynatraceMetricsFactory and the DynatraceMetricsSerializer.

Metric Creation

To create metrics, call one of the static methods on the DynatraceMetricsFactory. Available instruments are:

  • Gauge: create_int_gauge / create_float_gauge
  • Counter: create_int_counter_delta / create_float_counter_delta
  • Summary: create_int_summary / create_float_summary

In the simplest form, metric creation looks like this:

factory = DynatraceMetricsFactory()
metric = factory.create_int_gauge("int-gauge", 23)

Additionally, it is possible to pass a list of dimensions to the metric upon creation:

metric_dimensions = {
    "dim1": "val1",
    "dim2": "val2",
}

metric = factory.create_int_gauge("int-gauge", 23, metric_dimensions)

The dimensions will be added to the serialized metric. See the section on dimension precedence for more information.

Finally, it is also possible to add a timestamp to the metric:

# Passing None for the dimensions will not add any dimensions to the metric.
# pass the timestamp as unix-time milliseconds.
metric = factory.create_int_gauge("int-gauge", 23, None, time.time() * 1000)

# Alternatively, the dimensions parameter can be skipped and timestamp can be passed as a named parameter.
metric = factory.create_int_gauge("int-gauge", 23,
                                  timestamp=time.time() * 1000)

If the metric timestamp is omitted or invalid, the Dynatrace server uses its current time upon ingestion.

Metric serialization

The created metrics can then be serialized using a DynatraceMetricsSerializer:

# root level logger.
logger = logging.getLogger(__name__)
# The logger is optional. If no logger is specified, a logger will be created.
factory = DynatraceMetricsFactory(
    logger.getChild(DynatraceMetricsFactory.__name__))

# Create a list of default dimensions which are added to all metrics serialized by this serializer.
default_dims = {
    "default1": "value1",
    "default2": "value2"
}

serializer = DynatraceMetricsSerializer(
    logger.getChild(DynatraceMetricsSerializer.__name__),
    "prefix",  # metric key prefix, added to all metric names
    default_dims,  # default dimensions
    True,  # whether or not to enrich with Dynatrace metadata
    "metric-src",
    # the name of the framework that emits the metrics, e.g. 'opentelemetry'
)
metric = factory.create_int_gauge("int-gauge", 23)
print(serializer.serialize(metric))

# Should result in a line like: 
# prefix.int-gauge,default1=value1,default2=value2,dt.metrics.source=metric-src gauge,23

Common constants

The constants can be accessed via the static DynatraceMetricsApiConstants class .

Currently available constants are:

Dynatrace Metadata enrichment

If the enrich_with_dynatrace_metadata toggle in the DynatraceMetricsSerializer constructor is set to True (=default), an attempt is made to read Dynatrace metadata. On a host with a running OneAgent, setting this option will collect metadata and add it as dimensions to all serialized metrics. Metadata typically consist of the Dynatrace host ID and process group ID. More information on the underlying feature that is used by the library can be found in the Dynatrace documentation.

Dimension precedence

Since there are multiple levels of dimensions (default, metric-specific, serializer-specific) and duplicate keys are not allowed, there is a specified precedence in dimension keys. Default dimensions will be overwritten by metric-specific dimensions, and all dimensions will be overwritten by serializer-specific dimensions if they share the same key after normalization. Serializer-specific dimensions include the metadata dimensions, as well as the metrics source, which is added as a dimension with dt.metrics.source as its key if the metrics_source is set. Note that the serializer-specific dimensions will only contain dimension keys reserved by Dynatrace.

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

dynatrace-metric-utils-0.1.0b0.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

dynatrace_metric_utils-0.1.0b0-py3-none-any.whl (19.7 kB view details)

Uploaded Python 3

File details

Details for the file dynatrace-metric-utils-0.1.0b0.tar.gz.

File metadata

  • Download URL: dynatrace-metric-utils-0.1.0b0.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for dynatrace-metric-utils-0.1.0b0.tar.gz
Algorithm Hash digest
SHA256 ec7707958836eec2f50be79534b9e4f98cf5dd21d9a56362de7132e6df07f04c
MD5 a5741880968164dffb388fdf0e0e327a
BLAKE2b-256 3664d40874443604747170e9a5a8e1c0880d58d6ab36dce195c9f8ee61d358a8

See more details on using hashes here.

File details

Details for the file dynatrace_metric_utils-0.1.0b0-py3-none-any.whl.

File metadata

  • Download URL: dynatrace_metric_utils-0.1.0b0-py3-none-any.whl
  • Upload date:
  • Size: 19.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for dynatrace_metric_utils-0.1.0b0-py3-none-any.whl
Algorithm Hash digest
SHA256 323228f7118ffd63740fe9736fa5682208fa084444c08b8e719c36389fc3e4cf
MD5 6bf8e5b9cdb5bc8164319ebe8ff066b0
BLAKE2b-256 c4a92be2f1dbd264e595a48b2ce1fd2e58dbc81d7c933438328ec68da79d008f

See more details on using hashes here.

Supported by

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