Skip to content

Commit 6e435d8

Browse files
authored
refactor: simplify the Accept http header process (#13275)
1 parent 284521c commit 6e435d8

File tree

9 files changed

+48
-79
lines changed

9 files changed

+48
-79
lines changed

docs/en/changes/changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* Storage: separate `SpanAttachedEventRecord` for SkyWalking trace and Zipkin trace.
2626
* [Break Change]BanyanDB: Setup new Group policy.
2727
* Bump up commons-beanutils to 1.11.0.
28+
* Refactor: simplify the `Accept` http header process.
2829

2930
#### UI
3031

docs/en/status/query_cluster_nodes.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,22 @@ This API is used to get the unified and effective TTL configurations.
1818
{
1919
"host": "10.0.12.23",
2020
"port": 11800,
21-
"isSelf": true
21+
"self": true
2222
},
2323
{
2424
"host": "10.0.12.25",
2525
"port": 11800,
26-
"isSelf": false
26+
"self": false
2727
},
2828
{
2929
"host": "10.0.12.37",
3030
"port": 11800,
31-
"isSelf": false
31+
"self": false
3232
}
3333
]
3434
}
3535
```
3636

3737
The `nodes` list all the nodes in the cluster. The size of the list should be exactly same as your cluster setup.
3838
The `host` and `port` are the address of the OAP node, which are used for OAP nodes communicating with each other. The
39-
`isSelf` is a flag to indicate whether the node is the current node, others are remote nodes.
39+
`self` is a flag to indicate whether the node is the current node, others are remote nodes.

oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/CoreModuleProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ public void prepare() throws ServiceNotProvidedException, ModuleStartException {
265265
httpServer.initialize();
266266

267267
this.registerServiceImplementation(ConfigService.class, new ConfigService(moduleConfig, this));
268-
this.registerServiceImplementation(ServerStatusService.class, new ServerStatusService(getManager(), moduleConfig));
268+
this.registerServiceImplementation(ServerStatusService.class, new ServerStatusService(getManager()));
269269
this.registerServiceImplementation(HierarchyDefinitionService.class, new HierarchyDefinitionService(moduleConfig));
270270
hierarchyService = new HierarchyService(getManager(), moduleConfig);
271271
this.registerServiceImplementation(HierarchyService.class, hierarchyService);

oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/status/ServerStatusService.java

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,11 @@
1818

1919
package org.apache.skywalking.oap.server.core.status;
2020

21-
import com.google.gson.Gson;
22-
import io.vavr.Tuple2;
23-
import java.util.ArrayList;
21+
import java.util.HashMap;
2422
import java.util.List;
2523
import java.util.concurrent.CopyOnWriteArrayList;
2624
import lombok.Getter;
2725
import lombok.RequiredArgsConstructor;
28-
import org.apache.skywalking.oap.server.core.CoreModuleConfig;
2926
import org.apache.skywalking.oap.server.library.module.ApplicationConfiguration;
3027
import org.apache.skywalking.oap.server.library.module.ModuleManager;
3128
import org.apache.skywalking.oap.server.library.module.Service;
@@ -43,7 +40,6 @@
4340
@RequiredArgsConstructor
4441
public class ServerStatusService implements Service {
4542
private final ModuleManager manager;
46-
private final CoreModuleConfig moduleConfig;
4743
@Getter
4844
private BootingStatus bootingStatus = new BootingStatus();
4945
@Getter
@@ -98,7 +94,7 @@ public ConfigList dumpBootingConfigurations(String keywords4MaskingSecretsOfConf
9894
for (ApplicationConfiguration.ModuleConfiguration configuration : configurations) {
9995
final String moduleName = configuration.getModuleName();
10096
if (configuration.getProviders().size() == 1) {
101-
configList.add(moduleName + ".provider", configuration.getProviders().keySet().iterator().next());
97+
configList.put(moduleName + ".provider", configuration.getProviders().keySet().iterator().next());
10298
}
10399
configuration.getProviders().forEach(
104100
(providerName, providerConfiguration) ->
@@ -109,41 +105,25 @@ public ConfigList dumpBootingConfigurations(String keywords4MaskingSecretsOfConf
109105
value = "******";
110106
}
111107
}
112-
configList.add(moduleName + "." + providerName + "." + key, value.toString());
108+
configList.put(moduleName + "." + providerName + "." + key, value.toString());
113109
}
114110
)
115111
);
116112
}
117113
return configList;
118114
}
119115

120-
public static class ConfigList {
121-
private final static Gson GSON = new Gson();
122-
private List<Tuple2> configurations = new ArrayList<>(200);
123-
124-
public void add(String key, String value) {
125-
configurations.add(new Tuple2<>(key, value));
126-
}
127-
116+
public static class ConfigList extends HashMap<String, String> {
128117
@Override
129118
public String toString() {
130119
StringBuilder configList = new StringBuilder();
131-
for (Tuple2 tuple : configurations) {
132-
configList.append(tuple._1)
120+
for (final var entry : this.entrySet()) {
121+
configList.append(entry.getKey())
133122
.append("=")
134-
.append(tuple._2)
123+
.append(entry.getValue())
135124
.append("\n");
136125
}
137126
return configList.toString();
138127
}
139-
140-
public String toJsonString() {
141-
return GSON.toJson(configurations.stream()
142-
.collect(
143-
java.util.stream.Collectors.toMap(
144-
tuple -> tuple._1.toString(),
145-
tuple -> tuple._2.toString()
146-
)));
147-
}
148128
}
149129
}

oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/ttl/TTLDefinition.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,18 @@
1818

1919
package org.apache.skywalking.oap.server.core.storage.ttl;
2020

21-
import com.google.gson.Gson;
2221
import lombok.Data;
2322

2423
/**
2524
* TTLDefinition defines the TTL of the data in the storage.
2625
*/
2726
@Data
2827
public class TTLDefinition {
29-
private final static Gson GSON = new Gson();
3028
private final MetricsTTL metrics;
3129
private final RecordsTTL records;
3230

33-
public String generateTTLDefinition() {
31+
@Override
32+
public String toString() {
3433
StringBuilder ttlDefinition = new StringBuilder();
3534
ttlDefinition.append("# Metrics TTL includes the definition of the TTL of the metrics-ish data in the storage,\n");
3635
ttlDefinition.append("# e.g.\n");
@@ -67,8 +66,4 @@ public String generateTTLDefinition() {
6766
ttlDefinition.append("records.browserErrorLog.cold=").append(records.getColdBrowserErrorLog()).append("\n");
6867
return ttlDefinition.toString();
6968
}
70-
71-
public String generateTTLDefinitionAsJSONStr() {
72-
return GSON.toJson(this);
73-
}
7469
}

oap-server/server-query-plugin/status-query-plugin/src/main/java/org/apache/skywalking/oap/query/debug/ClusterStatusQueryHandler.java

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,18 @@
1818

1919
package org.apache.skywalking.oap.query.debug;
2020

21-
import com.google.gson.JsonArray;
22-
import com.google.gson.JsonObject;
2321
import com.linecorp.armeria.common.HttpRequest;
24-
import com.linecorp.armeria.common.HttpResponse;
25-
import com.linecorp.armeria.common.MediaType;
2622
import com.linecorp.armeria.server.annotation.ExceptionHandler;
2723
import com.linecorp.armeria.server.annotation.Get;
24+
import com.linecorp.armeria.server.annotation.ProducesJson;
25+
2826
import lombok.extern.slf4j.Slf4j;
27+
28+
import java.util.Map;
29+
import java.util.stream.Collectors;
30+
2931
import org.apache.skywalking.oap.server.core.CoreModule;
30-
import org.apache.skywalking.oap.server.core.remote.client.Address;
32+
import org.apache.skywalking.oap.server.core.remote.client.RemoteClient;
3133
import org.apache.skywalking.oap.server.core.remote.client.RemoteClientManager;
3234
import org.apache.skywalking.oap.server.library.module.ModuleManager;
3335

@@ -50,22 +52,15 @@ private RemoteClientManager getRemoteClientManager() {
5052
return remoteClientManager;
5153
}
5254

55+
@ProducesJson
5356
@Get("/status/cluster/nodes")
54-
public HttpResponse buildClusterNodeList(HttpRequest request) {
55-
JsonObject clusterInfo = new JsonObject();
56-
57-
JsonArray nodeList = new JsonArray();
58-
clusterInfo.add("nodes", nodeList);
59-
getRemoteClientManager().getRemoteClient().stream().map(c -> {
60-
final Address address = c.getAddress();
61-
JsonObject node = new JsonObject();
62-
node.addProperty("host", address.getHost());
63-
node.addProperty("port", address.getPort());
64-
node.addProperty("isSelf", address.isSelf());
65-
return node;
66-
}).forEach(nodeList::add);
67-
68-
return HttpResponse.of(MediaType.JSON_UTF_8, clusterInfo.toString());
57+
public Map<String, ?> buildClusterNodeList(HttpRequest request) {
58+
return Map.of(
59+
"nodes",
60+
getRemoteClientManager().getRemoteClient()
61+
.stream()
62+
.map(RemoteClient::getAddress)
63+
.collect(Collectors.toList())
64+
);
6965
}
70-
7166
}

oap-server/server-query-plugin/status-query-plugin/src/main/java/org/apache/skywalking/oap/query/debug/DebuggingHTTPHandler.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@
2626
import com.google.common.reflect.TypeToken;
2727
import com.google.gson.Gson;
2828
import com.linecorp.armeria.common.AggregatedHttpResponse;
29-
import com.linecorp.armeria.common.HttpHeaderNames;
3029
import com.linecorp.armeria.common.HttpRequest;
3130
import com.linecorp.armeria.server.annotation.Default;
3231
import com.linecorp.armeria.server.annotation.ExceptionHandler;
3332
import com.linecorp.armeria.server.annotation.Get;
3433
import com.linecorp.armeria.server.annotation.Param;
34+
import com.linecorp.armeria.server.annotation.ProducesJson;
35+
import com.linecorp.armeria.server.annotation.ProducesText;
36+
3537
import java.util.ArrayList;
3638
import java.util.Arrays;
3739
import java.util.List;
@@ -83,6 +85,7 @@
8385
import org.apache.skywalking.oap.server.core.query.type.debugging.DebuggingTrace;
8486
import org.apache.skywalking.oap.server.core.query.type.debugging.DebuggingTraceContext;
8587
import org.apache.skywalking.oap.server.core.status.ServerStatusService;
88+
import org.apache.skywalking.oap.server.core.status.ServerStatusService.ConfigList;
8689
import org.apache.skywalking.oap.server.library.module.ModuleManager;
8790
import zipkin2.Span;
8891

@@ -110,14 +113,11 @@ public DebuggingHTTPHandler(final ModuleManager manager, final StatusQueryConfig
110113
this.logQuery = new LogQuery(manager);
111114
}
112115

116+
@ProducesText
117+
@ProducesJson
113118
@Get("/debugging/config/dump")
114-
public String dumpConfigurations(HttpRequest request) {
115-
final String acceptHeader = request.headers().get(HttpHeaderNames.ACCEPT);
116-
if (acceptHeader != null && acceptHeader.toLowerCase().contains("application/json")) {
117-
return serverStatusService.dumpBootingConfigurations(config.getKeywords4MaskingSecretsOfConfig())
118-
.toJsonString();
119-
}
120-
return serverStatusService.dumpBootingConfigurations(config.getKeywords4MaskingSecretsOfConfig()).toString();
119+
public ConfigList dumpConfigurations(HttpRequest request) {
120+
return serverStatusService.dumpBootingConfigurations(config.getKeywords4MaskingSecretsOfConfig());
121121
}
122122

123123
@SneakyThrows

oap-server/server-query-plugin/status-query-plugin/src/main/java/org/apache/skywalking/oap/query/debug/TTLConfigQueryHandler.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818

1919
package org.apache.skywalking.oap.query.debug;
2020

21-
import com.linecorp.armeria.common.HttpHeaderNames;
22-
import com.linecorp.armeria.common.HttpRequest;
23-
import com.linecorp.armeria.common.HttpResponse;
24-
import com.linecorp.armeria.common.MediaType;
2521
import com.linecorp.armeria.server.annotation.ExceptionHandler;
2622
import com.linecorp.armeria.server.annotation.Get;
23+
import com.linecorp.armeria.server.annotation.ProducesJson;
24+
import com.linecorp.armeria.server.annotation.ProducesText;
25+
2726
import lombok.extern.slf4j.Slf4j;
2827
import org.apache.skywalking.oap.server.core.CoreModule;
2928
import org.apache.skywalking.oap.server.core.query.TTLStatusQuery;
29+
import org.apache.skywalking.oap.server.core.storage.ttl.TTLDefinition;
3030
import org.apache.skywalking.oap.server.library.module.ModuleManager;
3131

3232
@Slf4j
@@ -48,12 +48,10 @@ private TTLStatusQuery getTTLStatusQuery() {
4848
return ttlStatusQuery;
4949
}
5050

51+
@ProducesText
52+
@ProducesJson
5153
@Get("/status/config/ttl")
52-
public HttpResponse affectedTTLConfigurations(HttpRequest request) {
53-
final String acceptHeader = request.headers().get(HttpHeaderNames.ACCEPT);
54-
if (acceptHeader != null && acceptHeader.toLowerCase().contains("application/json")) {
55-
return HttpResponse.of(MediaType.JSON_UTF_8, getTTLStatusQuery().getTTL().generateTTLDefinitionAsJSONStr());
56-
}
57-
return HttpResponse.of(MediaType.PLAIN_TEXT_UTF_8, getTTLStatusQuery().getTTL().generateTTLDefinition());
54+
public TTLDefinition effectiveTTLConfigurations() {
55+
return getTTLStatusQuery().getTTL();
5856
}
5957
}

oap-server/server-tools/profile-exporter/tool-profile-snapshot-server-mock/src/main/java/org/apache/skywalking/oap/server/tool/profile/core/MockCoreModuleProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public void prepare() throws ServiceNotProvidedException, ModuleStartException {
137137

138138
CoreModuleConfig moduleConfig = new CoreModuleConfig();
139139
this.registerServiceImplementation(ConfigService.class, new ConfigService(moduleConfig, this));
140-
this.registerServiceImplementation(ServerStatusService.class, new ServerStatusService(getManager(), moduleConfig));
140+
this.registerServiceImplementation(ServerStatusService.class, new ServerStatusService(getManager()));
141141
moduleConfig.setEnableHierarchy(false);
142142
this.registerServiceImplementation(HierarchyDefinitionService.class, new HierarchyDefinitionService(moduleConfig));
143143
this.registerServiceImplementation(HierarchyService.class, new HierarchyService(getManager(), moduleConfig));

0 commit comments

Comments
 (0)