prometheus-api-client-python icon indicating copy to clipboard operation
prometheus-api-client-python copied to clipboard

Clarification on `get_metric_range_data` method

Open kev-pebble opened this issue 8 months ago • 3 comments

I am querying the Prometheus Host. I specify the start, end time and chunksize as 1 hour. When querying the data between the start and end times with 1 hour chunksize, is the data returned aggregated sum by the hour or simply the value at the hourly timestamp? My metric is a counter metric like kepler_container_joules_total, so the returned values as cumulative totals at the specified hourly timestamps?

def query_data(victoria_db_url, start_time, end_time, metric_name=None):

    prom = PrometheusConnect(url=victoria_db_url, disable_ssl=True)
    
    # Run query
    result = prom.all_metrics()
    
    kepler_metrics = [metric for metric in result if 'kepler' in metric]
    
    # save dfs as list
    df_list = []

    # set default metric
    if metric_name == None:
        metric_name == "kepler_container_joules_total"
        
    # Query the data
    for metric in kepler_metrics:

        if metric == metric_name:
        
            #print(f"Metric: {metric}")
            data = prom.get_metric_range_data(
                                                metric_name=metric,
                                                start_time = start_time,
                                                end_time = end_time,
                                                chunk_size = timedelta(hours=1)
                                             )
            
            if data:
                # Convert the metric data to a DataFrame
                metric_df = MetricSnapshotDataFrame(data)
                # Append the DataFrame to the list
                df_list.append(metric_df)

    df_kepler = pd.concat(df_list, ignore_index=True)

    return df_kepler

Is there a way to get aggregate hourly power consumption without the cumulative / counter time series data?

@4n4nd

kev-pebble avatar Feb 10 '25 23:02 kev-pebble