Skip to content

Commit

Permalink
Merge pull request #626 from swisspost/feature/issue625_refactor_forw…
Browse files Browse the repository at this point in the history
…arding_metrics

#625 refactored forwarding metrics to use timer only
  • Loading branch information
mcweba authored Jan 13, 2025
2 parents 17f8333 + 5d95887 commit fa073d8
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@
},
"/playground/cb/1/(.*)": {
"description": "CircuitBreaker Test",
"metricName": "cb_1",
"url": "http://localhost:1234/playground/server/tests/exp/$1"
},
"/playground/cb/2/(.*)": {
"description": "CircuitBreaker Test",
"metricName": "cb_2",
"url": "http://localhost:1234/playground/server/tests/exp/$1"
},
"/playground/cb/3/(.*)": {
"description": "CircuitBreaker Test",
"metricName": "cb_3",
"url": "http://localhost:1234/playground/server/tests/exp/$1"
},
"/playground/cb/4/(.*)": {
"description": "CircuitBreaker Test",
"metricName": "cb_4",
"url": "http://localhost:1234/playground/server/tests/exp/$1"
},
"/playground/": {
Expand Down
8 changes: 1 addition & 7 deletions gateleen-routing/README_routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ Each request header entry is validated in the format `<KEY>: <VALUE>`, so you ar

## Micrometer metrics
The routing feature is monitored with micrometer. The following metrics are available:
* gateleen_forwarded_total
* gateleen_forwarded_seconds
* gateleen_forwarded_seconds_max
* gateleen_forwarded_seconds_count
Expand All @@ -149,18 +148,13 @@ Additional tags are provided to split the forward count into sub counts.
| tag | description |
|------------|-------------------------------------------------------------------------------------------------------------------|
| metricName | The `metricName` property from the corresponding routing rule. With this, you are able to count requests per rule |
| type | Describes where the request was forwarded to. Possible values are `local`, `external` and `null` |
| type | Describes where the request was forwarded to. Possible values are `storage`, `local`, `external` and `null` |
| quantile | Values of `0.75` and `0.95` for percentile durations of requests |


Example metrics:

```
# HELP gateleen_forwarded_total Amount of forwarded requests
# TYPE gateleen_forwarded_total counter
gateleen_forwarded_total{metricName="storage-resources",type="storage",} 67565.0
gateleen_forwarded_total{metricName="infotool_v1_informations",type="external",} 655.0
gateleen_forwarded_total{metricName="infotool-v1",type="storage",} 4320.0
# HELP gateleen_forwarded_seconds_max Durations of forwarded requests
# TYPE gateleen_forwarded_seconds_max gauge
gateleen_forwarded_seconds_max{metricName="storage-resources",type="storage",} 8.5515
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ public abstract class AbstractForwarder implements Handler<RoutingContext> {
protected final MonitoringHandler monitoringHandler;
protected final String metricNameTag;

public static final String FORWARDER_COUNT_METRIC_NAME = "gateleen.forwarded";
public static final String FORWARDER_COUNT_METRIC_DESCRIPTION = "Amount of forwarded requests";
public static final String FORWARDS_METRIC_NAME = "gateleen.forwarded.seconds";
public static final String FORWARDS_METRIC_DESCRIPTION = "Durations of forwarded requests";
public static final String FORWARDER_METRIC_TAG_TYPE = "type";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.swisspush.gateleen.routing;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import io.netty.channel.ConnectTimeoutException;
Expand Down Expand Up @@ -67,7 +66,6 @@ public class Forwarder extends AbstractForwarder {
private static final int STATUS_CODE_2XX = 2;

private static final Logger LOG = LoggerFactory.getLogger(Forwarder.class);
private Counter forwardCounter;
private Timer forwardTimer;
private MeterRegistry meterRegistry;

Expand Down Expand Up @@ -99,11 +97,6 @@ public Forwarder(Vertx vertx, HttpClient client, Rule rule, final ResourceStorag
public void setMeterRegistry(MeterRegistry meterRegistry) {
this.meterRegistry = meterRegistry;
if (meterRegistry != null) {
forwardCounter = Counter.builder(FORWARDER_COUNT_METRIC_NAME)
.description(FORWARDER_COUNT_METRIC_DESCRIPTION)
.tag(FORWARDER_METRIC_TAG_TYPE, getRequestTarget(target))
.tag(FORWARDER_METRIC_TAG_METRICNAME, metricNameTag)
.register(meterRegistry);
forwardTimer = Timer.builder(FORWARDS_METRIC_NAME)
.description(FORWARDS_METRIC_DESCRIPTION)
.publishPercentiles(0.75, 0.95)
Expand Down Expand Up @@ -180,10 +173,6 @@ public void handle(final RoutingContext ctx, final Buffer bodyData, @Nullable fi
}
target = rule.getHost() + ":" + port;

if (forwardCounter != null) {
forwardCounter.increment();
}

if (monitoringHandler != null) {
monitoringHandler.updateRequestsMeter(target, req.uri());
monitoringHandler.updateRequestPerRuleMonitoring(req, rule.getMetricName());
Expand Down Expand Up @@ -305,6 +294,7 @@ public void handle(AsyncResult<HttpClientRequest> event) {

if (event.failed()) {
log.warn("Problem to request {}: {}", targetUri, event.cause());
handleForwardDurationMetrics(finalTimerSample);
final HttpServerResponse response = req.response();
response.setStatusCode(StatusCode.SERVICE_UNAVAILABLE.getStatusCode());
response.setStatusMessage(StatusCode.SERVICE_UNAVAILABLE.getStatusMessage());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.swisspush.gateleen.routing;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import io.vertx.core.MultiMap;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.eventbus.EventBus;
Expand Down Expand Up @@ -29,7 +29,8 @@
public class NullForwarder extends AbstractForwarder {

private EventBus eventBus;
private Counter forwardCounter;
private MeterRegistry meterRegistry;
private Timer forwardTimer;

public NullForwarder(Rule rule, LoggingResourceManager loggingResourceManager, LogAppenderRepository logAppenderRepository, @Nullable MonitoringHandler monitoringHandler, EventBus eventBus) {
super(rule, loggingResourceManager, logAppenderRepository, monitoringHandler);
Expand All @@ -45,9 +46,11 @@ public NullForwarder(Rule rule, LoggingResourceManager loggingResourceManager, L
*/
@Override
public void setMeterRegistry(MeterRegistry meterRegistry) {
this.meterRegistry = meterRegistry;
if(meterRegistry != null) {
forwardCounter = Counter.builder(FORWARDER_COUNT_METRIC_NAME)
.description(FORWARDER_COUNT_METRIC_DESCRIPTION)
forwardTimer = Timer.builder(FORWARDS_METRIC_NAME)
.description(FORWARDS_METRIC_DESCRIPTION)
.publishPercentiles(0.75, 0.95)
.tag(FORWARDER_METRIC_TAG_METRICNAME, metricNameTag)
.tag(FORWARDER_METRIC_TAG_TYPE, "null")
.register(meterRegistry);
Expand All @@ -63,8 +66,10 @@ public void handle(final RoutingContext ctx) {
return;
}

if(forwardCounter != null) {
forwardCounter.increment();
Timer.Sample timerSample = null;
if(meterRegistry != null) {
timerSample = Timer.start(meterRegistry);
timerSample.stop(forwardTimer);
}

if(monitoringHandler != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public class StorageForwarder extends AbstractForwarder {
private CORSHandler corsHandler;
private GateleenExceptionFactory gateleenExceptionFactory;

private Counter forwardCounter;
private Timer forwardTimer;
private MeterRegistry meterRegistry;

Expand Down Expand Up @@ -75,12 +74,6 @@ public StorageForwarder(EventBus eventBus, Rule rule, LoggingResourceManager log
public void setMeterRegistry(MeterRegistry meterRegistry) {
this.meterRegistry = meterRegistry;
if (meterRegistry != null) {
forwardCounter = Counter.builder(FORWARDER_COUNT_METRIC_NAME)
.description(FORWARDER_COUNT_METRIC_DESCRIPTION)
.tag(FORWARDER_METRIC_TAG_METRICNAME, metricNameTag)
.tag(FORWARDER_METRIC_TAG_TYPE, TYPE_STORAGE)
.register(meterRegistry);

forwardTimer = Timer.builder(FORWARDS_METRIC_NAME)
.description(FORWARDS_METRIC_DESCRIPTION)
.publishPercentiles(0.75, 0.95)
Expand All @@ -106,7 +99,6 @@ public void handle(final RoutingContext ctx) {
Timer.Sample timerSample = null;
if(meterRegistry != null) {
timerSample = Timer.start(meterRegistry);
forwardCounter.increment();
}

if (monitoringHandler != null) {
Expand Down
Loading

0 comments on commit fa073d8

Please sign in to comment.