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

     1  import requests
     2  import json
     3  from requests.auth import HTTPBasicAuth
     4  
     5  # NOTE: For original docs visit information visit: 
     6  # https://grafana.com/docs/pyroscope/next/configure-server/about-server-api/ 
     7  
     8  # Specify the path to your pprof file, Pyroscope server URL, and application name
     9  pprof_file_path = 'path/to/your/pprof-file.pprof'
    10  
    11  # Authentication details if using cloud
    12  basic_auth_username = "<username>"  # Replace with your Grafana Cloud stack user
    13  basic_auth_password = "<password>"  # Replace with your Grafana Cloud API key
    14  pyroscope_server = 'https://profiles-prod-001.grafana.net'
    15  
    16  # If not using cloud, use the following:
    17  # pyroscope_server = 'http://localhost:4040' # replace with your server address:port
    18  
    19  # Specify the application name
    20  application_name = 'my_application_name'  # Replace with your application's name
    21  
    22  # Construct the Pyroscope ingest URL
    23  pyroscope_url = f'{pyroscope_server}/ingest?name={application_name}'
    24  
    25  # # Define your sample type configuration (Modify as needed)
    26  # sample_type_config = {
    27  #     "your_sample_type": {  # Replace 'your_sample_type' with the actual sample type
    28  #         "units": "your_units",  # e.g., "samples", "bytes"
    29  #         "aggregation": "your_aggregation",  # e.g., "sum", "average"
    30  #         "display-name": "your_display_name",
    31  #         "sampled": True or False  # Set to True or False based on your data
    32  #     }
    33  # }
    34  
    35  # Sample type configuration
    36  sample_type_config = {
    37    "cpu": {
    38      "units": "samples",
    39      "aggregation": "sum",
    40      "display-name": "cpu_samples",
    41      "sampled": True
    42    }
    43  }
    44  
    45  # Form data to be sent
    46  multipart_form_data = {
    47      'profile': ('example.pprof', open(pprof_file_path, 'rb')),
    48      'sample_type_config': ('config.json', json.dumps(sample_type_config))
    49  }
    50  
    51  # Sending the request with authentication
    52  response = requests.post(
    53      pyroscope_url,
    54      files=multipart_form_data,
    55      auth=HTTPBasicAuth(basic_auth_username, basic_auth_password)
    56  )
    57  
    58  # Checking the response
    59  if response.status_code == 200:
    60      print("Profile data successfully sent to Pyroscope.")
    61  else:
    62      print(f"Failed to send data. Status code: {response.status_code}, Message: {response.text}")