Skip to content

Commit 61b71b0

Browse files
ahornaceVladimir Kotal
authored andcommitted
Dropwizard metrics support
1 parent 112419e commit 61b71b0

File tree

22 files changed

+288
-1316
lines changed

22 files changed

+288
-1316
lines changed

opengrok-indexer/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@ Portions Copyright (c) 2020-2020, Lubos Kosco <[email protected]>.
195195
<artifactId>jackson-annotations</artifactId>
196196
<version>${jackson.version}</version>
197197
</dependency>
198+
<dependency>
199+
<groupId>io.dropwizard.metrics</groupId>
200+
<artifactId>metrics-core</artifactId>
201+
<version>4.1.0</version>
202+
</dependency>
198203
</dependencies>
199204

200205
<build>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
22+
*/
23+
package org.opengrok.indexer;
24+
25+
import com.codahale.metrics.MetricRegistry;
26+
27+
public class Metrics {
28+
29+
private static final MetricRegistry instance = new MetricRegistry();
30+
31+
private Metrics() {
32+
}
33+
34+
public static MetricRegistry getInstance() {
35+
return instance;
36+
}
37+
38+
}

opengrok-indexer/src/main/java/org/opengrok/indexer/authorization/AuthorizationFramework.java

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
*/
2424
package org.opengrok.indexer.authorization;
2525

26+
import java.time.Duration;
27+
import java.time.Instant;
2628
import java.util.Map;
2729
import java.util.TreeMap;
2830
import java.util.concurrent.locks.ReadWriteLock;
@@ -31,6 +33,10 @@
3133
import java.util.logging.Logger;
3234
import javax.servlet.http.HttpServletRequest;
3335
import javax.servlet.http.HttpSession;
36+
37+
import com.codahale.metrics.Counter;
38+
import com.codahale.metrics.Timer;
39+
import org.opengrok.indexer.Metrics;
3440
import org.opengrok.indexer.configuration.Configuration;
3541
import org.opengrok.indexer.configuration.Group;
3642
import org.opengrok.indexer.configuration.Nameable;
@@ -39,7 +45,6 @@
3945
import org.opengrok.indexer.framework.PluginFramework;
4046
import org.opengrok.indexer.logger.LoggerFactory;
4147
import org.opengrok.indexer.web.Laundromat;
42-
import org.opengrok.indexer.web.Statistics;
4348

4449
/**
4550
* Placeholder for performing authorization checks.
@@ -50,6 +55,15 @@ public final class AuthorizationFramework extends PluginFramework<IAuthorization
5055

5156
private static final Logger LOGGER = LoggerFactory.getLogger(AuthorizationFramework.class);
5257

58+
private final Counter authStackReloadCounter = Metrics.getInstance().counter("authorization_stack_reload");
59+
private final Counter authCacheHits = Metrics.getInstance().counter("authorization_cache_hits");
60+
private final Counter authCacheMisses = Metrics.getInstance().counter("authorization_cache_misses");
61+
private final Counter authSessionsInvalidated = Metrics.getInstance().counter("authorization_sessions_invalidated");
62+
63+
private final Timer authTimer = Metrics.getInstance().timer("authorization");
64+
private final Timer authPositiveTimer = Metrics.getInstance().timer("authorization_positive");
65+
private final Timer authNegativeTimer = Metrics.getInstance().timer("authorization_negative");
66+
5367
/**
5468
* Stack of available plugins/stacks in the order of the execution.
5569
*/
@@ -377,12 +391,10 @@ protected void afterReload() {
377391
lock.writeLock().unlock();
378392
}
379393

380-
Statistics stats = RuntimeEnvironment.getInstance().getStatistics();
381-
stats.addRequest("authorization_stack_reload");
394+
authStackReloadCounter.inc();
382395

383396
// clean the old stack
384397
removeAll(oldStack);
385-
oldStack = null;
386398
loadingStack = null;
387399
}
388400

@@ -490,55 +502,50 @@ private boolean checkAll(HttpServletRequest request, String cache, Nameable enti
490502
return false;
491503
}
492504

493-
Statistics stats = RuntimeEnvironment.getInstance().getStatistics();
494-
495505
Boolean val;
496506
Map<String, Boolean> m = (Map<String, Boolean>) request.getAttribute(cache);
497507

498508
if (m == null) {
499509
m = new TreeMap<>();
500510
} else if ((val = m.get(entity.getName())) != null) {
501511
// cache hit
502-
stats.addRequest("authorization_cache_hits");
512+
authCacheHits.inc();
503513
return val;
504514
}
505515

506-
stats.addRequest("authorization_cache_misses");
516+
authCacheMisses.inc();
507517

