github.com/grafana/pyroscope@v1.18.0/examples/api/query.py (about)

     1  import requests
     2  from requests.auth import HTTPBasicAuth
     3  
     4  # NOTE: For original docs visit information visit:
     5  # https://grafana.com/docs/pyroscope/next/configure-server/about-server-api/
     6  
     7  # Authentication details if using cloud
     8  basic_auth_username = '<username>'  # Replace with your Grafana Cloud stack user
     9  basic_auth_password = '<password>'  # Replace with your Grafana Cloud API key
    10  pyroscope_server = 'https://profiles-prod-001.grafana.net'
    11  
    12  # If not using cloud, use the following:
    13  # pyroscope_server = 'http://localhost:4040' # replace with your server address:port
    14  
    15  application_name = 'my_application_name'
    16  query = f'process_cpu:cpu:nanoseconds:cpu:nanoseconds{{service_name="{application_name}"}}'
    17  query_from = 'now-1h'
    18  pyroscope_url = f'{pyroscope_server}/pyroscope/render?query={query}&from={query_from}'
    19  
    20  # Sending the request with authentication
    21  response = requests.get(
    22      pyroscope_url,
    23      auth=HTTPBasicAuth(basic_auth_username, basic_auth_password)
    24  )
    25  
    26  # Checking the response
    27  if response.status_code == 200:
    28      print(response.text)
    29  else:
    30      print(f"Failed to query data. Status code: {response.status_code}, Message: {response.text}")