Skip to content

Commit b25678a

Browse files
committed
Add support for JSON format in CLI mode also
1 parent fc2c7fc commit b25678a

File tree

8 files changed

+569
-17
lines changed

8 files changed

+569
-17
lines changed

src/main/java/io/opentelemetry/contrib/generator/telemetry/cli/CLIProcessor.java

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,33 @@ public static void main(String[] args) throws ParseException {
4848
throw new GeneratorException("One of metricDefinition, logDefinition or traceDefinition must be provided");
4949
}
5050
PayloadHandler payloadHandler = getPayloadHandler(line.getOptionValue("t"));
51-
GeneratorInput.YAMLFilesBuilder inputBuilder = new GeneratorInput.YAMLFilesBuilder(line.getOptionValue("r"));
52-
if (line.hasOption("m")) {
53-
inputBuilder.withMetricDefinitionYAML(line.getOptionValue("m"));
54-
}
55-
if (line.hasOption("l")) {
56-
inputBuilder.withLogDefinitionYAML(line.getOptionValue("l"));
57-
}
58-
if (line.hasOption("s")) {
59-
inputBuilder.withTraceDefinitionYAML(line.getOptionValue("s"));
51+
GeneratorInput input;
52+
if (line.hasOption("j")) {
53+
GeneratorInput.JSONFilesBuilder inputBuilder = new GeneratorInput.JSONFilesBuilder(line.getOptionValue("r"));
54+
if (line.hasOption("m")) {
55+
inputBuilder.withMetricDefinitionJSON(line.getOptionValue("m"));
56+
}
57+
if (line.hasOption("l")) {
58+
inputBuilder.withLogDefinitionJSON(line.getOptionValue("l"));
59+
}
60+
if (line.hasOption("s")) {
61+
inputBuilder.withTraceDefinitionJSON(line.getOptionValue("s"));
62+
}
63+
input = inputBuilder.build();
64+
} else {
65+
GeneratorInput.YAMLFilesBuilder inputBuilder = new GeneratorInput.YAMLFilesBuilder(line.getOptionValue("r"));
66+
if (line.hasOption("m")) {
67+
inputBuilder.withMetricDefinitionYAML(line.getOptionValue("m"));
68+
}
69+
if (line.hasOption("l")) {
70+
inputBuilder.withLogDefinitionYAML(line.getOptionValue("l"));
71+
}
72+
if (line.hasOption("s")) {
73+
inputBuilder.withTraceDefinitionYAML(line.getOptionValue("s"));
74+
}
75+
input = inputBuilder.build();
6076
}
61-
TelemetryGenerator generator = new TelemetryGenerator(inputBuilder.build(), payloadHandler);
77+
TelemetryGenerator generator = new TelemetryGenerator(input, payloadHandler);
6278
generator.runGenerator();
6379
}
6480

