This repository was archived by the owner on Jul 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDefaultExpressionBuilder.java
More file actions
79 lines (65 loc) · 2.22 KB
/
DefaultExpressionBuilder.java
File metadata and controls
79 lines (65 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.wavefront.labs.convert;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.wavefront.labs.convert.name.NameProcessor;
import com.wavefront.labs.convert.name.NameRule;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
import java.util.regex.Matcher;
public class DefaultExpressionBuilder implements ExpressionBuilder {
private static final Logger logger = LogManager.getLogger(DefaultExpressionBuilder.class);
protected Properties properties;
protected NameProcessor nameProcessor;
@Override
public void init(Properties properties) {
this.properties = properties;
try {
File nameProcessorFile = new File(properties.getProperty("convert.name.processor.file", "name-processor.yaml"));
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
nameProcessor = mapper.readValue(nameProcessorFile, new TypeReference<NameProcessor>() {
});
} catch (IOException e) {
logger.error("Error reading name processor file.", e);
nameProcessor = new NameProcessor();
}
}
@Override
public String buildMetricName(String orig) {
return orig.replaceAll("[^a-zA-Z0-9_,./\\-]", "-");
}
@Override
public String buildExpression(Object data) {
String name = buildName(data.toString());
String metricName = buildMetricName(name);
return "ts(\"" + metricName + "\")";
}
protected String buildName(String name) {
return buildName(name, null);
}
protected String buildName(String name, String type) {
if (type == null || type.equals("")) {
type = "metric";
}
HashMap<String, String> nameMap = nameProcessor.getMap(type);
if (nameMap.containsKey(name)) {
name = nameMap.get(name);
} else {
List<NameRule> rules = nameProcessor.getRules(type);
for (NameRule rule : rules) {
if (rule.getMatchPattern().matcher(name).matches()) {
Matcher matcher = rule.getSearchPattern().matcher(name);
if (matcher.find()) {
name = matcher.replaceAll(rule.getReplace());
}
}
}
}
return name;
}
}