Skip to content

Commit

Permalink
Add a mixin for the MetricsRegistry serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Macaulay committed Sep 4, 2018
1 parent d3430d7 commit 031086c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.codahale.metrics.MetricFilter;
import com.codahale.metrics.MetricRegistryListener;
import com.codahale.metrics.Timer;
import com.fasterxml.jackson.annotation.JsonValue;
import com.hotels.styx.api.MetricRegistry;
import com.hotels.styx.api.metrics.ScopedMetricRegistry;

Expand Down Expand Up @@ -55,7 +54,6 @@ public CodaHaleMetricRegistry() {
this(new com.codahale.metrics.MetricRegistry());
}

@JsonValue
public com.codahale.metrics.MetricRegistry getMetricRegistry() {
return metricRegistry;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@
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 @@ -53,7 +56,8 @@ 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 MetricRegistry metricRegistry;

Expand All @@ -64,11 +68,17 @@ public class MetricsHandler extends JsonHandler<MetricRegistry> {
* @param cacheExpiration duration for which generated page content should be cached
*/
public MetricsHandler(MetricRegistry metricRegistry, Optional<Duration> cacheExpiration) {
super(requireNonNull(metricRegistry), cacheExpiration, new MetricsModule(SECONDS, MILLISECONDS, DO_NOT_SHOW_SAMPLES));
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 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();

}

0 comments on commit 031086c

Please sign in to comment.