Skip to content

Commit

Permalink
Fix metrics collector NPE (#890)
Browse files Browse the repository at this point in the history
Fixes #734, #585
  • Loading branch information
chenjjl authored Aug 25, 2023
1 parent addf2fb commit 7654400
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,24 +103,36 @@ public List<MetricDTO> searchService(@RequestParam String ip, @RequestParam Stri
protected void addMetricsConfigToMap(Map<String, String> configMap, String ip) {
List<Provider> providers = providerService.findByAddress(ip);
if (providers.size() > 0) {
Provider provider = providers.get(0);
String service = provider.getService();
MetadataIdentifier providerIdentifier = new MetadataIdentifier(Tool.getInterface(service), Tool.getVersion(service), Tool.getGroup(service),
Constants.PROVIDER_SIDE, provider.getApplication());
String metaData = providerService.getProviderMetaData(providerIdentifier);
FullServiceDefinition providerServiceDefinition = new Gson().fromJson(metaData, FullServiceDefinition.class);
Map<String, String> parameters = providerServiceDefinition.getParameters();
configMap.put(parameters.get(Constants.METRICS_PORT), parameters.get(Constants.METRICS_PROTOCOL));
for (int i = 0; i < providers.size() && configMap.isEmpty(); i++) {
Provider provider = providers.get(i);
String service = provider.getService();
MetadataIdentifier providerIdentifier = new MetadataIdentifier(Tool.getInterface(service), Tool.getVersion(service), Tool.getGroup(service),
Constants.PROVIDER_SIDE, provider.getApplication());
String metaData = providerService.getProviderMetaData(providerIdentifier);
FullServiceDefinition providerServiceDefinition = new Gson().fromJson(metaData, FullServiceDefinition.class);
Map<String, String> parameters = providerServiceDefinition.getParameters();
String metricsPort = parameters.get(Constants.METRICS_PORT);
String metricsProtocol = parameters.get(Constants.METRICS_PROTOCOL);
if (metricsPort != null && metricsProtocol != null) {
configMap.put(metricsPort, metricsProtocol);
}
}
} else {
List<Consumer> consumers = consumerService.findByAddress(ip);
if (consumers.size() > 0) {
Consumer consumer = consumers.get(0);
String service = consumer.getService();
MetadataIdentifier consumerIdentifier = new MetadataIdentifier(Tool.getInterface(service), Tool.getVersion(service), Tool.getGroup(service),
Constants.CONSUMER_SIDE, consumer.getApplication());
String metaData = consumerService.getConsumerMetadata(consumerIdentifier);
Map<String, String> consumerParameters = new Gson().fromJson(metaData, Map.class);
configMap.put(consumerParameters.get(Constants.METRICS_PORT), consumerParameters.get(Constants.METRICS_PROTOCOL));
for (int i = 0; i < consumers.size() && configMap.isEmpty(); i++) {
Consumer consumer = consumers.get(i);
String service = consumer.getService();
MetadataIdentifier consumerIdentifier = new MetadataIdentifier(Tool.getInterface(service), Tool.getVersion(service), Tool.getGroup(service),
Constants.CONSUMER_SIDE, consumer.getApplication());
String metaData = consumerService.getConsumerMetadata(consumerIdentifier);
Map<String, String> consumerParameters = new Gson().fromJson(metaData, Map.class);
String metricsPort = consumerParameters.get(Constants.METRICS_PORT);
String metricsProtocol = consumerParameters.get(Constants.METRICS_PROTOCOL);
if (metricsPort != null && metricsProtocol != null) {
configMap.put(metricsPort, metricsProtocol);
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public MetrcisCollectServiceImpl() {
referenceConfig = new ReferenceConfig<>();
referenceConfig.setApplication(new ApplicationConfig("dubbo-admin"));
referenceConfig.setInterface(MetricsService.class);
//Keep it consistent with the ConfigManager cache
referenceConfig.setSticky(false);
}

public void setUrl(String url) {
Expand Down

0 comments on commit 7654400

Please sign in to comment.