This is an automated email from the ASF dual-hosted git repository.

sadadw1 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozhera.git


The following commit(s) were added to refs/heads/master by this push:
     new e1a39c98 fix: encode Prometheus query params so a literal '+' in label 
values is not decoded to space (#689)
e1a39c98 is described below

commit e1a39c981ddb909d39bda4f5f84e11c67135e6a7
Author: jiangpingping <[email protected]>
AuthorDate: Thu Sep 3 16:51:35 2026 +0800

    fix: encode Prometheus query params so a literal '+' in label values is not 
decoded to space (#689)
    
    RestTemplateService.getHttpM relied on RestTemplate URI template variable
    expansion, which keeps a literal '+' in the query string. Prometheus then
    decodes '+' as a space per application/x-www-form-urlencoded, so a label
    value such as serverEnv containing '+' (e.g. '...+c3') fails exact matching
    and the query returns no data.
    
    Encode each query parameter value once via UriUtils.encode (which, unlike
    encodeQueryParam, encodes '+' as %2B since '+' is not an unreserved
    character) and pass a pre-encoded URI to avoid double encoding. This also
    fixes the ali branch which previously did not expand the {query} 
placeholder.
    
    Signed-off-by: itlemon <[email protected]>
---
 .../monitor/service/http/RestTemplateService.java  | 33 ++++++++++++++++++++--
 1 file changed, 30 insertions(+), 3 deletions(-)

diff --git 
a/ozhera-monitor/ozhera-monitor-service/src/main/java/org/apache/ozhera/monitor/service/http/RestTemplateService.java
 
b/ozhera-monitor/ozhera-monitor-service/src/main/java/org/apache/ozhera/monitor/service/http/RestTemplateService.java
index d0fad71f..f5b2fdd0 100644
--- 
a/ozhera-monitor/ozhera-monitor-service/src/main/java/org/apache/ozhera/monitor/service/http/RestTemplateService.java
+++ 
b/ozhera-monitor/ozhera-monitor-service/src/main/java/org/apache/ozhera/monitor/service/http/RestTemplateService.java
@@ -28,7 +28,10 @@ import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 import org.springframework.web.client.RestClientException;
 import org.springframework.web.client.RestTemplate;
+import org.springframework.web.util.UriUtils;
 
+import java.net.URI;
+import java.nio.charset.StandardCharsets;
 import java.util.Map;
 import java.util.Set;
 
@@ -69,16 +72,19 @@ public class RestTemplateService {
         String result = null;
         try {
             if (!CollectionUtils.isEmpty(map)) {
-                url = expandURLByMap(url, map);
+                url = buildEncodedUrl(url, map);
             }
+            // The url has already been query-encoded exactly once; use the 
URI overload so that
+            // RestTemplate does not expand/encode it again (prevents double 
encoding).
+            URI uri = URI.create(url);
             if (prometheusClusterType.equals("ali")) {
                 HttpHeaders headers = new HttpHeaders();
                 headers.add("Authorization", prometheusHeaderToken);
                 HttpEntity<String> entity = new HttpEntity<>(headers);
-                ResponseEntity<String> response = restTemplate.exchange(url, 
HttpMethod.GET, entity, String.class);
+                ResponseEntity<String> response = restTemplate.exchange(uri, 
HttpMethod.GET, entity, String.class);
                 result = response.getBody();
             } else {
-                result = restTemplate.getForObject(url, String.class, map);
+                result = restTemplate.getForObject(uri, String.class);
             }
             log.info("RestTemplateService.getHttp url : {}, map : {},result : 
{} ", url, map, result);
         } catch (RestClientException e) {
@@ -88,6 +94,27 @@ public class RestTemplateService {
         return result;
     }
 
+    /**
+     * Build the full URL by manually encoding each parameter value.
+     * UriUtils.encode encodes every character that is not unreserved, so '+' 
becomes %2B and a
+     * space becomes %20. This is required because UriUtils.encodeQueryParam 
treats '+' as a legal
+     * query character and leaves it as-is, in which case the server (e.g. 
Prometheus) would decode
+     * the literal '+' into a space per the application/x-www-form-urlencoded 
rule. As a result a
+     * label value such as serverEnv containing '+' would fail exact matching 
and return no data.
+     */
+    private static String buildEncodedUrl(String url, Map<String, Object> map) 
{
+        StringBuilder sb = new StringBuilder(url);
+        sb.append("?");
+        Set<Map.Entry<String, Object>> entries = map.entrySet();
+        for (Map.Entry<String, Object> entry : entries) {
+            sb.append(entry.getKey())
+                    .append("=")
+                    .append(UriUtils.encode(String.valueOf(entry.getValue()), 
StandardCharsets.UTF_8))
+                    .append("&");
+        }
+        return sb.deleteCharAt(sb.length() - 1).toString();
+    }
+
     public String getHttpMPost(String url, com.alibaba.fastjson.JSONObject 
param, MediaType mediaType){
 
         log.info("RestTemplateService.getHttpMPost 
url:{},param:{},mediaType:{}",url,param,mediaType);


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to