Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version metric should be a string instead of an integer. #352

Merged
merged 1 commit into from
Nov 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 0 additions & 26 deletions components/proxy/src/main/java/com/hotels/styx/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Objects;
import java.util.Optional;

import static com.google.common.base.Objects.toStringHelper;
import static java.lang.Integer.parseInt;
import static org.slf4j.LoggerFactory.getLogger;

/**
Expand Down Expand Up @@ -76,31 +74,7 @@ public String releaseVersion() {
return releaseTag.substring(firstDot + 1);
}

public Optional<Integer> buildNumber() {
String releaseVersion = releaseVersion();

Optional<String> buildNumberAsString = extractFinalInt(releaseVersion);

return buildNumberAsString.flatMap(this::parseInteger);
}

private Optional<String> extractFinalInt(String dotSeparatedIntegers) {
int lastDot = dotSeparatedIntegers.lastIndexOf('.');

if (lastDot == -1 || lastDot == dotSeparatedIntegers.length() - 1) {
return Optional.empty();
}

return Optional.of(dotSeparatedIntegers.substring(lastDot + 1));
}

private Optional<Integer> parseInteger(String string) {
try {
return Optional.of(parseInt(string));
} catch (NumberFormatException e) {
return Optional.empty();
}
}

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

import java.lang.management.RuntimeMXBean;
import java.time.Duration;
import java.util.Optional;

import static java.lang.String.format;
import static java.lang.management.ManagementFactory.getPlatformMBeanServer;
Expand All @@ -47,23 +46,13 @@ private CoreMetrics() {
}

public static void registerCoreMetrics(Version buildInfo, MetricRegistry metrics) {
registerVersionMetric(buildInfo, metrics);
registerVersionMetric(metrics, buildInfo.releaseVersion());
registerJvmMetrics(metrics);
metrics.register("os", new OperatingSystemMetricSet());
}

private static void registerVersionMetric(Version buildInfo, MetricRegistry metrics) {
Optional<Integer> buildNumber = buildInfo.buildNumber();

if (buildNumber.isPresent()) {
registerVersionMetric(metrics, buildNumber.get());
} else {
LOG.warn("Could not acquire build number from release version: {}", buildInfo);
}
}

private static void registerVersionMetric(MetricRegistry metricRegistry, Integer buildNumber) {
Gauge<Integer> versionGauge = () -> buildNumber;
private static void registerVersionMetric(MetricRegistry metricRegistry, String versionNumber) {
Gauge<String> versionGauge = () -> versionNumber;

metricRegistry.scope("styx").register("version.buildnumber", versionGauge);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import static com.hotels.styx.Version.newVersion;
import static com.hotels.styx.Version.readVersionFrom;
import static com.hotels.styx.support.matchers.IsOptional.isPresent;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -29,6 +30,7 @@ public class VersionTest {
public void readBuildInfoFromTheSpecifiedPath() {
Version version = readVersionFrom("/version.json");
assertThat(version.releaseTag(), containsString("STYX"));
assertThat(version.releaseVersion(), is("0.0-125"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import static org.hamcrest.Matchers.is;

public class CoreMetricsTest {
private final Version version = new Version("1.2.3");
private final Version version = new Version("STYX.1.2.3");

@Test
public void registersVersionMetric() {
Expand All @@ -37,7 +37,7 @@ public void registersVersionMetric() {

Gauge gauge = metrics.getGauges().get("styx.version.buildnumber");

assertThat(gauge.getValue(), is(3));
assertThat(gauge.getValue(), is("1.2.3"));
}

@Test
Expand Down
3 changes: 3 additions & 0 deletions components/proxy/src/test/resources/version.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"release.tag": "STYX.0.0-125"
}