DataDog / datadogpy

The Datadog Python library
https://datadoghq.com/
Other
612 stars 302 forks source link

Error while pushing metrics to datadog (AttributeError: 'str' object has no attribute 'get') #743

Closed mrigank002 closed 1 year ago

mrigank002 commented 1 year ago

This is what my metrics is: metrics={ "series": [ { "type": "rate", "interval":1, "host": "GitLab", "metric": "thl.certificate.days.test", "tags": [ "source:vault", "sre-environment:dev", "sre-appstackcode:odccsm", "namespace:namespace", "name:name", "region:region", "trigram:trigram", "purpose:purpose" ], "points": [ [ time.time(), "5" ] ] } ] }

api.Metric.send(metrics=metrics)

error:

Traceback (most recent call last): File "/home/gitlab-runner/builds/wkpzw2iM/0/mcs/odc/certificate-operations/datadog_test.py", line 47, in api.Metric.send(metrics=metrics) File "/home/gitlab-runner/.local/lib/python3.10/site-packages/datadog/api/metrics.py", line 109, in send return super(Metric, cls).send( File "/home/gitlab-runner/.local/lib/python3.10/site-packages/datadog/api/resources.py", line 80, in send return APIClient.submit( File "/home/gitlab-runner/.local/lib/python3.10/site-packages/datadog/api/api_client.py", line 145, in submit if obj_params.get("host", "") == "": AttributeError: 'str' object has no attribute 'get'

mrigank002 commented 1 year ago

This obj_params is a dictionary and it should run .get method. But I don't know why it is throwing exception.

mrigank002 commented 1 year ago

when I am running this code in my machine for tis dict it is working fine:

for obj_params in metrics["series"]: print(obj_params.get("host", ""))

github-actions[bot] commented 1 year ago

Thanks for your contribution!

This issue has been automatically marked as stale because it has not had activity in the last 30 days. Note that the issue will not be automatically closed, but this notification will remind us to investigate why there's been inactivity. Thank you for participating in the Datadog open source community.

If you would like this issue to remain open:

  1. Verify that you can still reproduce the issue in the latest version of this project.

  2. Comment that the issue is still reproducible and include updated details requested in the issue template.

therve commented 1 year ago

Hi,

Sorry for not responding earlier. The issue is that you have a series key which should not be here. Here's a working example:

metrics = [
    {
        "type": "rate",
        "interval": 1,
        "host": "GitLab",
        "metric": "thl.certificate.days.test",                                                                                                                 
        "tags": [
            "source:vault",
            "sre-environment:dev",
            "sre-appstackcode:odccsm",                                                                                                                         
            "namespace:namespace",
            "name:name",
            "region:region",
            "trigram:trigram",                                                                                                                                 
            "purpose:purpose",
        ], 
        "points": [[time.time(), 5]],
    }
]

api.Metric.send(metrics=metrics)