Skip to content

Commit

Permalink
feat: agg metric name format (#712)
Browse files Browse the repository at this point in the history
  • Loading branch information
xzchaoo authored Oct 25, 2023
1 parent 48f9823 commit 3ab7037
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
import java.util.Map;
import java.util.Set;

import org.apache.commons.text.StringSubstitutor;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import io.holoinsight.server.agg.v1.core.Utils;
import io.holoinsight.server.agg.v1.executor.output.AggStringLookup;
import io.holoinsight.server.agg.v1.executor.output.MergedCompleteness;
import io.holoinsight.server.agg.v1.executor.output.XOutput;
import io.holoinsight.server.extension.MetricStorage;
Expand Down Expand Up @@ -47,7 +49,10 @@ public void write(Batch batch) {
long time0 = System.currentTimeMillis();

String baseName = batch.oi.getName();
boolean useFormatName = baseName.contains("%s");
AggStringLookup aggStringLookup = new AggStringLookup();
boolean useFormatName = baseName.contains("$");
StringSubstitutor stringSubstitutor = new StringSubstitutor(aggStringLookup);

String ts = Utils.formatTimeShort(batch.window.getTimestamp());

boolean debug = false;
Expand All @@ -63,15 +68,13 @@ public void write(Batch batch) {
}

String metricName;
if ("value".equals(e.getKey())) {
metricName = baseName;
if (useFormatName) {
aggStringLookup.bind(e.getKey(), g.tags, batch.key.getPartitionInfo());
metricName = stringSubstitutor.replace(baseName);
} else {
if (useFormatName) {
metricName = String.format(baseName, e.getKey());
} else {
metricName = baseName + "_" + e.getKey();
}
metricName = baseName;
}

usedMetricNames.add(metricName);

try {
Expand Down
4 changes: 4 additions & 0 deletions server/agg/agg-executor-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
</dependency>
</dependencies>


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/
package io.holoinsight.server.agg.v1.executor.output;

import java.util.Map;

import org.apache.commons.text.lookup.StringLookup;

import io.holoinsight.server.agg.v1.executor.executor.FixedSizeTags;

/**
* <p>
* created at 2023/10/25
*
* @author xzchaoo
*/
public class AggStringLookup implements StringLookup {

private static final String UNKNOWN = "UNKNOWN";

private String fieldName;
private FixedSizeTags tags;
private Map<String, String> partition;

public void bind(String fieldName, FixedSizeTags tags, Map<String, String> partition) {
this.fieldName = fieldName;
this.tags = tags;
this.partition = partition;
}

@Override
public String lookup(String key) {
if (fieldName != null && "field".equals(key)) {
return fieldName;
}

if (tags != null && key.startsWith("tag.")) {
return tags.getTagValue(key.substring("tag.".length()), UNKNOWN);
}

if (partition != null && key.startsWith("partition.")) {
return partition.getOrDefault(key.substring("partition.".length()), UNKNOWN);
}

return UNKNOWN;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import java.util.Map;

import org.apache.commons.text.StringSubstitutor;

import com.alibaba.fastjson.JSON;

import io.holoinsight.server.agg.v1.core.Utils;
Expand All @@ -21,6 +23,7 @@
@Slf4j
public class XConsoleOutput implements XOutput {
public static final String TYPE = "CONSOLE";
private boolean printMultiFields = true;

@Override
public String type() {
Expand All @@ -32,15 +35,42 @@ public void write(Batch batch) {
AggTaskKey key = batch.key;
WindowInfo w = batch.window;
OutputItem oi = batch.oi;

String baseName = batch.oi.getName();
AggStringLookup aggStringLookup = new AggStringLookup();
boolean useFormatName = baseName.contains("$");
StringSubstitutor stringSubstitutor = new StringSubstitutor(aggStringLookup);

for (Group g : batch.getGroups()) {
Map<String, Object> finalFields = g.finalFields;
log.info("[agg] [{}] emit name=[{}] ts=[{}] tags=[{}] fields={}", //
key, //
oi.getName(), //
Utils.formatTimeShort(w.getTimestamp()), //
g.getTags(), //
JSON.toJSONString(finalFields)); //

if (printMultiFields) {
log.info("[agg] [{}] emit name=[{}] ts=[{}] tags=[{}] fields={}", //
key, //
oi.getName(), //
Utils.formatTimeShort(w.getTimestamp()), //
g.getTags(), //
JSON.toJSONString(finalFields)); //
} else {
for (Map.Entry<String, Object> e : finalFields.entrySet()) {
String metricName;
if (useFormatName) {
aggStringLookup.bind(e.getKey(), g.tags, batch.key.getPartitionInfo());
metricName = stringSubstitutor.replace(baseName);
} else {
metricName = baseName;
}
log.info("[agg] [{}] emit name=[{}] ts=[{}] metric=[{}] tags=[{}] value=[{}]", //
key, //
oi.getName(), //
Utils.formatTimeShort(w.getTimestamp()), //
metricName, g.getTags(), //
e.getValue()); //
}
}

}

}

}
6 changes: 6 additions & 0 deletions server/holoinsight-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@
<artifactId>hll</artifactId>
<version>1.6.0</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.10.0</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down

0 comments on commit 3ab7037

Please sign in to comment.