Skip to content

Commit f84c7fd

Browse files
committed
simplify
1 parent 597cfe1 commit f84c7fd

4 files changed

Lines changed: 102 additions & 102 deletions

File tree

instrumentation-docs/src/main/java/io/opentelemetry/instrumentation/docs/internal/EmittedMetrics.java

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ public static class Metric {
9696
private String name;
9797
private String description;
9898
private String type;
99-
private String instrumentType;
10099

101100
@Nullable
102101
@JsonProperty("is_monotonic")
@@ -114,7 +113,6 @@ public Metric(
114113
this.name = name;
115114
this.description = description;
116115
this.type = type;
117-
this.instrumentType = "";
118116
this.isMonotonic = null;
119117
this.unit = unit;
120118
this.attributes = attributes;
@@ -124,30 +122,12 @@ public Metric(
124122
String name,
125123
String description,
126124
String type,
127-
String instrumentType,
128-
String unit,
129-
List<TelemetryAttribute> attributes) {
130-
this.name = name;
131-
this.description = description;
132-
this.type = type;
133-
this.instrumentType = instrumentType;
134-
this.isMonotonic = null;
135-
this.unit = unit;
136-
this.attributes = attributes;
137-
}
138-
139-
public Metric(
140-
String name,
141-
String description,
142-
String type,
143-
String instrumentType,
144125
@Nullable Boolean isMonotonic,
145126
String unit,
146127
List<TelemetryAttribute> attributes) {
147128
this.name = name;
148129
this.description = description;
149130
this.type = type;
150-
this.instrumentType = instrumentType;
151131
this.isMonotonic = isMonotonic;
152132
this.unit = unit;
153133
this.attributes = attributes;
@@ -157,7 +137,6 @@ public Metric() {
157137
this.name = "";
158138
this.description = "";
159139
this.type = "";
160-
this.instrumentType = "";
161140
this.unit = "";
162141
this.attributes = new ArrayList<>();
163142
}
@@ -187,11 +166,38 @@ public void setType(String type) {
187166
}
188167

189168
public String getInstrumentType() {
190-
return instrumentType;
169+
return inferInstrumentType(this.type, this.isMonotonic);
191170
}
192171

193-
public void setInstrumentType(String instrumentType) {
194-
this.instrumentType = instrumentType;
172+
/**
173+
* Infers the InstrumentType from the MetricDataType string and isMonotonic flag.
174+
*
175+
* @param metricDataType the MetricDataType string (e.g., "LONG_SUM", "DOUBLE_GAUGE")
176+
* @param isMonotonic whether the metric is monotonic (for SUM types), null if not applicable
177+
* @return the inferred InstrumentType string
178+
*/
179+
private static String inferInstrumentType(
180+
String metricDataType, @Nullable Boolean isMonotonic) {
181+
if (metricDataType == null || metricDataType.isEmpty()) {
182+
return "";
183+
}
184+
185+
return switch (metricDataType) {
186+
case "HISTOGRAM", "EXPONENTIAL_HISTOGRAM", "SUMMARY" -> "histogram";
187+
case "LONG_GAUGE", "DOUBLE_GAUGE" -> "gauge";
188+
case "LONG_SUM", "DOUBLE_SUM" -> {
189+
// Use isMonotonic flag to distinguish between COUNTER and UP_DOWN_COUNTER
190+
if (isMonotonic != null && isMonotonic) {
191+
yield "counter";
192+
} else if (isMonotonic != null) {
193+
yield "updowncounter";
194+
} else {
195+
// Unknown, default to counter
196+
yield "counter";
197+
}
198+
}
199+
default -> "";
200+
};
195201
}
196202

197203
@Nullable
@@ -229,7 +235,6 @@ public static class Builder {
229235
private String name = "";
230236
private String description = "";
231237
private String type = "";
232-
private String instrumentType = "";
233238
@Nullable private Boolean isMonotonic = null;
234239
private String unit = "";
235240
private List<TelemetryAttribute> attributes = new ArrayList<>();
@@ -252,12 +257,6 @@ public Builder type(String type) {
252257
return this;
253258
}
254259

255-
@CanIgnoreReturnValue
256-
public Builder instrumentType(String instrumentType) {
257-
this.instrumentType = instrumentType;
258-
return this;
259-
}
260-
261260
@CanIgnoreReturnValue
262261
public Builder isMonotonic(@Nullable Boolean isMonotonic) {
263262
this.isMonotonic = isMonotonic;
@@ -277,7 +276,7 @@ public Builder attributes(List<TelemetryAttribute> attributes) {
277276
}
278277

279278
public Metric build() {
280-
return new Metric(name, description, type, instrumentType, isMonotonic, unit, attributes);
279+
return new Metric(name, description, type, isMonotonic, unit, attributes);
281280
}
282281
}
283282

instrumentation-docs/src/main/java/io/opentelemetry/instrumentation/docs/parsers/EmittedMetricsParser.java

Lines changed: 2 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.Map;
2222
import java.util.logging.Logger;
2323
import java.util.stream.Stream;
24-
import javax.annotation.Nullable;
2524

2625
/**
2726
* This class is responsible for parsing metric-* files from the `.telemetry` directory of an
@@ -124,9 +123,7 @@ private static Map<String, EmittedMetrics> aggregateMetricsByScope(
124123
new EmittedMetrics.MetricsByScope(
125124
scopeEntry.getKey(), new ArrayList<>(scopeEntry.getValue().values())));
126125
}
127-
EmittedMetrics emittedMetrics = new EmittedMetrics(when, mergedScopes);
128-
enrichMetricsWithInstrumentType(emittedMetrics);
129-
result.put(when, emittedMetrics);
126+
result.put(when, new EmittedMetrics(when, mergedScopes));
130127
}
131128
return result;
132129
}
@@ -164,69 +161,10 @@ public static Map<String, EmittedMetrics> parseMetrics(Map<String, StringBuilder
164161
deduplicatedScopes.add(
165162
new EmittedMetrics.MetricsByScope(scope, new ArrayList<>(dedupedMetrics.values())));
166163
}
167-
EmittedMetrics emittedMetrics = new EmittedMetrics(when, deduplicatedScopes);
168-
enrichMetricsWithInstrumentType(emittedMetrics);
169-
metricsMap.put(when, emittedMetrics);
164+
metricsMap.put(when, new EmittedMetrics(when, deduplicatedScopes));
170165
}
171166
return metricsMap;
172167
}
173168

174-
/**
175-
* Infers the InstrumentType from the MetricDataType string and isMonotonic flag, since MetricData
176-
* only contains the aggregated type (e.g., LONG_SUM, DOUBLE_GAUGE)
177-
*
178-
* @param metricDataType the MetricDataType string (e.g., "LONG_SUM", "DOUBLE_GAUGE")
179-
* @param isMonotonic whether the metric is monotonic (for SUM types), null if not applicable
180-
* @return the inferred InstrumentType string
181-
*/
182-
private static String inferInstrumentType(String metricDataType, @Nullable Boolean isMonotonic) {
183-
if (metricDataType == null || metricDataType.isEmpty()) {
184-
return "UNKNOWN";
185-
}
186-
187-
return switch (metricDataType) {
188-
case "HISTOGRAM", "EXPONENTIAL_HISTOGRAM", "SUMMARY" -> "histogram";
189-
case "LONG_GAUGE", "DOUBLE_GAUGE" -> "gauge";
190-
case "LONG_SUM", "DOUBLE_SUM" -> {
191-
// Use isMonotonic flag to distinguish between COUNTER and UP_DOWN_COUNTER
192-
if (isMonotonic != null && isMonotonic) {
193-
yield "counter";
194-
} else if (isMonotonic != null) {
195-
yield "updowncounter";
196-
} else {
197-
// Unknown, default to counter
198-
yield "counter";
199-
}
200-
}
201-
default -> "UNKNOWN";
202-
};
203-
}
204-
205-
/**
206-
* Populates the instrumentType field for each metric based on its MetricDataType and isMonotonic
207-
* flag. This is called after parsing the YAML to enrich the metric data with inferred instrument
208-
* types.
209-
*
210-
* @param metrics the EmittedMetrics object to enrich
211-
*/
212-
private static void enrichMetricsWithInstrumentType(EmittedMetrics metrics) {
213-
if (metrics.getMetricsByScope() == null) {
214-
return;
215-
}
216-
217-
for (EmittedMetrics.MetricsByScope scope : metrics.getMetricsByScope()) {
218-
if (scope.getMetrics() == null) {
219-
continue;
220-
}
221-
222-
for (EmittedMetrics.Metric metric : scope.getMetrics()) {
223-
if (metric.getInstrumentType() == null || metric.getInstrumentType().isEmpty()) {
224-
String inferredType = inferInstrumentType(metric.getType(), metric.getIsMonotonic());
225-
metric.setInstrumentType(inferredType);
226-
}
227-
}
228-
}
229-
}
230-
231169
private EmittedMetricsParser() {}
232170
}

instrumentation-docs/src/main/java/io/opentelemetry/instrumentation/docs/parsers/MetricParser.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ public static Map<String, Map<String, AggregatedMetricInfo>> aggregateMetrics(
120120
metric.getName(),
121121
metric.getDescription(),
122122
metric.getType(),
123-
metric.getInstrumentType(),
124123
metric.getIsMonotonic(),
125124
metric.getUnit()));
126125
if (metric.getAttributes() != null) {
@@ -153,7 +152,6 @@ public static Map<String, List<EmittedMetrics.Metric>> buildFilteredMetrics(
153152
.name(aggInfo.name)
154153
.description(aggInfo.description)
155154
.type(aggInfo.type)
156-
.instrumentType(aggInfo.instrumentType)
157155
.isMonotonic(aggInfo.isMonotonic)
158156
.unit(aggInfo.unit)
159157
.attributes(new ArrayList<>(aggInfo.attributes))
@@ -168,7 +166,6 @@ static class AggregatedMetricInfo {
168166
final String name;
169167
final String description;
170168
final String type;
171-
final String instrumentType;
172169
@Nullable final Boolean isMonotonic;
173170
final String unit;
174171
final Set<TelemetryAttribute> attributes = new HashSet<>();
@@ -177,13 +174,11 @@ static class AggregatedMetricInfo {
177174
String name,
178175
String description,
179176
String type,
180-
String instrumentType,
181177
@Nullable Boolean isMonotonic,
182178
String unit) {
183179
this.name = name;
184180
this.description = description;
185181
this.type = type;
186-
this.instrumentType = instrumentType;
187182
this.isMonotonic = isMonotonic;
188183
this.unit = unit;
189184
}

instrumentation-docs/src/test/java/io/opentelemetry/instrumentation/docs/utils/YamlHelperTest.java

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,8 @@ void testMetricsParsing() throws Exception {
388388
metrics:
389389
- name: db.client.operation.duration
390390
description: Duration of database client operations.
391-
type: HISTOGRAM
391+
instrument: histogram
392+
data_type: HISTOGRAM
392393
unit: s
393394
attributes:
394395
- name: db.namespace
@@ -406,6 +407,73 @@ void testMetricsParsing() throws Exception {
406407
assertThat(expectedYaml).isEqualTo(stringWriter.toString());
407408
}
408409

410+
@Test
411+
void testMetricsWithDifferentInstrumentTypes() throws Exception {
412+
List<InstrumentationModule> modules = new ArrayList<>();
413+
414+
List<EmittedMetrics.Metric> metrics =
415+
List.of(
416+
new EmittedMetrics.Metric("test.histogram", "desc", "HISTOGRAM", "s", emptyList()),
417+
new EmittedMetrics.Metric("test.counter", "desc", "LONG_SUM", true, "1", emptyList()),
418+
new EmittedMetrics.Metric(
419+
"test.updowncounter", "desc", "LONG_SUM", false, "1", emptyList()),
420+
new EmittedMetrics.Metric("test.gauge", "desc", "DOUBLE_GAUGE", "{test}", emptyList()));
421+
422+
modules.add(
423+
new InstrumentationModule.Builder()
424+
.srcPath("instrumentation/test/test-1.0")
425+
.instrumentationName("test-1.0")
426+
.namespace("test")
427+
.group("test")
428+
.metrics(Map.of("default", metrics))
429+
.build());
430+
431+
StringWriter stringWriter = new StringWriter();
432+
BufferedWriter writer = new BufferedWriter(stringWriter);
433+
434+
YamlHelper.generateInstrumentationYaml(modules, writer);
435+
writer.flush();
436+
437+
String expectedYaml =
438+
"""
439+
libraries:
440+
test:
441+
- name: test-1.0
442+
source_path: instrumentation/test/test-1.0
443+
scope:
444+
name: io.opentelemetry.test-1.0
445+
telemetry:
446+
- when: default
447+
metrics:
448+
- name: test.counter
449+
description: desc
450+
instrument: counter
451+
data_type: LONG_SUM
452+
unit: '1'
453+
attributes: []
454+
- name: test.gauge
455+
description: desc
456+
instrument: gauge
457+
data_type: DOUBLE_GAUGE
458+
unit: '{test}'
459+
attributes: []
460+
- name: test.histogram
461+
description: desc
462+
instrument: histogram
463+
data_type: HISTOGRAM
464+
unit: s
465+
attributes: []
466+
- name: test.updowncounter
467+
description: desc
468+
instrument: updowncounter
469+
data_type: LONG_SUM
470+
unit: '1'
471+
attributes: []
472+
""";
473+
474+
assertThat(expectedYaml).isEqualTo(stringWriter.toString());
475+
}
476+
409477
@Test
410478
void testSpanParsing() throws Exception {
411479
List<InstrumentationModule> modules = new ArrayList<>();

0 commit comments

Comments
 (0)