• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

oracle / opengrok / #3663

26 Oct 2023 04:47PM UTC coverage: 66.029% (-9.1%) from 75.156%
#3663

push

web-flow
opengrok web project code smell fixes (#4457)

---------

Signed-off-by: Gino Augustine <ginoaugustine@gmail.com>

19 of 19 new or added lines in 4 files covered. (100.0%)

38701 of 58612 relevant lines covered (66.03%)

0.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/opengrok-web/src/main/java/org/opengrok/web/StatisticsFilter.java
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) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
22
 */
23
package org.opengrok.web;
24

25
import java.io.IOException;
26
import java.time.Duration;
27
import java.time.Instant;
28

29
import io.micrometer.core.instrument.DistributionSummary;
30
import io.micrometer.core.instrument.MeterRegistry;
31
import io.micrometer.core.instrument.Timer;
32
import jakarta.servlet.Filter;
33
import jakarta.servlet.FilterChain;
34
import jakarta.servlet.FilterConfig;
35
import jakarta.servlet.ServletException;
36
import jakarta.servlet.ServletRequest;
37
import jakarta.servlet.ServletResponse;
38
import jakarta.servlet.http.HttpServletRequest;
39
import jakarta.servlet.http.HttpServletResponse;
40
import org.jetbrains.annotations.NotNull;
41
import org.opengrok.indexer.Metrics;
42
import org.opengrok.indexer.web.QueryParameters;
43
import org.opengrok.indexer.web.SearchHelper;
44

45
public class StatisticsFilter implements Filter {
×
46

47
    static final String REQUESTS_METRIC = "requests";
48
    private static final String CATEGORY_TAG = "category";
49

50
    private final DistributionSummary requests = Metrics.getPrometheusRegistry().summary(REQUESTS_METRIC);
×
51

52
    @Override
53
    public void init(FilterConfig fc) throws ServletException {
54
        //No init config Operation
55
    }
×
56

57
    @Override
58
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain fc)
59
            throws IOException, ServletException {
60

61
        requests.record(1);
×
62

63
        HttpServletRequest httpReq = (HttpServletRequest) servletRequest;
×
64

65
        Instant start = Instant.now();
×
66

67
        PageConfig config = PageConfig.get(httpReq);
×
68

69
        fc.doFilter(servletRequest, servletResponse);
×
70

71
        measure((HttpServletResponse) servletResponse, httpReq, Duration.between(start, Instant.now()), config);
×
72
    }
×
73

74
    private void measure(HttpServletResponse httpResponse, HttpServletRequest httpReq,
75
                         Duration duration, PageConfig config) {
76
        String category;
77
        category = getCategory(httpReq, config);
×
78

79
        Timer categoryTimer = Timer.builder("requests.latency").
×
80
                tags(CATEGORY_TAG, category, "code", String.valueOf(httpResponse.getStatus())).
×
81
                register(Metrics.getPrometheusRegistry());
×
82
        categoryTimer.record(duration);
×
83

84
        SearchHelper helper = (SearchHelper) config.getRequestAttribute(SearchHelper.REQUEST_ATTR);
×
85
        MeterRegistry registry = Metrics.getRegistry();
×
86
        if (helper != null && registry != null) {
×
87
            if (helper.getHits() == null || helper.getHits().length == 0) {
×
88
                Timer.builder("search.latency").
×
89
                        tags(CATEGORY_TAG, "ui", "outcome", "empty").
×
90
                        register(registry).
×
91
                        record(duration);
×
92
            } else {
93
                Timer.builder("search.latency").
×
94
                        tags(CATEGORY_TAG, "ui", "outcome", "success").
×
95
                        register(registry).
×
96
                        record(duration);
×
97
            }
98
        }
99
    }
×
100

101
    @NotNull
102
    private String getCategory(HttpServletRequest httpReq, PageConfig config) {
103
        String category;
104
        if (isRoot(httpReq)) {
×
105
            category = "root";
×
106
        } else {
107
            String prefix = config.getPrefix().toString();
×
108
            if (prefix.isEmpty()) {
×
109
                category = "unknown";
×
110
            } else {
111
                category = prefix.substring(1);
×
112
                if (category.equals("xref") && httpReq.getParameter(QueryParameters.ANNOTATION_PARAM) != null) {
×
113
                    category = "annotate";
×
114
                }
115
            }
116
        }
117
        return category;
×
118
    }
119

120
    private boolean isRoot(final HttpServletRequest httpReq) {
121
        return httpReq.getRequestURI().replace(httpReq.getContextPath(), "").equals("/")
×
122
                || httpReq.getRequestURI().replace(httpReq.getContextPath(), "").equals("");
×
123
    }
124

125
    @Override
126
    public void destroy() {
127
        //No destroy Operation
128
    }
×
129
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc