1313import java .net .Socket ;
1414import java .nio .file .Files ;
1515import java .nio .file .Paths ;
16+ import java .time .Duration ;
1617import java .util .*;
1718
19+ import org .springframework .http .client .SimpleClientHttpRequestFactory ;
20+
1821@ Service
1922public class K8sDiagnosticService {
2023
@@ -30,7 +33,10 @@ public class K8sDiagnosticService {
3033 private volatile String k8sInitError = null ;
3134
3235 public K8sDiagnosticService () {
33- this .restTemplate = new RestTemplate ();
36+ SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory ();
37+ factory .setConnectTimeout (Duration .ofSeconds (5 ));
38+ factory .setReadTimeout (Duration .ofSeconds (10 ));
39+ this .restTemplate = new RestTemplate (factory );
3440 initializeK8sClient ();
3541 logger .info ("K8s Diagnostic Service initialisiert (K8s API verfügbar: {})." , k8sInitialized );
3642 }
@@ -87,8 +93,8 @@ public Map<String, Object> getFullSidecarDetails() {
8793 String rawStats = restTemplate .getForObject (
8894 ENVOY_ADMIN_URL + "/stats?filter=.*(errors|5xx|timeout|retry|failed|reset|refused|overflow).*" ,
8995 String .class );
90- Map <String , String > activeErrors = parseErrorStatsOnly (rawStats );
91- health .put ("activeErrorMetrics" , parseErrorStatsOnly ( rawStats ) );
96+ Map <String , String > activeErrors = parseStats (rawStats , true );
97+ health .put ("activeErrorMetrics" , activeErrors );
9298 health .put ("errorCount" , activeErrors .size ());
9399 report .put ("healthDiagnostics" , health );
94100
@@ -100,27 +106,6 @@ public Map<String, Object> getFullSidecarDetails() {
100106 return report ;
101107 }
102108
103- private Map <String , String > parseErrorStatsOnly (String rawStats ) {
104- Map <String , String > errorMap = new TreeMap <>();
105- if (rawStats != null ) {
106- rawStats .lines ()
107- .filter (line -> line .contains (":" ))
108- .forEach (line -> {
109- int colonIndex = line .lastIndexOf (':' );
110- if (colonIndex > 0 && colonIndex < line .length () - 1 ) {
111- String key = line .substring (0 , colonIndex ).trim ();
112- String val = line .substring (colonIndex + 1 ).trim ();
113- try {
114- if (Long .parseLong (val ) > 0 ) {
115- errorMap .put (key , val );
116- }
117- } catch (NumberFormatException ignored ) {
118- }
119- }
120- });
121- }
122- return errorMap ;
123- }
124109
125110 private Map <String , Object > getEnvoyDetails () {
126111 Map <String , Object > envoy = new HashMap <>();
@@ -136,7 +121,7 @@ private Map<String, Object> getEnvoyDetails() {
136121 // Wichtige Netzwerk-Stats (Retries, Timeouts, 5xx Fehler)
137122 String stats = restTemplate .getForObject (
138123 ENVOY_ADMIN_URL + "/stats?filter=cluster.*.upstream_rq_(5xx|timeout|retry)" , String .class );
139- envoy .put ("networkStats" , parseStats (stats ));
124+ envoy .put ("networkStats" , parseStats (stats , false ));
140125
141126 } catch (Exception e ) {
142127 envoy .put ("error" , "Envoy Admin API nicht erreichbar: " + e .getMessage ());
@@ -178,16 +163,25 @@ private String summarizeClusters(String rawClusters) {
178163 return count + " aktive Upstream-Cluster-Einträge" ;
179164 }
180165
181- private Map <String , String > parseStats (String rawStats ) {
182- Map <String , String > statsMap = new HashMap <>();
166+ private Map <String , String > parseStats (String rawStats , boolean onlyNonZero ) {
167+ Map <String , String > statsMap = onlyNonZero ? new TreeMap <>() : new HashMap <>();
183168 if (rawStats != null ) {
184169 rawStats .lines ()
185170 .filter (line -> line .contains (":" ))
186171 .forEach (line -> {
187172 int colonIndex = line .lastIndexOf (':' );
188173 if (colonIndex > 0 && colonIndex < line .length () - 1 ) {
189- statsMap .put (line .substring (0 , colonIndex ).trim (),
190- line .substring (colonIndex + 1 ).trim ());
174+ String key = line .substring (0 , colonIndex ).trim ();
175+ String val = line .substring (colonIndex + 1 ).trim ();
176+ if (onlyNonZero ) {
177+ try {
178+ if (Long .parseLong (val ) > 0 ) {
179+ statsMap .put (key , val );
180+ }
181+ } catch (NumberFormatException ignored ) {}
182+ } else {
183+ statsMap .put (key , val );
184+ }
191185 }
192186 });
193187 }
0 commit comments