@@ -95,13 +111,18 @@ private static Options getOptions() {
95111
.hasArg()
96112
.required()
97113
.build();
114+
Option jsonFormatFlag = Option.builder("j")
115+
.argName("jsonFormat")
116+
.longOpt("jsonFormat")
117+
.desc("Flag to use JSON format for input definitions")
118+
.build();
98119
Options options = new Options();
99120
options.addOption(resourceDefinition);
100121
options.addOption(metricDefinition);
101122
options.addOption(logsDefinition);
102123
options.addOption(traceDefinition);
103124
options.addOption(targetEnvYAML);
104-
125+
options.addOption(jsonFormatFlag);
105126
HelpFormatter formatter = new HelpFormatter();
106127
formatter.printHelp("test-telemetry-generator-all.jar", options, true);
107128
return options;

src/main/java/io/opentelemetry/contrib/generator/telemetry/dto/GeneratorInput.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,17 +139,17 @@ public JSONFilesBuilder(String resourceDefinitionJSON) {
139139
this.resourceDefinitionJSON = resourceDefinitionJSON;
140140
}
141141

142-
public JSONFilesBuilder withMetricDefinitionYAML(String metricDefinitionYAML) {
142+
public JSONFilesBuilder withMetricDefinitionJSON(String metricDefinitionYAML) {
143143
this.metricDefinitionJSON = metricDefinitionYAML;
144144
return this;
145145
}
146146

147-
public JSONFilesBuilder withLogDefinitionYAML(String logDefinitionYAML) {
147+
public JSONFilesBuilder withLogDefinitionJSON(String logDefinitionYAML) {
148148
this.logDefinitionJSON = logDefinitionYAML;
149149
return this;
150150
}
151151

152-
public JSONFilesBuilder withTraceDefinitionYAML(String traceDefinitionYAML) {
152+
public JSONFilesBuilder withTraceDefinitionJSON(String traceDefinitionYAML) {
153153
this.traceDefinitionJSON = traceDefinitionYAML;
154154
return this;
155155
}

src/test/java/io/opentelemetry/contrib/generator/telemetry/TestAllGeneratorsWithJSONInput.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public class TestAllGeneratorsWithJSONInput {
3232
@BeforeClass
3333
public void generateData() {
3434
GeneratorInput generatorInput = new GeneratorInput.JSONFilesBuilder(RESOURCES_JSON)
35-
.withMetricDefinitionYAML(METRICS_JSON)
36-
.withLogDefinitionYAML(LOGS_JSON)
37-
.withTraceDefinitionYAML(TRACES_JSON)
35+
.withMetricDefinitionJSON(METRICS_JSON)
36+
.withLogDefinitionJSON(LOGS_JSON)
37+
.withTraceDefinitionJSON(TRACES_JSON)
3838
.build();
3939
TelemetryGenerator telemetryGenerator = new TelemetryGenerator(generatorInput, payloadStore, true);
4040
telemetryGenerator.runGenerator();

src/test/java/io/opentelemetry/contrib/generator/telemetry/TestCLIProcessor.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,22 @@ public void testMetricsTracesBasicAuthGRPC() throws ParseException {
5252
CLIProcessor.main(cliArgs);
5353
}
5454

55+
@Test
56+
public void testWithJSONFormatInputs() throws ParseException {
57+
String cliResourcesPath = Paths.get(System.getProperty("user.dir"), "src", "test", "resources",
58+
"test-definitions", "cli", "json").toString();
59+
String RESOURCES_JSON = Paths.get(cliResourcesPath, "resource-definition.json").toString();
60+
String METRICS_JSON = Paths.get(cliResourcesPath, "metrics-cli-test.json").toString();
61+
String LOGS_JSON = Paths.get(cliResourcesPath, "logs-cli-test.json").toString();
62+
String TRACES_JSON = Paths.get(cliResourcesPath, "traces-cli-test.json").toString();
63+
String basicAuthTargetYAML = Paths.get(cliResourcesPath, "..", "target-basicauth.yaml").toString();
64+
String[] cliArgs = new String[] {
65+
"--resourceDefinition", RESOURCES_JSON, "--metricDefinition", METRICS_JSON, "--logDefinition", LOGS_JSON,
66+
"--spanDefinition", TRACES_JSON, "--target", basicAuthTargetYAML, "--jsonFormat"
67+
};
68+
CLIProcessor.main(cliArgs);
69+
}
70+
5571
@Test(expectedExceptions = MissingOptionException.class)
5672
public void testWithoutResourceDefinition() throws ParseException {
5773
String noAuthTargetYAML = Paths.get(cliResourcesPath, "target-noauth.yaml").toString();
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"globalPayloadFrequencySeconds": 15,
3+
"logs": [
4+
{
5+
"attributes": {
6+
"log.labels": "{\"generator\": roundRobin([\"Telemetry-Generator\", \"Telemetry-Generator-v2\"]), \"type\": \"k8s\"}"
7+
},
8+
"severityOrderFunction": "severityDistributionCount([\"INFO\", \"ERROR\", \"DEBUG\"], [1, 2, 3])",
9+
"payloadFrequencySeconds": 20,
10+
"payloadCount": 4,
11+
"copyCount": 10,
12+
"reportingResourcesCounts": {
13+
"container": 10,
14+
"pod": 10
15+
}
16+
},
17+
{
18+
"severityOrderFunction": "severityDistributionCount([\"ERROR\", \"WARN\", \"DEBUG\"], [1, 1, 4])",
19+
"payloadFrequencySeconds": 20,
20+
"payloadCount": 2,
21+
"copyCount": 200,
22+
"reportingResourcesCounts": {
23+
"node": 30
24+
}
25+
},
26+
{
27+
"severityOrderFunction": "severityDistributionPercentage([\"INFO\", \"TRACE\"], [20, 80])",
28+
"payloadFrequencySeconds": 10,
29+
"payloadCount": 4,
30+
"reportingResourcesCounts": {
31+
"machine": 4,
32+
"container": 150
33+
}
34+
}
35+
]
36+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
{
2+
"payloadFrequencySeconds": 10,
3+
"payloadCount": 6,
4+
"metrics": [
5+
{
6+
"name": "system.network.in.kb.sec",
7+
"unit": "kBy/s",
8+
"otelType": "summary",
9+
"valueFunction": "absoluteCosineSequenceSummary(\"*7000\", 5)",
10+
"isDouble": true,
11+
"quantiles": [
12+
0,
13+
50,
14+
100
15+
],
16+
"reportingResources": [
17+
"network_interface",
18+
"container",
19+
"machine"
20+
],
21+
"attributes": {
22+
"system.internal.ip": "IPv4Sequence(\"10.134.1.34\")"
23+
}
24+
},
25+
{
26+
"name": "system.network.out.kb.sec",
27+
"unit": "kBy/s",
28+
"otelType": "summary",
29+
"valueFunction": "randomSummary(1, 10, \"*400\", 7)",
30+
"isDouble": true,
31+
"quantiles": [
32+
0,
33+
50,
34+
100
35+
],
36+
"reportingResources": [
37+
"network_interface",
38+
"container",
39+
"machine"
40+
],
41+
"attributes": {
42+
"system.internal.ip": "IPv4Sequence(\"10.121.17.65\")",
43+
"network.device.type": "roundRobin([\"ethernet\", \"wired\", \"wireless\"])"
44+
}
45+
},
46+
{
47+
"name": "pod.restarts",
48+
"unit": 1,
49+
"otelType": "sum",
50+
"aggregationTemporality": "delta",
51+
"isMonotonic": true,
52+
"valueFunction": "arithmeticSequence(0, 1, \"/3\")",
53+
"reportingResources": [
54+
"pod"
55+
]
56+
},
57+
{
58+
"name": "cpu.used",
59+
"unit": "{cores}",
60+
"otelType": "gauge",
61+
"valueFunction": "absoluteSineSequence(\"*50\")",
62+
"isDouble": true,
63+
"reportingResources": [
64+
"node",
65+
"container",
66+
"machine",
67+
"pod"
68+
],
69+
"attributes": {
70+
"max.cpu.processes": "[counter(\"app-process-\"), roundRobin([\"kernel_task\", \"java\", \"mds\"])]"
71+
}
72+
},
73+
{
74+
"name": "filesystem.used",
75+
"unit": "MBy",
76+
"otelType": "sum",
77+
"aggregationTemporality": "delta",
78+
"valueFunction": "logarithmicSequence(30, 1, \"*1024\")",
79+
"isDouble": true,
80+
"reportingResources": [
81+
"disk",
82+
"aws_rds",
83+
"aws_ebs",
84+
"node",
85+
"container",
86+
"machine",
87+
"pod"
88+
]
89+
},
90+
{
91+
"name": "memory.used",
92+
"unit": "MBy",
93+
"otelType": "sum",
94+
"aggregationTemporality": "delta",
95+
"valueFunction": "controlledRandom(3072, 10240, \"\")",
96+
"reportingResources": [
97+
"aws_rds",
98+
"node",
99+
"container",
100+
"machine",
101+
"pod"
102+
]
103+
}
104+
]
105+
}

0 commit comments

Comments
 (0)