diff --git a/src/gprofiler/backend/routers/metrics_routes.py b/src/gprofiler/backend/routers/metrics_routes.py index c64a9fca..c12a603b 100644 --- a/src/gprofiler/backend/routers/metrics_routes.py +++ b/src/gprofiler/backend/routers/metrics_routes.py @@ -251,17 +251,33 @@ def calculate_trend_in_cpu( def get_html_metadata( fg_params: FGParamsBaseModel = Depends(flamegraph_base_request_params), ): + logger.info(f"get_html_metadata DEBUG - fg_params: {fg_params}") + logger.info(f"get_html_metadata DEBUG - filter: {fg_params.filter}") + host_name_value = get_rql_first_eq_key(fg_params.filter, FilterTypes.HOSTNAME_KEY) + logger.info(f"get_html_metadata DEBUG - extracted host_name_value: {host_name_value}") + if not host_name_value: + logger.error("get_html_metadata ERROR - No hostname filter provided") raise HTTPException(400, detail="Must filter by hostname to get the html metadata") + + logger.info(f"get_html_metadata DEBUG - Calling get_metrics_response with lookup_for='lasthtml'") s3_path = get_metrics_response(fg_params, lookup_for="lasthtml") + logger.info(f"get_html_metadata DEBUG - Received s3_path: {s3_path}") + if not s3_path: + logger.error("get_html_metadata ERROR - No s3_path returned from get_metrics_response") raise HTTPException(404, detail="The html metadata path not found in CH") + s3_dal = S3ProfileDal(logger) try: + logger.info(f"get_html_metadata DEBUG - Fetching S3 object: {s3_path}") html_content = s3_dal.get_object(s3_path, is_gzip=True) - except ClientError: + logger.info(f"get_html_metadata DEBUG - Successfully fetched HTML content, length: {len(html_content)}") + except ClientError as e: + logger.error(f"get_html_metadata ERROR - S3 ClientError: {e}") raise HTTPException(status_code=404, detail="The html metadata file not found in S3") + return HTMLMetadata(content=html_content) diff --git a/src/gprofiler/backend/utils/request_utils.py b/src/gprofiler/backend/utils/request_utils.py index 583187f5..1522f647 100644 --- a/src/gprofiler/backend/utils/request_utils.py +++ b/src/gprofiler/backend/utils/request_utils.py @@ -170,7 +170,13 @@ def get_metrics_response( compared_start_datetime=None, compared_end_datetime=None, ) -> Union[List, Dict, str]: + logger.info(f"get_metrics_response DEBUG - fg_params: {fg_params}") + logger.info(f"get_metrics_response DEBUG - lookup_for: {lookup_for}") + logger.info(f"get_metrics_response DEBUG - filter: {fg_params.filter}") + fg_filter = fg_params.filter.json().encode() if fg_params.filter else None + logger.info(f"get_metrics_response DEBUG - encoded fg_filter: {fg_filter}") + db_api_params = get_api_params( fg_params.service_name, fg_params.start_time, @@ -182,13 +188,26 @@ def get_metrics_response( lookup_for=lookup_for, interval=interval, ) + logger.info(f"get_metrics_response DEBUG - db_api_params: {db_api_params}") + + url = f"{QUERY_API_BASE_URL}/api/v1/metrics/{lookup_for}" + logger.info(f"get_metrics_response DEBUG - Making request to: {url}") + logger.info(f"get_metrics_response DEBUG - Request params: {db_api_params}") + try: response = requests.get( - url=f"{QUERY_API_BASE_URL}/api/v1/metrics/{lookup_for}", + url=url, params=db_api_params, verify=REST_CERTIFICATE_PATH, auth=(REST_USERNAME, REST_PASSWORD), ) - except requests.exceptions.ConnectionError: + logger.info(f"get_metrics_response DEBUG - Response status: {response.status_code}") + if response.status_code != 200: + logger.error(f"get_metrics_response ERROR - Response content: {response.text}") + except requests.exceptions.ConnectionError as e: + logger.error(f"get_metrics_response ERROR - Connection error: {e}") raise HTTPException(status_code=502, detail="Failed connect to flamedb api") - return _common_fg_rest_response(response, db_api_params) + + result = _common_fg_rest_response(response, db_api_params) + logger.info(f"get_metrics_response DEBUG - Final result: {result}") + return result diff --git a/src/gprofiler_flamedb_rest/db/clickhouse.go b/src/gprofiler_flamedb_rest/db/clickhouse.go index f641531a..243ba2fb 100644 --- a/src/gprofiler_flamedb_rest/db/clickhouse.go +++ b/src/gprofiler_flamedb_rest/db/clickhouse.go @@ -1076,11 +1076,26 @@ func (c *ClickHouseClient) FetchSessionsCount(ctx context.Context, params common func (c *ClickHouseClient) FetchLastHTML(ctx context.Context, params common.MetricsLastHTMLParams, filterQuery string) (string, error) { + // Debug: Log input parameters + log.Printf("FetchLastHTML DEBUG - ServiceId: %d", params.ServiceId) + log.Printf("FetchLastHTML DEBUG - StartTime: %s", common.FormatTime(params.StartDateTime)) + log.Printf("FetchLastHTML DEBUG - EndTime: %s", common.FormatTime(params.EndDateTime)) + log.Printf("FetchLastHTML DEBUG - HostName filter: %v", params.HostName) + log.Printf("FetchLastHTML DEBUG - FilterQuery: %s", filterQuery) + _, conditions := BuildConditions(params.ContainerName, params.HostName, params.InstanceType, params.K8SObject, filterQuery) + + // Debug: Log generated conditions + log.Printf("FetchLastHTML DEBUG - Generated conditions: %s", conditions) + query := fmt.Sprintf(` SELECT argMax(HTMLPath,Timestamp) FROM flamedb.metrics WHERE ServiceId = %d AND (Timestamp BETWEEN '%s' AND '%s') %s; `, params.ServiceId, common.FormatTime(params.StartDateTime), common.FormatTime(params.EndDateTime), conditions) + + // Debug: Log final query + log.Printf("FetchLastHTML DEBUG - Final query: %s", query) + rows, err := c.client.Query(query) if err == nil { defer rows.Close() @@ -1088,13 +1103,17 @@ func (c *ClickHouseClient) FetchLastHTML(ctx context.Context, params common.Metr var htmlPath string err = rows.Scan(&htmlPath) if err != nil { - log.Printf("error scan result: %v", err) + log.Printf("FetchLastHTML ERROR - scan result: %v", err) + } else { + log.Printf("FetchLastHTML DEBUG - Found HTMLPath: %s", htmlPath) } return htmlPath, nil } err = rows.Err() } else { - log.Println(err) + log.Printf("FetchLastHTML ERROR - query execution: %v", err) } + + log.Printf("FetchLastHTML DEBUG - No results found, returning empty string") return "", nil }