Skip to content

Commit ec6838a

Browse files
authored
Improvement: Inline single value tags (#443)
Inline single value tags
1 parent 8a9c840 commit ec6838a

File tree

4 files changed

+36
-16
lines changed

4 files changed

+36
-16
lines changed

changelog/@unreleased/pr-443.v2.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type: improvement
2+
improvement:
3+
description: Inline single value tags
4+
links:
5+
- https://github.com/palantir/metric-schema/pull/443

metric-schema-java/src/integrationInput/java/com/palantir/test/MonitorsMetrics.java

+16-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

metric-schema-java/src/main/java/com/palantir/metric/schema/UtilityGenerator.java

+13-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.codahale.metrics.Gauge;
2020
import com.google.common.collect.ImmutableList;
21+
import com.google.common.collect.Iterables;
2122
import com.google.errorprone.annotations.CheckReturnValue;
2223
import com.palantir.logsafe.Preconditions;
2324
import com.palantir.tritium.metrics.registry.MetricName;
@@ -106,7 +107,7 @@ private static void generateConstants(
106107
MetricDefinition definition,
107108
ImplementationVisibility visibility) {
108109
definition.getTagDefinitions().forEach(tagDef -> {
109-
if (tagDef.getValues().isEmpty()) {
110+
if (tagDef.getValues().size() <= 1) {
110111
return;
111112
}
112113

@@ -145,6 +146,11 @@ private static CodeBlock metricName(
145146
definition.getTagDefinitions().forEach(tagDef -> {
146147
if (tagDef.getValues().isEmpty()) {
147148
builder.add(".putSafeTags($S, $L)", tagDef.getName(), Custodian.sanitizeName(tagDef.getName()));
149+
} else if (tagDef.getValues().size() == 1) {
150+
builder.add(
151+
".putSafeTags($S, $S)",
152+
tagDef.getName(),
153+
Iterables.getOnlyElement(tagDef.getValues()).getValue());
148154
} else {
149155
builder.add(
150156
".putSafeTags($S, $L.getValue())", tagDef.getName(), Custodian.sanitizeName(tagDef.getName()));
@@ -250,8 +256,9 @@ private static void generateMetricFactoryBuilder(
250256
.addModifiers(visibility.apply())
251257
.addMethods(abstractBuildMethods)
252258
.build());
253-
ImmutableList<TagDefinition> tagList =
254-
definition.getTagDefinitions().stream().collect(ImmutableList.toImmutableList());
259+
ImmutableList<TagDefinition> tagList = definition.getTagDefinitions().stream()
260+
.filter(tagDefinition -> tagDefinition.getValues().size() != 1)
261+
.collect(ImmutableList.toImmutableList());
255262
for (int i = 0; i < tagList.size(); i++) {
256263
boolean lastTag = i == tagList.size() - 1;
257264
TagDefinition tag = tagList.get(i);
@@ -355,7 +362,9 @@ private static void generateMetricFactoryBuilder(
355362
}
356363

357364
private static int numArgs(MetricDefinition definition) {
358-
return definition.getTagDefinitions().size()
365+
return (int) definition.getTagDefinitions().stream()
366+
.filter(tagDefinition -> tagDefinition.getValues().size() != 1)
367+
.count()
359368
// Gauges require a gauge argument.
360369
+ (MetricType.GAUGE.equals(definition.getType()) ? 1 : 0);
361370
}

metric-schema-java/src/test/resources/constant-tags.yml

+2
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ namespaces:
1212
- type
1313
- name: locator
1414
values: [ package:identifier ]
15+
- name: otherLocator
16+
values: [ package:identifier, package:identifier2 ]

0 commit comments

Comments
 (0)