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

oracle / opengrok / #3642

23 Oct 2023 02:33PM UTC coverage: 75.784% (+1.4%) from 74.413%
#3642

push

web-flow
Sonar code smell issue fixes (#4450)

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

200 of 200 new or added lines in 39 files covered. (100.0%)

44390 of 58574 relevant lines covered (75.78%)

0.76 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

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

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

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

60
        requests.record(1);
×
61

62
        HttpServletRequest httpReq = (HttpServletRequest) servletRequest;
×
63

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

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

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

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

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

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

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

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

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

124
    @Override
125
    public void destroy() {
126
        //No destroy Operation
127
    }
×
128
}
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