Skip to content

Commit ccb997c

Browse files
russgoldrjeberhard
authored andcommitted
Handle string values in WME configuration
1 parent ee1a28f commit ccb997c

File tree

2 files changed

+67
-42
lines changed

2 files changed

+67
-42
lines changed

operator/src/main/java/oracle/kubernetes/weblogic/domain/model/MonitoringExporterConfiguration.java

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.gson.stream.JsonReader;
2020
import com.google.gson.stream.JsonToken;
2121
import com.google.gson.stream.JsonWriter;
22+
import jakarta.validation.constraints.NotNull;
2223
import oracle.kubernetes.json.Default;
2324
import oracle.kubernetes.json.Description;
2425
import org.apache.commons.lang3.builder.EqualsBuilder;
@@ -96,6 +97,8 @@ static class ExporterQuery extends HashMap<String,ExporterQuery> {
9697
@Description("The attributes for which metrics are to be output. If not specified and a prefix is defined, "
9798
+ "all values on the MBean will be selected.")
9899
private String[] values;
100+
101+
private Map<String,String[]> stringValues;
99102
}
100103

101104
// This class controls serialization of the exporter configuration.
@@ -136,6 +139,7 @@ private void writeQuery(JsonWriter out, ExporterQuery src) throws IOException {
136139
writeOptionalStringField(out, "type", src.type);
137140
writeOptionalStringField(out, "prefix", src.prefix);
138141
writeOptionalValueArray(out, src.values);
142+
writeOptionalStringValues(out, src.stringValues);
139143

140144
for (Map.Entry<String, ExporterQuery> entry : src.entrySet()) {
141145
out.name(entry.getKey());
@@ -153,16 +157,31 @@ private void writeOptionalStringField(JsonWriter out, String name, @Nullable Str
153157

154158
private void writeOptionalValueArray(JsonWriter out, @Nullable String[] values) throws IOException {
155159
if (values != null && values.length > 0) {
156-
out.name("values");
157-
out.beginArray();
158-
for (String value : values) {
159-
out.value(value);
160+
writeArray(out, "values", values);
161+
}
162+
}
163+
164+
private static void writeArray(JsonWriter out, String name, @NotNull String [] values) throws IOException {
165+
out.name(name);
166+
out.beginArray();
167+
for (String value : values) {
168+
out.value(value);
169+
}
170+
out.endArray();
171+
}
172+
173+
private void writeOptionalStringValues(JsonWriter out, @Nullable Map<String, String[]> stringValues) throws IOException {
174+
if (stringValues != null && !stringValues.isEmpty()) {
175+
out.name("stringValues");
176+
out.beginObject();
177+
for (String key: stringValues.keySet()) {
178+
writeArray(out, key, stringValues.get(key));
160179
}
161-
out.endArray();
180+
out.endObject();
162181
}
163182
}
164183

165-
@Override
184+
@Override
166185
public MonitoringExporterConfiguration read(JsonReader in) throws IOException {
167186
MonitoringExporterConfiguration configuration = new MonitoringExporterConfiguration();
168187
in.beginObject();
@@ -202,23 +221,13 @@ private ExporterQuery readQuery(JsonReader in) throws IOException {
202221
while (in.hasNext()) {
203222
String name = in.nextName();
204223
switch (name) {
205-
case "key":
206-
query.key = in.nextString();
207-
break;
208-
case "keyName":
209-
query.keyName = in.nextString();
210-
break;
211-
case "type":
212-
query.type = in.nextString();
213-
break;
214-
case "prefix":
215-
query.prefix = in.nextString();
216-
break;
217-
case "values":
218-
query.values = readArrayValue(in);
219-
break;
220-
default:
221-
query.put(name, readQuery(in));
224+
case "key" -> query.key = in.nextString();
225+
case "keyName" -> query.keyName = in.nextString();
226+
case "type" -> query.type = in.nextString();
227+
case "prefix" -> query.prefix = in.nextString();
228+
case "values" -> query.values = readArrayValue(in);
229+
case "stringValues" -> query.stringValues = readStringValues(in);
230+
default -> query.put(name, readQuery(in));
222231
}
223232
}
224233
in.endObject();
@@ -242,6 +251,17 @@ private void readStringArray(JsonReader in, List<String> result) throws IOExcept
242251
}
243252
in.endArray();
244253
}
254+
255+
private Map<String,String[]> readStringValues(JsonReader in) throws IOException {
256+
Map<String,String[]> result = new HashMap<>();
257+
in.beginObject();
258+
while (in.hasNext()) {
259+
String name = in.nextName();
260+
result.put(name, readArrayValue(in));
261+
}
262+
in.endObject();
263+
return result;
264+
}
245265
}
246266

247267
@Override

operator/src/test/java/oracle/kubernetes/weblogic/domain/model/MonitoringExporterConfigurationTest.java

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2021, Oracle and/or its affiliates.
1+
// Copyright (c) 2021, 2025, 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

44
package oracle.kubernetes.weblogic.domain.model;
@@ -35,26 +35,31 @@ void tearDown() {
3535
void deserializeFromJson() {
3636
final MonitoringExporterConfiguration configuration = MonitoringExporterConfiguration.createFromYaml(CONFIG);
3737

38-
assertThat(configuration.asJsonString(), hasJsonPath("$.metricsNameSnakeCase", equalTo(true)));
39-
assertThat(configuration.asJsonString(), hasJsonPath("$.queries[0].applicationRuntimes.key", equalTo("name")));
40-
assertThat(configuration.asJsonString(),
41-
hasJsonPath("$.queries[0].applicationRuntimes.componentRuntimes.type", equalTo("WebAppComponentRuntime")));
38+
final String jsonString = configuration.asJsonString();
39+
assertThat(jsonString, hasJsonPath("$.metricsNameSnakeCase", equalTo(true)));
40+
assertThat(jsonString, hasJsonPath("$.queries[0].applicationRuntimes.key", equalTo("name")));
41+
assertThat(jsonString, hasJsonPath("$.queries[0].applicationRuntimes.componentRuntimes.type", equalTo("WebAppComponentRuntime")));
42+
assertThat(configuration.matchesYaml(CONFIG), is(true));
4243
}
4344

44-
private static final String CONFIG = "---\n"
45-
+ "metricsNameSnakeCase: true\n"
46-
+ "queries:\n"
47-
+ "- applicationRuntimes:\n"
48-
+ " key: name\n"
49-
+ " componentRuntimes:\n"
50-
+ " type: WebAppComponentRuntime\n"
51-
+ " prefix: webapp_config_\n"
52-
+ " key: name\n"
53-
+ " values: [deploymentState, type, contextRoot, sourceInfo, openSessionsHighCount]\n"
54-
+ " servlets:\n"
55-
+ " prefix: weblogic_servlet_\n"
56-
+ " key: servletName\n"
57-
+ " values: [invocationTotalCount, executionTimeTotal]\n";
45+
private static final String CONFIG = """
46+
---
47+
metricsNameSnakeCase: true
48+
queries:
49+
- applicationRuntimes:
50+
key: name
51+
componentRuntimes:
52+
type: WebAppComponentRuntime
53+
prefix: webapp_config_
54+
key: name
55+
values: [deploymentState, type, contextRoot, sourceInfo, openSessionsHighCount]
56+
stringValues:
57+
state: [ok,failed,overloaded,critical,warn]
58+
servlets:
59+
prefix: weblogic_servlet_
60+
key: servletName
61+
values: [invocationTotalCount, executionTimeTotal]
62+
""";
5863

5964
@Test
6065
void matchVisuallyDifferentYaml() {

0 commit comments

Comments
 (0)