Skip to content

Commit 073595b

Browse files
ahornaceVladimir Kotal
authored andcommitted
Use micrometer and expose metrics in prometheus format
1 parent 61b71b0 commit 073595b

File tree

15 files changed

+77
-133
lines changed

15 files changed

+77
-133
lines changed

opengrok-indexer/pom.xml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,14 @@ Portions Copyright (c) 2020-2020, Lubos Kosco <[email protected]>.
196196
<version>${jackson.version}</version>
197197
</dependency>
198198
<dependency>
199-
<groupId>io.dropwizard.metrics</groupId>
200-
<artifactId>metrics-core</artifactId>
201-
<version>4.1.0</version>
199+
<groupId>io.micrometer</groupId>
200+
<artifactId>micrometer-core</artifactId>
201+
<version>${micrometer.version}</version>
202+
</dependency>
203+
<dependency>
204+
<groupId>io.micrometer</groupId>
205+
<artifactId>micrometer-registry-prometheus</artifactId>
206+
<version>${micrometer.version}</version>
202207
</dependency>
203208
</dependencies>
204209

opengrok-indexer/src/main/java/org/opengrok/indexer/Metrics.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,21 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opengrok.indexer;
2424

25-
import com.codahale.metrics.MetricRegistry;
25+
import io.micrometer.prometheus.PrometheusConfig;
26+
import io.micrometer.prometheus.PrometheusMeterRegistry;
2627

