Skip to content

Commit

Permalink
metric-schema includes the java version by default as a javaVersion
Browse files Browse the repository at this point in the history
… tag (#832)

metric-schema includes the java version by default as a `javaVersion` tag, for example `javaVersion:17.0.3`
  • Loading branch information
carterkozak authored Aug 2, 2022
1 parent db8f073 commit d460ee1
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 10 deletions.
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-832.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: improvement
improvement:
description: metric-schema includes the java version by default as a `javaVersion`
tag, for example `javaVersion:17.0.3`
links:
- https://github.com/palantir/metric-schema/pull/832

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@
/** Field and parameter names used by this generator. */
final class ReservedNames {

static final String LIBRARY_NAME = "LIBRARY_NAME";
static final String LIBRARY_VERSION = "LIBRARY_VERSION";
static final String LIBRARY_NAME_FIELD = "LIBRARY_NAME";
static final String LIBRARY_NAME_TAG = "libraryName";
static final String LIBRARY_VERSION_FIELD = "LIBRARY_VERSION";
static final String LIBRARY_VERSION_TAG = "libraryVersion";
static final String JAVA_VERSION_FIELD = "JAVA_VERSION";
static final String JAVA_VERSION_TAG = "javaVersion";
static final String FACTORY_METHOD = "of";
static final String GAUGE_NAME = "gauge";
static final String REGISTRY_NAME = "registry";

private static final ImmutableSet<String> RESERVED_NAMES =
ImmutableSet.of(FACTORY_METHOD, GAUGE_NAME, REGISTRY_NAME, LIBRARY_NAME, LIBRARY_VERSION);
private static final ImmutableSet<String> RESERVED_NAMES = ImmutableSet.of(
FACTORY_METHOD, GAUGE_NAME, JAVA_VERSION_FIELD, LIBRARY_NAME_FIELD, LIBRARY_VERSION_FIELD, REGISTRY_NAME);

/** Returns true if the input string cannot be used. */
static boolean isValid(String input) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.codahale.metrics.Gauge;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Iterables;
import com.google.errorprone.annotations.CheckReturnValue;
import com.palantir.logsafe.Preconditions;
Expand Down Expand Up @@ -60,15 +61,27 @@ static JavaFile generateUtilityClass(
Preconditions.class)
.returns(className)
.build())
.addField(TaggedMetricRegistry.class, ReservedNames.REGISTRY_NAME, Modifier.PRIVATE, Modifier.FINAL);
.addField(TaggedMetricRegistry.class, ReservedNames.REGISTRY_NAME, Modifier.PRIVATE, Modifier.FINAL)
.addField(FieldSpec.builder(
String.class,
ReservedNames.JAVA_VERSION_FIELD,
Modifier.PRIVATE,
Modifier.STATIC,
Modifier.FINAL)
.initializer("$T.getProperty($S, $S)", System.class, "java.version", "unknown")
.build());
if (libraryName.isPresent()) {
builder.addField(FieldSpec.builder(
String.class, ReservedNames.LIBRARY_NAME, Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL)
String.class,
ReservedNames.LIBRARY_NAME_FIELD,
Modifier.PRIVATE,
Modifier.STATIC,
Modifier.FINAL)
.initializer("$S", libraryName.get())
.build());
builder.addField(FieldSpec.builder(
String.class,
ReservedNames.LIBRARY_VERSION,
ReservedNames.LIBRARY_VERSION_FIELD,
Modifier.PRIVATE,
Modifier.STATIC,
Modifier.FINAL)
Expand Down Expand Up @@ -158,13 +171,28 @@ private static CodeBlock metricName(
".putSafeTags($S, $L.getValue())", tagDef.getName(), Custodian.sanitizeName(tagDef.getName()));
}
});
ImmutableSortedSet<String> insensitiveTags = insensitiveTags(definition);
if (libraryName.isPresent()) {
builder.add(".putSafeTags(\"libraryName\", $L)", ReservedNames.LIBRARY_NAME);
builder.add(".putSafeTags(\"libraryVersion\", $L)", ReservedNames.LIBRARY_VERSION);
if (!insensitiveTags.contains(ReservedNames.LIBRARY_NAME_TAG)) {
builder.add(".putSafeTags($S, $L)", ReservedNames.LIBRARY_NAME_TAG, ReservedNames.LIBRARY_NAME_FIELD);
}
if (!insensitiveTags.contains(ReservedNames.LIBRARY_VERSION_TAG)) {
builder.add(
".putSafeTags($S, $L)", ReservedNames.LIBRARY_VERSION_TAG, ReservedNames.LIBRARY_VERSION_FIELD);
}
}
if (!insensitiveTags.contains(ReservedNames.JAVA_VERSION_TAG)) {
builder.add(".putSafeTags($S, $L)", ReservedNames.JAVA_VERSION_TAG, ReservedNames.JAVA_VERSION_FIELD);
}
return builder.add(".build()").build();
}

private static ImmutableSortedSet<String> insensitiveTags(MetricDefinition definition) {
return definition.getTagDefinitions().stream()
.map(TagDefinition::getName)
.collect(ImmutableSortedSet.toImmutableSortedSet(String.CASE_INSENSITIVE_ORDER));
}

private static MethodSpec generateToString(ClassName className) {
return MethodSpec.methodBuilder("toString")
.addModifiers(Modifier.PUBLIC)
Expand Down
Loading

0 comments on commit d460ee1

Please sign in to comment.