wu-sheng commented on a change in pull request #6495:
URL: https://github.com/apache/skywalking/pull/6495#discussion_r588252751
##########
File path:
oap-server/analyzer/meter-analyzer/src/main/java/org/apache/skywalking/oap/meter/analyzer/dsl/SampleFamily.java
##########
@@ -376,15 +374,63 @@ public SampleFamily endpoint(List<String> serviceKeys,
List<String> endpointKeys
if (this == EMPTY) {
return EMPTY;
}
- this.context.setMeterEntity(MeterEntity.newEndpoint(dim(serviceKeys),
dim(endpointKeys)));
- return left(io.vavr.collection.Stream.concat(serviceKeys,
endpointKeys).asJava());
+ return createMeterSamples(serviceKeys, endpointKeys,
ScopeType.ENDPOINT);
+ }
+
+ private SampleFamily createMeterSamples(List<String> serviceKeys,
List<String> level2keys, ScopeType scopeType) {
+ final List<String> labelKeys =
io.vavr.collection.Stream.concat(serviceKeys, level2keys).asJava();
+ Map<MeterEntity, Sample[]> meterSamples = new HashMap<>();
+ Arrays.stream(samples)
+ .collect(groupingBy(it -> getLabels(labelKeys, it),
mapping(identity(), toList())))
+ .forEach((labels, samples) -> {
+ MeterEntity meterEntity = getMeterEntity(samples,
serviceKeys, level2keys, scopeType);
+ Preconditions.checkNotNull(meterEntity);
+ meterSamples.put(meterEntity, left(labelKeys, samples));
+ });
+
+ this.context.setMeterSamples(meterSamples);
+ //This samples is original, The grouped samples is in context which
mapping with MeterEntity
+ return SampleFamily.build(this.context, samples);
+ }
+
+ private MeterEntity getMeterEntity(List<Sample> samples,
+ List<String> serviceKeys,
+ List<String> level2keys,
+ ScopeType scopeType) {
+ switch (scopeType) {
+ case SERVICE:
+ return MeterEntity.newService(dim(serviceKeys, samples));
+ case SERVICE_INSTANCE:
+ return MeterEntity.newServiceInstance(dim(serviceKeys,
samples), dim(level2keys, samples));
+ case ENDPOINT:
+ return MeterEntity.newEndpoint(dim(serviceKeys, samples),
dim(level2keys, samples));
+ }
+ return null;
}
private String dim(List<String> labelKeys) {
- String name = labelKeys.stream().map(k ->
samples[0].labels.getOrDefault(k, "")).collect(Collectors.joining("."));
+ String name = labelKeys.stream()
+ .map(k -> samples[0].labels.getOrDefault(k, ""))
+ .collect(Collectors.joining("."));
+ return CharMatcher.is('.').trimFrom(name);
+ }
+
+ private String dim(List<String> labelKeys, List<Sample> samples) {
+ String name = labelKeys.stream()
+ .map(k -> samples.get(0).labels.getOrDefault(k,
""))
+ .collect(Collectors.joining("."));
return CharMatcher.is('.').trimFrom(name);
}
+ private Sample[] left(List<String> labelKeys, List<Sample> samples) {
Review comment:
@hanahmily These `dim` and `left` seem internal APIs, and as it takes
`List<Sample> samples` as object, we should change these 2 methods as static.
Please confirm.
@wankai123 `List<Sample> samples` should be the 1st parameter as the target.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]