Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get the Query value widget through datadog api #1915

Open
kumarsyamala opened this issue Mar 20, 2024 · 5 comments
Open

How to get the Query value widget through datadog api #1915

kumarsyamala opened this issue Mar 20, 2024 · 5 comments

Comments

@kumarsyamala
Copy link

Hi

I am trying to use the Datadog Api.

I already have the Datadog dashboard in that we have few widgets which was created to show the trace count query.

I am planning to get the value of the trace count query through Datadog Api using postman. like how we get the details of a dashboard by giving the dashboard id.

I have used the get dashboard Api call through postman, I can the widget and the query also, but I am not getting the value in the response.

Can someone help me in getting the query value from widget trace count?

@nkzou
Copy link
Contributor

nkzou commented Mar 22, 2024

Hi there, you can try using the query_scalar_data function of the Metrics API - docs here. There's some more info about the endpoint here, and an example usage here.

Hopefully that helps, feel free to reach out if you run into any issues.

Copy link

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.

@github-actions github-actions bot added the stale label Apr 22, 2024
@felipefrodrigues
Copy link

Hello @kumarsyamala
Did you find any solution ?

@kumarsyamala
Copy link
Author

kumarsyamala commented Feb 5, 2025

@felipefrodrigues No

I couldn't find the solution

I looked everywhere but didn't find it

@felipefrodrigues
Copy link

felipefrodrigues commented Feb 5, 2025

I need to extract data from the widget and I consulted queryscalardata as @nkzou recommended

look at this example: https://github.com/DataDog/datadog-api-client-python/blob/master/examples/v2/metrics/QueryScalarData.py

I used this to create my script and it worked

main script


from dashboard_data import get_dashboard_data
from widget_data import get_widget_data

def get_metric():
  from_ts = 1738749600000
  to_ts = 1738753200000
  widget_id = YOUR_WIDGET_ID
  api_key = YOUR_API_KEY
  app_key = YOUR_APP_KEY

  dashboard_json = get_dashboard_data(api_key, app_key)
  widget_data = get_widget_data(
    dashboard_json,
    widget_id,
    from_ts,
    to_ts,
    api_key,
    app_key
  )

  if widget_data:
      return widget_data
  return {}

script to get the dashboard:


from datadog_api_client import ApiClient, Configuration
from datadog_api_client.v1.api.dashboards_api import DashboardsApi

def get_dashboard_data(api_key, app_key):
  configuration = Configuration()
  configuration.api_key["apiKeyAuth"] = api_key
  configuration.api_key["appKeyAuth"] = app_key


  with ApiClient(configuration) as api_client:
    api_instance = DashboardsApi(api_client)
    try:
      api_response = api_instance.get_dashboard(YOUR_DASHBOARD_ID)
      dashboard_json = api_response.to_dict()
      return dashboard_json
    except Exception as e:
      print(f"Error getting dashboard: {e}")
      return None

Script to extract widget data


from datadog_api_client import ApiClient, Configuration
from datadog_api_client.v2.api.metrics_api import MetricsApi
from datadog_api_client.v2.model.query_formula import QueryFormula
from datadog_api_client.v2.model.scalar_formula_query_request import ScalarFormulaQueryRequest
from datadog_api_client.v2.model.scalar_formula_request import ScalarFormulaRequest
from datadog_api_client.v2.model.scalar_formula_request_attributes import ScalarFormulaRequestAttributes
from datadog_api_client.v2.model.scalar_formula_request_queries import ScalarFormulaRequestQueries
from datadog_api_client.v2.model.scalar_formula_request_type import ScalarFormulaRequestType
from datadog_api_client.v2.model.events_compute import EventsCompute
from datadog_api_client.v2.model.events_scalar_query import EventsScalarQuery
import json

def get_widget_data(dashboard_json, widget_id, from_ts, to_ts, api_key, app_key):
  widget = next((w for w in dashboard_json.get("widgets", []) if w.get("id") == widget_id), None)
  configuration = Configuration()
  configuration.api_key["apiKeyAuth"] = api_key
  configuration.api_key["appKeyAuth"] = app_key

  queries_data = widget['definition']['requests'][0]['queries']
  formulas_data = widget['definition']['requests'][0]['formulas']
  formulas = []
  queries = []

  for q in queries_data:
    query_object = EventsScalarQuery(
      compute=EventsCompute(
        aggregation=q["compute"]["aggregation"],
        metric=q["compute"]["metric"]
      ),
      data_source=q["data_source"],
      name=q["name"],
      search= {
        "query": q["search"]["query"]
      },
      group_by=q["group_by"]
    )
    queries.append(query_object)


  for f in formulas_data:
    formula_object = QueryFormula(
      formula=f["formula"]
    )
    formulas.append(formula_object)
  
  with ApiClient(configuration) as api_client:
    api_instance = MetricsApi(api_client)
    body = ScalarFormulaQueryRequest(
      data=ScalarFormulaRequest(
        attributes=ScalarFormulaRequestAttributes(
          formulas=formulas,
          _from=from_ts,
          queries=ScalarFormulaRequestQueries(queries),
          to=to_ts,
        ),
      type=ScalarFormulaRequestType.SCALAR_REQUEST
    ))
    response = api_instance.query_scalar_data(body=body)
  
  if response and response.data and response.data.attributes:
    results = {}
    attributes = response.data.attributes

    if attributes.columns:
      for column in attributes.columns:
        results[column.name] = column.values
    return json.dumps(results)

  return json.dumps({})
  

I hope it helps you

@github-actions github-actions bot removed the stale label Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants