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

Adapt Version metric to a more flexible pattern #355

Merged
merged 3 commits into from
Dec 20, 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
27 changes: 27 additions & 0 deletions components/proxy/src/main/java/com/hotels/styx/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,21 @@
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

/**
* Version of the current styx server.
*/
public class Version {
private static final Logger LOG = getLogger(Version.class);
private static final Pattern VERSION_FORMAT = Pattern.compile("(\\d+)\\.(\\d+)[-.](\\d+).*");


private final String releaseTag;

Expand Down Expand Up @@ -74,7 +80,28 @@ public String releaseVersion() {
return releaseTag.substring(firstDot + 1);
}

public Optional<Integer> buildNumber() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this used anywhere beyond tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

umm the changes to CoreMetrics were uncommited... it should be fixed now.

String releaseVersion = releaseVersion();
Optional<String> buildNumberAsString = extractFinalInt(releaseVersion);
return buildNumberAsString.flatMap(this::parseInteger);
}

private Optional<String> extractFinalInt(String versionString) {
Matcher m = VERSION_FORMAT.matcher(versionString);
if (!m.matches()) {
return Optional.empty();
} else {
return Optional.of(m.group(3));
}
}

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 @@ -71,7 +71,7 @@ class ProxyConnectorFactory implements ServerConnectorFactory {
MetricRegistry metrics,
HttpErrorStatusListener errorStatusListener,
String unwiseCharacters,
ResponseEnhancer responseEnhancer,
ResponseEnhancer responseEnhancer,
boolean requestTracking) {
this.serverConfig = requireNonNull(serverConfig);
this.metrics = requireNonNull(metrics);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

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 @@ -46,13 +47,23 @@ private CoreMetrics() {
}

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

private static void registerVersionMetric(MetricRegistry metricRegistry, String versionNumber) {
Gauge<String> versionGauge = () -> versionNumber;
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;

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

import org.testng.annotations.Test;

import java.util.Optional;

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 @@ -30,11 +31,12 @@ public class VersionTest {
public void readBuildInfoFromTheSpecifiedPath() {
Version version = readVersionFrom("/version.json");
assertThat(version.releaseTag(), containsString("STYX"));
assertThat(version.releaseVersion(), is("0.0-125"));
assertThat(version.buildNumber(), is(Optional.of(125)));
}

@Test
public void willCreateAnEmptyBuildInfoIfFailsToRead() {
assertThat(readVersionFrom("version.txt"), is(newVersion()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void registersVersionMetric() {

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

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

@Test
Expand Down