Skip to main content

Anodot API python package

Project description

Latest Release Python versions

Anodot API python package

Install:

pip install python-anodot

Posting metrics

More about the flow and protocols you can read here Posting metrics with Anodot API

Note:

  • All dots and spaces in measurement names and in dimensions are replaced with an underscore _
  • When you pass more than 1000 events to the anodot.send function they are splitted into chuncks before sending
  • Events should be sorted by timestamp in the ascending order

Protocol 2.0

import anodot
import logging
import sys

from datetime import datetime

data = [
    {"time": "2020-01-01 00:00:00", "packets_in": 100, "packets_out": 120, "host": "host134", "region": "region1"},
    {"time": "2020-01-01 00:01:00", "packets_in": 163, "packets_out": 130, "host": "host126", "region": "region1"},
    {"time": "2020-01-01 00:02:00", "packets_in": 215, "packets_out": 140, "host": "host101", "region": "region2"}
]

VERSION = 1

events = []
for event in data:
    timestamp = datetime.strptime(event['time'], '%Y-%m-%d %H:%M:%S')
    events.append(
        anodot.Metric20(
            what='packets_in',
            value=event['packets_in'],
            target_type=anodot.TargetType.GAUGE,
            timestamp=timestamp,
            dimensions={'host': event['host'], 'region': event['region']},
            tags={'tag_name': ['tag_value']},
            version=VERSION,
        )
    )
    events.append(
        anodot.Metric20(
            what='packets_out',
            value=event['packets_out'],
            target_type=anodot.TargetType.GAUGE,
            timestamp=timestamp,
            dimensions={'host': event['host'], 'region': event['region']},
            tags={'tag_name': ['tag_value']},
            version=VERSION,
        )
    )

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
logger.addHandler(handler)

anodot.send(events, token='datacollectiontoken', logger=logger, base_url='https://app.anodot.com')

Protocol 3.0

import anodot
import logging
import sys

from datetime import datetime

data = [
    {"time": "2020-01-01 00:00:00", "packets_in": 100, "packets_out": 120, "host": "host134", "region": "region1"},
    {"time": "2020-01-01 00:01:00", "packets_in": 163, "packets_out": 130, "host": "host126", "region": "region1"},
    {"time": "2020-01-01 00:02:00", "packets_in": 215, "packets_out": 140, "host": "host101", "region": "region2"}
]

VERSION = 1

api_client = anodot.ApiClient('accesskeyhere', base_url='https://app.anodot.com')
schema = api_client.create_schema(
    anodot.Schema(
        name='new_schema',
        dimensions=['host', 'region'],
        measurements=[
            anodot.Measurement('packets_in', anodot.Aggregation.AVERAGE),
            anodot.Measurement('packets_out', anodot.Aggregation.AVERAGE),
        ],
        missing_dim_policy=anodot.MissingDimPolicy(action=anodot.MissingDimPolicyAction.FAIL),
        version=VERSION,
    )
)

events = []
for event in data:
    timestamp = datetime.strptime(event['time'], '%Y-%m-%d %H:%M:%S')
    events.append(
        anodot.Metric30(
            schema_id=schema['schema']['id'],
            measurements={"packets_in": event['packets_in'], "packets_out": event['packets_out']},
            timestamp=timestamp,
            dimensions={'host': event['host'], 'region': event['region']},
            tags={'tag_name': ['tag_value']},
        )
    )

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
logger.addHandler(handler)

anodot.send(
    events,
    token='datacollectiontoken',
    logger=logger,
    base_url='https://app.anodot.com',
    protocol=anodot.Protocol.ANODOT30,
)

anodot.send_watermark(
    anodot.Watermark(
        schema_id=schema['schema']['id'],
        timestamp=datetime.strptime('2020-01-01 00:03:00', '%Y-%m-%d %H:%M:%S'),
    ),
    token='datacollectiontoken',
    logger=logger,
    base_url='https://app.anodot.com',
)

Posting metrics (for package version <2.0)

Example

import logging
import sys

from anodot import metric
from datetime import datetime

data = [
    {"time": "2020-01-01 00:00:00", "packets_in": 100, "packets_out": 120, "host": "host134", "region": "region1"},
    {"time": "2020-01-01 00:01:00", "packets_in": 163, "packets_out": 130, "host": "host126", "region": "region1"},
    {"time": "2020-01-01 00:02:00", "packets_in": 215, "packets_out": 140, "host": "host101", "region": "region2"}
]

VERSION = 1

events = []
for event in data:
    timestamp = datetime.strptime(event['time'], '%Y-%m-%d %H:%M:%S')
    events.append(
        metric.Metric(
            what='packets_in',
            value=event['packets_in'],
            target_type=metric.TargetType.GAUGE,
            timestamp=timestamp,
            dimensions={'host': event['host'], 'region': event['region']},
            tags={'tag_name': ['tag_value']},
            version=VERSION
        )
    )
    events.append(
        metric.Metric(
            what='packets_out',
            value=event['packets_out'],
            target_type=metric.TargetType.GAUGE,
            timestamp=timestamp,
            dimensions={'host': event['host'], 'region': event['region']},
            tags={'tag_name': ['tag_value']},
            version=VERSION
        )
    )

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
logger.addHandler(handler)

metric.send(events, token='datacollectiontoken', logger=logger, base_url='https://app.anodot.com')

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

python-anodot-2.0.7.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

python_anodot-2.0.7-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file python-anodot-2.0.7.tar.gz.

File metadata

  • Download URL: python-anodot-2.0.7.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for python-anodot-2.0.7.tar.gz
Algorithm Hash digest
SHA256 66141ff3007d2f1f451febd561ca6b7708f9a410f21c4e8a2992c762e0429b05
MD5 7e890a304291df8b69d2f502e4f18405
BLAKE2b-256 098cabe68854c774288f70941397e2f64e31cfd23876fb6d36fba6244e6cdad5

See more details on using hashes here.

File details

Details for the file python_anodot-2.0.7-py3-none-any.whl.

File metadata

File hashes

Hashes for python_anodot-2.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 88b7655ef09a9fe837521efdc37ff1e8c15e2ba13696cf7e6599dbf7f64f291c
MD5 8d4d712250e531f46b0579eff9e59333
BLAKE2b-256 25d022febb781bc0e5b62cfb7dd6dfed813ef7c3e3abc8abf00b45307a776000

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