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

Injectable metricsRegistry #263

Merged
merged 6 commits into from
Sep 6, 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
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,12 @@ public interface MetricRegistry {
*/
SortedMap<String, Timer> getTimers(MetricFilter filter);


/**
* A map of metric names to metrics.
*
* @return the metrics
*/
SortedMap<String, Metric> getMetrics();

}
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,9 @@ public SortedMap<String, Timer> getTimers() {
public SortedMap<String, Timer> getTimers(MetricFilter filter) {
return this.parent.getTimers(filter);
}

@Override
public SortedMap<String, Metric> getMetrics() {
return this.parent.getMetrics();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Map;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;

/**
* A {@link MetricRegistry} that acts as an adapter for Codahale's {@link com.codahale.metrics.MetricRegistry}.
Expand Down Expand Up @@ -227,4 +228,9 @@ public long getCount() {
return (long) getSnapshot().size();
}
}

@Override
public SortedMap<String, Metric> getMetrics() {
return new TreeMap<>(metricRegistry.getMetrics());
}
}
11 changes: 6 additions & 5 deletions components/proxy/src/main/java/com/hotels/styx/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
package com.hotels.styx;

import com.google.common.eventbus.EventBus;
import com.hotels.styx.api.metrics.codahale.CodaHaleMetricRegistry;
import com.hotels.styx.configstore.ConfigStore;
import com.hotels.styx.api.MetricRegistry;
import com.hotels.styx.proxy.HttpErrorStatusCauseLogger;
import com.hotels.styx.proxy.HttpErrorStatusMetrics;
import com.hotels.styx.api.metrics.codahale.CodaHaleMetricRegistry;
import com.hotels.styx.configstore.ConfigStore;
import com.hotels.styx.server.HttpErrorStatusListener;
import com.hotels.styx.server.ServerEnvironment;

Expand Down Expand Up @@ -76,7 +77,7 @@ public AggregatedConfiguration configuration() {
}

@Override
public CodaHaleMetricRegistry metricRegistry() {
public MetricRegistry metricRegistry() {
return serverEnvironment.metricRegistry();
}

Expand All @@ -94,7 +95,7 @@ public ServerEnvironment serverEnvironment() {
*/
public static class Builder {
private AggregatedConfiguration aggregatedConfiguration;
private CodaHaleMetricRegistry metricRegistry;
private MetricRegistry metricRegistry;
private Version version;
private EventBus eventBus;

Expand All @@ -114,7 +115,7 @@ public Builder configuration(StyxConfig configuration) {
return this;
}

public Builder metricsRegistry(CodaHaleMetricRegistry metricRegistry) {
public Builder metricsRegistry(MetricRegistry metricRegistry) {
this.metricRegistry = metricRegistry;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,10 @@ public SortedMap<String, Timer> getTimers() {
public SortedMap<String, Timer> getTimers(MetricFilter filter) {
return null;
}

@Override
public SortedMap<String, Metric> getMetrics() {
return filterKeys(original.getMetrics(), STARTS_WITH_JVM);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@
package com.hotels.styx.admin.handlers;

import com.codahale.metrics.Metric;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.json.MetricsModule;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.hotels.styx.api.FullHttpResponse;
import com.hotels.styx.api.HttpInterceptor;
import com.hotels.styx.api.HttpRequest;
import com.hotels.styx.api.HttpResponse;
import com.hotels.styx.api.MetricRegistry;
import com.hotels.styx.api.StyxObservable;
import com.hotels.styx.api.metrics.codahale.CodaHaleMetricRegistry;
import com.hotels.styx.infrastructure.configuration.json.mixins.CodaHaleMetricRegistryMixin;

import java.time.Duration;
import java.util.Map;
Expand Down Expand Up @@ -54,22 +56,31 @@ public class MetricsHandler extends JsonHandler<MetricRegistry> {
private static final String PRETTY_PRINT_PARAM = "pretty";

private final ObjectMapper metricSerialiser = new ObjectMapper()
.registerModule(new MetricsModule(SECONDS, MILLISECONDS, DO_NOT_SHOW_SAMPLES));
.registerModule(new MetricsModule(SECONDS, MILLISECONDS, DO_NOT_SHOW_SAMPLES))
.addMixIn(CodaHaleMetricRegistry.class, CodaHaleMetricRegistryMixin.class);

private final CodaHaleMetricRegistry metricRegistry;
private final MetricRegistry metricRegistry;

/**
* Constructs a new handler.
*
* @param metricRegistry metrics registry
* @param cacheExpiration duration for which generated page content should be cached
*/
public MetricsHandler(CodaHaleMetricRegistry metricRegistry, Optional<Duration> cacheExpiration) {
super(requireNonNull(metricRegistry.getMetricRegistry()), cacheExpiration, new MetricsModule(SECONDS, MILLISECONDS, DO_NOT_SHOW_SAMPLES));
public MetricsHandler(MetricRegistry metricRegistry, Optional<Duration> cacheExpiration) {
super(requireNonNull(metricRegistry), cacheExpiration,
new MetricsModule(SECONDS, MILLISECONDS, DO_NOT_SHOW_SAMPLES),
new FullMetricsModule());

this.metricRegistry = metricRegistry;
}

private static class FullMetricsModule extends SimpleModule {
FullMetricsModule() {
setMixInAnnotation(CodaHaleMetricRegistry.class, CodaHaleMetricRegistryMixin.class);
}
}

@Override
public StyxObservable<HttpResponse> handle(HttpRequest request, HttpInterceptor.Context context) {
MetricRequest metricRequest = new MetricRequest(request);
Expand All @@ -80,7 +91,7 @@ public StyxObservable<HttpResponse> handle(HttpRequest request, HttpInterceptor.
}

private FullHttpResponse.Builder restrictedMetricsResponse(MetricRequest request) {
Map<String, Metric> fullMetrics = metricRegistry.getMetricRegistry().getMetrics();
Map<String, Metric> fullMetrics = metricRegistry.getMetrics();

Map<String, Metric> restricted = filter(fullMetrics, (name, metric) -> request.matchesRoot(name));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright (C) 2013-2018 Expedia Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.hotels.styx.infrastructure.configuration.json.mixins;

import com.fasterxml.jackson.annotation.JsonValue;

/**
* Jackson annotations for {@link com.hotels.styx.api.metrics.codahale.CodaHaleMetricRegistry}.
*/
public abstract class CodaHaleMetricRegistryMixin {

@JsonValue
public abstract com.codahale.metrics.MetricRegistry getMetricRegistry();

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.hotels.styx.Environment;
import com.hotels.styx.StyxConfig;
import com.hotels.styx.Version;
import com.hotels.styx.api.MetricRegistry;
import com.hotels.styx.api.configuration.Configuration;
import com.hotels.styx.api.extension.service.spi.StyxService;
import com.hotels.styx.api.metrics.codahale.CodaHaleMetricRegistry;
Expand Down Expand Up @@ -50,7 +51,7 @@ public class StyxServerComponents {
private StyxServerComponents(Builder builder) {
StyxConfig styxConfig = requireNonNull(builder.styxConfig);

this.environment = newEnvironment(styxConfig);
this.environment = newEnvironment(styxConfig, builder.metricRegistry);
builder.loggingSetUp.setUp(environment);

this.plugins = builder.pluginsLoader.load(environment);
Expand All @@ -73,10 +74,10 @@ public List<NamedPlugin> plugins() {
return plugins;
}

private static Environment newEnvironment(StyxConfig styxConfig) {
private static Environment newEnvironment(StyxConfig styxConfig, MetricRegistry metricRegistry) {
return new Environment.Builder()
.configuration(styxConfig)
.metricsRegistry(new CodaHaleMetricRegistry())
.metricsRegistry(metricRegistry)
.buildInfo(readBuildInfo())
.eventBus(new AsyncEventBus("styx", newSingleThreadExecutor()))
.build();
Expand Down Expand Up @@ -109,6 +110,7 @@ public static final class Builder {
private LoggingSetUp loggingSetUp = DO_NOT_MODIFY;
private PluginsLoader pluginsLoader = PLUGINS_FROM_CONFIG;
private ServicesLoader servicesLoader = SERVICES_FROM_CONFIG;
private MetricRegistry metricRegistry = new CodaHaleMetricRegistry();

private final Map<String, StyxService> additionalServices = new HashMap<>();

Expand All @@ -117,6 +119,11 @@ public Builder styxConfig(StyxConfig styxConfig) {
return this;
}

public Builder metricsRegistry(MetricRegistry metricRegistry) {
this.metricRegistry = requireNonNull(metricRegistry);
return this;
}

public Builder configuration(Configuration configuration) {
return styxConfig(new StyxConfig(configuration));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,20 @@
package com.hotels.styx.server;

import com.hotels.styx.api.metrics.codahale.CodaHaleMetricRegistry;
import com.hotels.styx.api.MetricRegistry;

public final class ServerEnvironment {
private final CodaHaleMetricRegistry metricRegistry;
private final MetricRegistry metricRegistry;

public ServerEnvironment() {
this(new CodaHaleMetricRegistry());
}

public ServerEnvironment(CodaHaleMetricRegistry metricRegistry) {
public ServerEnvironment(MetricRegistry metricRegistry) {
this.metricRegistry = metricRegistry;
}

public CodaHaleMetricRegistry metricRegistry() {
public MetricRegistry metricRegistry() {
return metricRegistry;
}
}