508-
long time = 0;
509-
boolean overallDecision = false;
518+
Duration duration;
519+
boolean overallDecision;
510520

511521
lock.readLock().lock();
512522
try {
513523
// Make sure there is a HTTP session that corresponds to current plugin version.
514524
HttpSession session;
515525
if (((session = request.getSession(false)) != null) && isSessionInvalid(session)) {
516526
session.invalidate();
517-
stats.addRequest("authorization_sessions_invalidated");
527+
authSessionsInvalidated.inc();
518528
}
519529
request.getSession().setAttribute(SESSION_VERSION, getPluginVersion());
520530

521-
time = System.currentTimeMillis();
522-
531+
Instant start = Instant.now();
523532
overallDecision = performCheck(entity, pluginPredicate, skippingPredicate);
533+
Instant end = Instant.now();
534+
duration = Duration.between(start, end);
524535
} finally {
525536
lock.readLock().unlock();
526537
}
527538

528-
if (time > 0) {
529-
time = System.currentTimeMillis() - time;
530-
531-
stats.addRequestTime("authorization", time);
532-
stats.addRequestTime(
533-
String.format("authorization_%s", overallDecision ? "positive" : "negative"),
534-
time);
535-
stats.addRequestTime(
536-
String.format("authorization_%s_of_%s", overallDecision ? "positive" : "negative", entity.getName()),
537-
time);
538-
stats.addRequestTime(
539-
String.format("authorization_of_%s", entity.getName()),
540-
time);
539+
authTimer.update(duration);
540+
if (overallDecision) {
541+
authPositiveTimer.update(duration);
542+
} else {
543+
authNegativeTimer.update(duration);
541544
}
545+
Metrics.getInstance().timer(String.format("authorization_of_%s", entity.getName())).update(duration);
546+
Metrics.getInstance()
547+
.timer(String.format("authorization_%s_of_%s", overallDecision ? "positive" : "negative", entity.getName()))
548+
.update(duration);
542549

543550
m.put(entity.getName(), overallDecision);
544551
request.setAttribute(cache, m);

opengrok-indexer/src/main/java/org/opengrok/indexer/authorization/AuthorizationStack.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,24 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2017, 2019 Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2018, Chris Fraire <[email protected]>.
2323
*/
2424
package org.opengrok.indexer.authorization;
2525

26+
import java.time.Duration;
27+
import java.time.Instant;
2628
import java.util.ArrayList;
2729
import java.util.List;
2830
import java.util.Locale;
2931
import java.util.Map;
3032
import java.util.TreeMap;
3133
import java.util.logging.Level;
3234
import java.util.logging.Logger;
35+
36+
import org.opengrok.indexer.Metrics;
3337
import org.opengrok.indexer.configuration.Nameable;
34-
import org.opengrok.indexer.configuration.RuntimeEnvironment;
3538
import org.opengrok.indexer.logger.LoggerFactory;
36-
import org.opengrok.indexer.web.Statistics;
3739

3840
/**
3941
* Subclass of {@link AuthorizationEntity}. It implements the methods to
@@ -222,8 +224,7 @@ protected boolean processStack(Nameable entity,
222224
PluginDecisionPredicate pluginPredicate,
223225
PluginSkippingPredicate skippingPredicate) {
224226

225-
Statistics stats = RuntimeEnvironment.getInstance().getStatistics();
226-
long time = System.currentTimeMillis();
227+
Instant start = Instant.now();
227228

228229
boolean overallDecision = true;
229230
for (AuthorizationEntity authEntity : getStack()) {
@@ -282,17 +283,19 @@ protected boolean processStack(Nameable entity,
282283
}
283284
}
284285
}
285-
time = System.currentTimeMillis() - time;
286-
287-
stats.addRequestTime(
288-
String.format("authorization_in_stack_%s_%s", getName(), overallDecision ? "positive" : "negative"),
289-
time);
290-
stats.addRequestTime(
291-
String.format("authorization_in_stack_%s_%s_of_%s", getName(), overallDecision ? "positive" : "negative", entity.getName()),
292-
time);
293-
stats.addRequestTime(
294-
String.format("authorization_in_stack_%s_of_%s", getName(), entity.getName()),
295-
time);
286+
Duration duration = Duration.between(start, Instant.now());
287+
288+
Metrics.getInstance()
289+
.timer(String.format("authorization_in_stack_%s_%s", getName(),
290+
overallDecision ? "positive" : "negative"))
291+
.update(duration);
292+
Metrics.getInstance()
293+
.timer(String.format("authorization_in_stack_%s_%s_of_%s", getName(),
294+
overallDecision ? "positive" : "negative", entity.getName()))
295+
.update(duration);
296+
Metrics.getInstance()
297+
.timer(String.format("authorization_in_stack_%s_of_%s", getName(), entity.getName()))
298+
.update(duration);
296299

297300
return overallDecision;
298301
}

opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/Configuration.java

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2007, 2020, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2017-2020, Chris Fraire <[email protected]>.
2323
* Portions Copyright (c) 2020, Aleksandr Kirillov <[email protected]>.
2424
*/
@@ -76,7 +76,6 @@ public final class Configuration {
7676

7777
private static final Logger LOGGER = LoggerFactory.getLogger(Configuration.class);
7878
public static final String PLUGIN_DIRECTORY_DEFAULT = "plugins";
79-
public static final String STATISTICS_FILE_DEFAULT = "statistics.json";
8079

8180
/**
8281
* A check if a pattern contains at least one pair of parentheses meaning
@@ -217,7 +216,6 @@ public final class Configuration {
217216
private boolean scopesEnabled;
218217
private boolean projectsEnabled;
219218
private boolean foldingEnabled;
220-
private String statisticsFilePath;
221219
/*
222220
* Set to false if we want to disable fetching history of individual files
223221
* (by running appropriate SCM command) when the history is not found
@@ -429,14 +427,6 @@ public void setCtagsTimeout(long timeout) throws IllegalArgumentException {
429427
this.ctagsTimeout = timeout;
430428
}
431429

432-
public String getStatisticsFilePath() {
433-
return statisticsFilePath;
434-
}
435-
436-
public void setStatisticsFilePath(String statisticsFilePath) {
437-
this.statisticsFilePath = statisticsFilePath;
438-
}
439-
440430
public boolean isLastEditedDisplayMode() {
441431
return lastEditedDisplayMode;
442432
}
@@ -524,7 +514,6 @@ public Configuration() {
524514
setScanningDepth(defaultScanningDepth); // default depth of scanning for repositories
525515
setScopesEnabled(true);
526516
setSourceRoot(null);
527-
setStatisticsFilePath(null);
528517
//setTabSize(4);
529518
setTagsEnabled(false);
530519
//setUserPage("http://www.myserver.org/viewProfile.jspa?username=");
@@ -848,21 +837,16 @@ public String getDataRoot() {
848837
/**
849838
* Sets data root.
850839
*
851-
* This method also sets the pluginDirectory if it is not already set and
852-
* also this method sets the statisticsFilePath if it is not already set
840+
* This method also sets the pluginDirectory if it is not already set.
853841
*
854842
* @see #setPluginDirectory(java.lang.String)
855-
* @see #setStatisticsFilePath(java.lang.String)
856843
*
857844
* @param dataRoot data root path
858845
*/
859846
public void setDataRoot(String dataRoot) {
860847
if (dataRoot != null && getPluginDirectory() == null) {
861848
setPluginDirectory(dataRoot + "/../" + PLUGIN_DIRECTORY_DEFAULT);
862849
}
863-
if (dataRoot != null && getStatisticsFilePath() == null) {
864-
setStatisticsFilePath(dataRoot + "/" + STATISTICS_FILE_DEFAULT);
865-
}
866850
this.dataRoot = dataRoot;
867851
}
868852

opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/RuntimeEnvironment.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@
7272
import org.opengrok.indexer.util.PathUtils;
7373
import org.opengrok.indexer.util.ResourceLock;
7474
import org.opengrok.indexer.web.Prefix;
75-
import org.opengrok.indexer.web.Statistics;
7675
import org.opengrok.indexer.web.Util;
7776
import org.opengrok.indexer.web.messages.Message;
7877
import org.opengrok.indexer.web.messages.MessagesContainer;
@@ -99,7 +98,6 @@ public final class RuntimeEnvironment {
9998
private final Map<String, SearcherManager> searcherManagerMap = new ConcurrentHashMap<>();
10099

101100
private String configURI;
102-
private Statistics statistics = new Statistics();
103101
IncludeFiles includeFiles = new IncludeFiles();
104102
private final MessagesContainer messagesContainer = new MessagesContainer();
105103

@@ -251,14 +249,6 @@ public long getCtagsTimeout() {
251249
public void setCtagsTimeout(long ctagsTimeout) {
252250
syncWriteConfiguration(ctagsTimeout, Configuration::setCtagsTimeout);
253251
}
254-
255-
public Statistics getStatistics() {
256-
return statistics;
257-
}
258-
259-
public void setStatistics(Statistics statistics) {
260-
this.statistics = statistics;
261-
}
262252

263253
public void setLastEditedDisplayMode(boolean lastEditedDisplayMode) {
264254
syncWriteConfiguration(lastEditedDisplayMode, Configuration::setLastEditedDisplayMode);

0 commit comments

Comments
 (0)