Skip to content

Commit

Permalink
Endpoint times are configurable through Yaml file
Browse files Browse the repository at this point in the history
  • Loading branch information
fhanik committed Sep 25, 2017
1 parent 12eec94 commit 3993c35
Show file tree
Hide file tree
Showing 13 changed files with 593 additions and 101 deletions.
Expand Up @@ -35,7 +35,6 @@
public class MetricsQueue { public class MetricsQueue {


public static final int MAX_ENTRIES = 5; public static final int MAX_ENTRIES = 5;
public static final int MAX_TIME = 3000;


private ConcurrentLinkedDeque<RequestMetric> queue; private ConcurrentLinkedDeque<RequestMetric> queue;
private Map<StatusCodeGroup, RequestMetricSummary> statistics; private Map<StatusCodeGroup, RequestMetricSummary> statistics;
Expand All @@ -53,21 +52,23 @@ public MetricsQueue(@JsonProperty("lastRequests") ConcurrentLinkedDeque<RequestM


public boolean offer(RequestMetric metric) { public boolean offer(RequestMetric metric) {
queue.offer(metric); queue.offer(metric);
//remove eariest entries //remove earliest entries
while (queue.size() > MAX_ENTRIES) { while (queue.size() > MAX_ENTRIES) {
queue.removeLast(); queue.removeFirst();
} }


StatusCodeGroup statusCode = StatusCodeGroup.valueOf(metric.getStatusCode()); StatusCodeGroup statusCode = StatusCodeGroup.valueOf(metric.getStatusCode());
if (!statistics.containsKey(statusCode)) { if (!statistics.containsKey(statusCode)) {
statistics.putIfAbsent(statusCode, new RequestMetricSummary()); statistics.putIfAbsent(statusCode, new RequestMetricSummary());
} }
RequestMetricSummary totals = statistics.get(statusCode); RequestMetricSummary totals = statistics.get(statusCode);
totals.add(metric.getRequestCompleteTime()- metric.getRequestStartTime(), long time = metric.getRequestCompleteTime() - metric.getRequestStartTime();
totals.add(time,
time < metric.getUriGroup().getLimit(),
metric.getNrOfDatabaseQueries(), metric.getNrOfDatabaseQueries(),
metric.getDatabaseQueryTime(), metric.getDatabaseQueryTime(),
metric.getQueries().stream().filter(q -> !q.isIntolerable()).count(), metric.getQueries().stream().filter(q -> q.isIntolerable()).count(),
metric.getQueries().stream().filter(q -> !q.isIntolerable()).mapToLong(q -> q.getRequestCompleteTime()-q.getRequestStartTime()).sum() metric.getQueries().stream().filter(q -> q.isIntolerable()).mapToLong(q -> q.getRequestCompleteTime()-q.getRequestStartTime()).sum()
); );
return true; return true;
} }
Expand Down
Expand Up @@ -24,15 +24,17 @@
@JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnoreProperties(ignoreUnknown = true)
public class RequestMetric { public class RequestMetric {
private String uri; private String uri;
private UrlGroup uriGroup;
private int statusCode; private int statusCode;
private long requestStartTime; private long requestStartTime;
private long requestCompleteTime; private long requestCompleteTime;
private List<QueryMetric> queries = new LinkedList<>(); private List<QueryMetric> queries = new LinkedList<>();


public static RequestMetric start(String uri, long start) { public static RequestMetric start(String uri, UrlGroup group, long start) {
RequestMetric metric = new RequestMetric(); RequestMetric metric = new RequestMetric();
metric.requestStartTime = start; metric.requestStartTime = start;
metric.uri = uri; metric.uri = uri;
metric.uriGroup = group;
return metric; return metric;
} }


Expand Down Expand Up @@ -73,4 +75,8 @@ public long getNrOfDatabaseQueries() {
public long getDatabaseQueryTime() { public long getDatabaseQueryTime() {
return queries.stream().mapToLong(q -> q.getRequestCompleteTime() - q.getRequestStartTime()).sum(); return queries.stream().mapToLong(q -> q.getRequestCompleteTime() - q.getRequestStartTime()).sum();
} }

public UrlGroup getUriGroup() {
return uriGroup;
}
} }
Expand Up @@ -53,10 +53,10 @@ public RequestMetricSummary(@JsonProperty("count") long count,
this.averageDatabaseIntolerableQueryTime = averageDatabaseIntolerableQueryTime; this.averageDatabaseIntolerableQueryTime = averageDatabaseIntolerableQueryTime;
} }


public synchronized void add(long time, long dbQueries, long dbTime, long failedDbQueries, long failedDbQueryTime) { public synchronized void add(long time, boolean tolerable, long dbQueries, long dbTime, long failedDbQueries, long failedDbQueryTime) {
averageTime = addToAverage(count, averageTime, 1, time); averageTime = addToAverage(count, averageTime, 1, time);
count++; count++;
if (time > MetricsQueue.MAX_TIME) { if (!tolerable) {
averageIntolerableTime = addToAverage(intolerableCount, averageIntolerableTime, 1, time); averageIntolerableTime = addToAverage(intolerableCount, averageIntolerableTime, 1, time);
++intolerableCount; ++intolerableCount;
} }
Expand Down
@@ -0,0 +1,84 @@
/*
* ****************************************************************************
* Cloud Foundry
* Copyright (c) [2009-2017] Pivotal Software, Inc. All Rights Reserved.
*
* This product is licensed to you under the Apache License, Version 2.0 (the "License").
* You may not use this product except in compliance with the License.
*
* This product includes a number of subcomponents with
* separate copyright notices and license terms. Your use of these
* subcomponents is subject to the terms and conditions of the
* subcomponent's license, as noted in the LICENSE file.
* ****************************************************************************
*/

package org.cloudfoundry.identity.uaa.metrics;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import java.util.HashMap;
import java.util.Map;

@JsonIgnoreProperties(ignoreUnknown = true)
public class UrlGroup {
private String pattern;
private String group;
private long limit;
private String category;

public String getPattern() {
return pattern;
}

public UrlGroup setPattern(String pattern) {
this.pattern = pattern;
return this;
}

public String getGroup() {
return group;
}

public UrlGroup setGroup(String group) {
this.group = group;
return this;
}

public long getLimit() {
return limit;
}

public UrlGroup setLimit(long limit) {
this.limit = limit;
return this;
}

public String getCategory() {
return category;
}

public UrlGroup setCategory(String category) {
this.category = category;
return this;
}

@JsonIgnore
public Map<String,Object> getMap() {
HashMap<String, Object> map = new HashMap<>();
map.put("pattern", getPattern());
map.put("group", getGroup());
map.put("limit", getLimit());
map.put("category", getCategory());
return map;
}

public static UrlGroup from(Map<String,Object> map) {
return new UrlGroup()
.setPattern((String) map.get("pattern"))
.setGroup((String) map.get("group"))
.setCategory((String) map.get("category"))
.setLimit(((Number) map.get("limit")).longValue());
}
}

0 comments on commit 3993c35

Please sign in to comment.