1- // Copyright (c) 2017, 2022 , Oracle and/or its affiliates.
1+ // Copyright (c) 2017, 2023 , Oracle and/or its affiliates.
22// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33
44package com .oracle .wls .exporter .domain ;
55
6+ import java .util .HashMap ;
7+ import java .util .Map ;
8+ import java .util .Optional ;
9+
610import com .google .gson .JsonArray ;
711import com .google .gson .JsonElement ;
812import com .google .gson .JsonObject ;
913import com .google .gson .JsonPrimitive ;
1014
11- import java .util .HashMap ;
12- import java .util .Map ;
13- import java .util .Optional ;
14-
1515import static com .oracle .wls .exporter .domain .MapUtils .isNullOrEmptyString ;
1616
1717/**
@@ -100,16 +100,21 @@ private String[] getKeysAsArray() {
100100
101101 private void scrapeItem () {
102102 if (excludeByType ()) return ;
103- String itemQualifiers = getItemQualifiers ();
103+ final String itemQualifiers = getItemQualifiers ();
104104
105105 for (String valueName : getValueNames ()) {
106- if (valueName .equals (selector .getKey ())) continue ;
107- JsonElement value = object .get (valueName );
108- addMetric (itemQualifiers , valueName , value );
106+ scrapeValue (itemQualifiers , valueName );
109107 }
108+
110109 scrapeSubObjects (itemQualifiers );
111110 }
112111
112+ private void scrapeValue (String itemQualifiers , String valueName ) {
113+ if (valueName .equals (selector .getKey ())) return ;
114+
115+ new ScrapedMetric (itemQualifiers , valueName ).add ();
116+ }
117+
113118 private boolean excludeByType () {
114119 final JsonElement typeField = object .get ("type" );
115120 final String typeFilter = selector .getType ();
@@ -133,43 +138,60 @@ private String asQuotedString(JsonElement jsonElement) {
133138 return QUOTE + jsonElement .getAsString () + QUOTE ;
134139 }
135140
136- private void addMetric (String itemQualifiers , String valueName , JsonElement value ) {
137- if (value != null && value .isJsonPrimitive ()) {
138- addMetric (itemQualifiers , valueName , value .getAsJsonPrimitive ());
141+
142+ class ScrapedMetric {
143+ private final String itemQualifiers ;
144+ private final String valueName ;
145+ private final JsonPrimitive jsonPrimitive ;
146+
147+ ScrapedMetric (String itemQualifiers , String valueName ) {
148+ this .itemQualifiers = itemQualifiers ;
149+ this .valueName = valueName ;
150+
151+ this .jsonPrimitive = Optional .ofNullable (object .get (valueName ))
152+ .filter (JsonElement ::isJsonPrimitive )
153+ .map (JsonElement ::getAsJsonPrimitive )
154+ .orElse (null );
139155 }
140- }
141156
142- private void addMetric (String itemQualifiers , String valueName , JsonPrimitive jsonPrimitive ) {
143- Optional .ofNullable (getMetricValue (valueName , jsonPrimitive ))
144- .ifPresent (value -> addMetric (getMetricName (itemQualifiers , valueName ), value ));
145- }
157+ void add () {
158+ Optional .ofNullable (jsonPrimitive ).map (this ::toMetricValue ).ifPresent (v -> metrics .put (getMetricName (), v ));
159+ }
146160
147- private void addMetric (String metricName , Object value ) {
148- metrics .put (metricName , value );
149- }
161+ private Object toMetricValue (JsonPrimitive jsonPrimitive ) {
162+ if (jsonPrimitive .isNumber ())
163+ return jsonPrimitive .getAsNumber ();
164+ else if (isStringMetric ())
165+ return selector .getStringMetricValue (valueName , jsonPrimitive .getAsString ());
166+ else if (selector .acceptsStrings () && jsonPrimitive .isString ())
167+ return jsonPrimitive .getAsString ();
168+ else
169+ return null ;
170+ }
150171
151- private Object getMetricValue (String valueName , JsonPrimitive jsonPrimitive ) {
152- if (jsonPrimitive .isNumber ())
153- return jsonPrimitive .getAsNumber ();
154- else if (selector .isStringMetric (valueName ) && jsonPrimitive .isString ())
155- return selector .getStringMetricValue (valueName , jsonPrimitive .getAsString ());
156- else if (selector .acceptsStrings () && jsonPrimitive .isString ())
157- return jsonPrimitive .getAsString ();
158- else
159- return null ;
160- }
172+ private boolean isStringMetric () {
173+ return selector .isStringMetric (valueName ) && jsonPrimitive .isString ();
174+ }
161175
162- private String getMetricName ( String itemQualifiers , String valueName ) {
163- StringBuilder sb = new StringBuilder ();
164- if (selector .getPrefix () != null ) sb .append (getCorrectCase (selector .getPrefix ()));
165- sb .append (getCorrectCase (valueName ));
166- if (!isNullOrEmptyString (itemQualifiers ))
167- sb .append ('{' ).append (itemQualifiers ).append ('}' );
168- return sb .toString ();
169- }
176+ private String getMetricName ( ) {
177+ StringBuilder sb = new StringBuilder ();
178+ if (selector .getPrefix () != null ) sb .append (getCorrectCase (selector .getPrefix ()));
179+ sb .append (getCorrectCase (valueName ));
180+ if (!isNullOrEmptyString (itemQualifiers ))
181+ sb .append ('{' ).append (augmented ( itemQualifiers ) ).append ('}' );
182+ return sb .toString ();
183+ }
170184
171- private String getCorrectCase (String valueName ) {
172- return metricNameSnakeCase ? SnakeCaseUtil .convert (valueName ) : valueName ;
185+ private String getCorrectCase (String valueName ) {
186+ return metricNameSnakeCase ? SnakeCaseUtil .convert (valueName ) : valueName ;
187+ }
188+
189+ private String augmented (String itemQualifiers ) {
190+ if (isStringMetric ())
191+ return itemQualifiers + ",value=\" " + jsonPrimitive .getAsString () + '"' ;
192+ else
193+ return itemQualifiers ;
194+ }
173195 }
174196 }
175197
0 commit comments