27-
public class Metrics {
28+
public final class Metrics {
2829

29-
private static final MetricRegistry instance = new MetricRegistry();
30+
private static final PrometheusMeterRegistry instance = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
3031

3132
private Metrics() {
3233
}
3334

34-
public static MetricRegistry getInstance() {
35+
public static PrometheusMeterRegistry getInstance() {
3536
return instance;
3637
}
3738

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

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

2020
/*
21-
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2020, Chris Fraire <[email protected]>.
2323
*/
2424
package org.opengrok.indexer.authorization;
@@ -34,8 +34,8 @@
3434
import javax.servlet.http.HttpServletRequest;
3535
import javax.servlet.http.HttpSession;
3636

37-
import com.codahale.metrics.Counter;
38-
import com.codahale.metrics.Timer;
37+
import io.micrometer.core.instrument.Counter;
38+
import io.micrometer.core.instrument.Timer;
3939
import org.opengrok.indexer.Metrics;
4040
import org.opengrok.indexer.configuration.Configuration;
4141
import org.opengrok.indexer.configuration.Group;
@@ -391,7 +391,7 @@ protected void afterReload() {
391391
lock.writeLock().unlock();
392392
}
393393

394-
authStackReloadCounter.inc();
394+
authStackReloadCounter.increment();
395395

396396
// clean the old stack
397397
removeAll(oldStack);
@@ -509,11 +509,11 @@ private boolean checkAll(HttpServletRequest request, String cache, Nameable enti
509509
m = new TreeMap<>();
510510
} else if ((val = m.get(entity.getName())) != null) {
511511
// cache hit
512-
authCacheHits.inc();
512+
authCacheHits.increment();
513513
return val;
514514
}
515515

516-
authCacheMisses.inc();
516+
authCacheMisses.increment();
517517

518518
Duration duration;
519519
boolean overallDecision;
@@ -524,7 +524,7 @@ private boolean checkAll(HttpServletRequest request, String cache, Nameable enti
524524
HttpSession session;
525525
if (((session = request.getSession(false)) != null) && isSessionInvalid(session)) {
526526
session.invalidate();
527-
authSessionsInvalidated.inc();
527+
authSessionsInvalidated.increment();
528528
}
529529
request.getSession().setAttribute(SESSION_VERSION, getPluginVersion());
530530

@@ -536,16 +536,16 @@ private boolean checkAll(HttpServletRequest request, String cache, Nameable enti
536536
lock.readLock().unlock();
537537
}
538538

539-
authTimer.update(duration);
539+
authTimer.record(duration);
540540
if (overallDecision) {
541-
authPositiveTimer.update(duration);
541+
authPositiveTimer.record(duration);
542542
} else {
543-
authNegativeTimer.update(duration);
543+
authNegativeTimer.record(duration);
544544
}
545-
Metrics.getInstance().timer(String.format("authorization_of_%s", entity.getName())).update(duration);
545+
Metrics.getInstance().timer(String.format("authorization_of_%s", entity.getName())).record(duration);
546546
Metrics.getInstance()
547547
.timer(String.format("authorization_%s_of_%s", overallDecision ? "positive" : "negative", entity.getName()))
548-
.update(duration);
548+
.record(duration);
549549

550550
m.put(entity.getName(), overallDecision);
551551
request.setAttribute(cache, m);

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

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

2020
/*
21-
* Copyright (c) 2017, 2019 Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2017, 2020 Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2018, Chris Fraire <[email protected]>.
2323
*/
2424
package org.opengrok.indexer.authorization;
@@ -288,14 +288,14 @@ protected boolean processStack(Nameable entity,
288288
Metrics.getInstance()
289289
.timer(String.format("authorization_in_stack_%s_%s", getName(),
290290
overallDecision ? "positive" : "negative"))
291-
.update(duration);
291+
.record(duration);
292292
Metrics.getInstance()
293293
.timer(String.format("authorization_in_stack_%s_%s_of_%s", getName(),
294294
overallDecision ? "positive" : "negative", entity.getName()))
295-
.update(duration);
295+
.record(duration);
296296
Metrics.getInstance()
297297
.timer(String.format("authorization_in_stack_%s_of_%s", getName(), entity.getName()))
298-
.update(duration);
298+
.record(duration);
299299

300300
return overallDecision;
301301
}

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

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

2020
/*
21-
* Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2006, 2020, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2017-2020, Chris Fraire <[email protected]>.
2323
*/
2424
package org.opengrok.indexer.configuration;
@@ -1537,14 +1537,6 @@ public IncludeFiles getIncludeFiles() {
15371537
return includeFiles;
15381538
}
15391539

1540-
public String getStatisticsFilePath() {
1541-
return syncReadConfiguration(Configuration::getStatisticsFilePath);
1542-
}
1543-
1544-
public void setStatisticsFilePath(String statisticsFilePath) {
1545-
syncWriteConfiguration(statisticsFilePath, Configuration::setStatisticsFilePath);
1546-
}
1547-
15481540
/**
15491541
* Return the authorization framework used in this environment.
15501542
*

opengrok-indexer/src/test/java/org/opengrok/indexer/authorization/AuthorizationFrameworkReloadTest.java

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

2020
/*
21-
* Copyright (c) 2017, 2019 Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2017, 2020 Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opengrok.indexer.authorization;
2424

@@ -146,7 +146,7 @@ public void run() {
146146
}
147147

148148
// Double check that at least one reload() was done.
149-
long reloads = Metrics.getInstance().counter("authorization_stack_reload").getCount();
149+
long reloads = (long) Metrics.getInstance().counter("authorization_stack_reload").count();
150150
assertTrue(reloads > 0);
151151
}
152152

opengrok-web/pom.xml

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ information: Portions Copyright [yyyy] [name of copyright owner]
1818
1919
CDDL HEADER END
2020
21-
Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
21+
Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
2222
Portions Copyright (c) 2018, Chris Fraire <[email protected]>.
2323
2424
-->
@@ -158,16 +158,6 @@ Portions Copyright (c) 2018, Chris Fraire <[email protected]>.
158158
<artifactId>modelmapper</artifactId>
159159
<version>2.3.0</version>
160160
</dependency>
161-
<dependency>
162-
<groupId>io.dropwizard.metrics</groupId>
163-
<artifactId>metrics-jersey2</artifactId>
164-
<version>4.1.0</version>
165-
</dependency>
166-
<dependency>
167-
<groupId>io.dropwizard.metrics</groupId>
168-
<artifactId>metrics-servlets</artifactId>
169-
<version>4.1.0</version>
170-
</dependency>
171161
</dependencies>
172162

173163
<build>

opengrok-web/src/main/java/org/opengrok/web/AuthorizationFilter.java

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

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

2626
import java.io.IOException;
27+
import java.time.Duration;
28+
import java.time.Instant;
2729
import java.util.logging.Level;
2830
import java.util.logging.Logger;
2931
import javax.servlet.Filter;
@@ -35,8 +37,8 @@
3537
import javax.servlet.http.HttpServletRequest;
3638
import javax.servlet.http.HttpServletResponse;
3739

38-
import com.codahale.metrics.Meter;
39-
import com.codahale.metrics.Timer;
40+
import io.micrometer.core.instrument.DistributionSummary;
41+
import io.micrometer.core.instrument.Timer;
4042
import org.opengrok.indexer.Metrics;
4143
import org.opengrok.indexer.configuration.Project;
4244
import org.opengrok.indexer.logger.LoggerFactory;
@@ -48,11 +50,11 @@ public class AuthorizationFilter implements Filter {
4850

4951
private static final Logger LOGGER = LoggerFactory.getLogger(AuthorizationFilter.class);
5052

51-
private final Meter requests = Metrics.getInstance().meter(StatisticsFilter.REQUESTS_METRIC);
53+
private final DistributionSummary requests = Metrics.getInstance().summary(StatisticsFilter.REQUESTS_METRIC);
5254
private final Timer requestsForbidden = Metrics.getInstance().timer("requests_forbidden");
5355

5456
@Override
55-
public void init(FilterConfig fc) throws ServletException {
57+
public void init(FilterConfig fc) {
5658
}
5759

5860
@Override
@@ -75,10 +77,10 @@ public void doFilter(ServletRequest sr, ServletResponse sr1, FilterChain fc) thr
7577

7678
PageConfig config = PageConfig.get(httpReq);
7779

78-
Timer.Context timerCtx = requestsForbidden.time();
79-
try {
80-
Project p = config.getProject();
81-
if (p != null && !config.isAllowed(p)) {
80+
Project p = config.getProject();
81+
if (p != null && !config.isAllowed(p)) {
82+
Instant start = Instant.now();
83+
try {
8284
if (LOGGER.isLoggable(Level.INFO)) {
8385
if (httpReq.getRemoteUser() != null) {
8486
LOGGER.log(Level.INFO, "Access denied for user ''{0}'' for URI: {1}",
@@ -98,7 +100,7 @@ public void doFilter(ServletRequest sr, ServletResponse sr1, FilterChain fc) thr
98100
* In this branch of the if statement the filter processing stopped
99101
* and does not follow to the StatisticsFilter.
100102
*/
101-
requests.mark();
103+
requests.record(1);
102104

103105
if (!config.getEnv().getIncludeFiles().getForbiddenIncludeFileContent(false).isEmpty()) {
104106
sr.getRequestDispatcher("/eforbidden").forward(sr, sr1);
@@ -107,9 +109,9 @@ public void doFilter(ServletRequest sr, ServletResponse sr1, FilterChain fc) thr
107109

108110
httpRes.sendError(HttpServletResponse.SC_FORBIDDEN, "Access forbidden");
109111
return;
112+
} finally {
113+
requestsForbidden.record(Duration.between(start, Instant.now()));
110114
}
111-
} finally {
112-
timerCtx.stop();
113115
}
114116
fc.doFilter(sr, sr1);
115117
}

opengrok-web/src/main/java/org/opengrok/web/HealthCheckServletContextListener.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

opengrok-web/src/main/java/org/opengrok/web/StatisticsFilter.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
* CDDL HEADER END
1818
*/
1919

20-
/*
21-
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
20+
/*
21+
* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opengrok.web;
2424

@@ -33,8 +33,8 @@
3333
import javax.servlet.ServletResponse;
3434
import javax.servlet.http.HttpServletRequest;
3535

36-
import com.codahale.metrics.Meter;
37-
import com.codahale.metrics.Timer;
36+
import io.micrometer.core.instrument.DistributionSummary;
37+
import io.micrometer.core.instrument.Timer;
3838
import org.opengrok.indexer.Metrics;
3939
import org.opengrok.indexer.web.PageConfig;
4040
import org.opengrok.indexer.web.Prefix;
@@ -44,7 +44,7 @@ public class StatisticsFilter implements Filter {
4444

4545
static final String REQUESTS_METRIC = "requests";
4646

47-
private final Meter requests = Metrics.getInstance().meter(REQUESTS_METRIC);
47+
private final DistributionSummary requests = Metrics.getInstance().summary(REQUESTS_METRIC);
4848

4949
private final Timer genericTimer = Metrics.getInstance().timer("*");
5050
private final Timer emptySearch = Metrics.getInstance().timer("empty_search");
@@ -80,26 +80,26 @@ public void doFilter(ServletRequest sr, ServletResponse sr1, FilterChain fc)
8080
* Add the request to the statistics. Be aware of the colliding call in
8181
* {@code AuthorizationFilter#doFilter}.
8282
*/
83-
requests.mark();
84-
genericTimer.update(duration);
83+
requests.record(1);
84+
genericTimer.record(duration);
8585

86-
Metrics.getInstance().timer(category).update(duration);
86+
Metrics.getInstance().timer(category).record(duration);
8787

8888
/* supplementary categories */
8989
if (config.getProject() != null) {
9090
Metrics.getInstance()
9191
.timer("viewing_of_" + config.getProject().getName())
92-
.update(duration);
92+
.record(duration);
9393
}
9494

9595
SearchHelper helper = (SearchHelper) config.getRequestAttribute(SearchHelper.REQUEST_ATTR);
9696
if (helper != null) {
9797
if (helper.hits == null || helper.hits.length == 0) {
9898
// empty search
99-
emptySearch.update(duration);
99+
emptySearch.record(duration);
100100
} else {
101101
// successful search
102-
successfulSearch.update(duration);
102+
successfulSearch.record(duration);
103103
}
104104
}
105105
}

0 commit comments

Comments
 (0)