/
JerseyTags.java
179 lines (158 loc) · 6.75 KB
/
JerseyTags.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* Copyright 2017 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micrometer.core.instrument.binder.jersey.server;
import io.micrometer.common.lang.Nullable;
import io.micrometer.common.util.StringUtils;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.binder.http.Outcome;
import org.glassfish.jersey.server.ContainerRequest;
import org.glassfish.jersey.server.ContainerResponse;
import org.glassfish.jersey.server.ExtendedUriInfo;
import org.glassfish.jersey.server.monitoring.RequestEvent;
import org.glassfish.jersey.uri.UriTemplate;
import java.util.List;
import java.util.regex.Pattern;
/**
* Factory methods for {@link Tag Tags} associated with a request-response exchange that
* is handled by Jersey server.
*
* @author Michael Weirauch
* @author Johnny Lim
* @since 1.8.0
* @deprecated since 1.13.0 use the jersey-micrometer module in the Jersey project instead
*/
@Deprecated
public final class JerseyTags {
private static final Tag URI_NOT_FOUND = Tag.of("uri", "NOT_FOUND");
private static final Tag URI_REDIRECTION = Tag.of("uri", "REDIRECTION");
private static final Tag URI_ROOT = Tag.of("uri", "root");
private static final Tag URI_UNKNOWN = Tag.of("uri", "UNKNOWN");
private static final Tag EXCEPTION_NONE = Tag.of("exception", "None");
private static final Tag STATUS_SERVER_ERROR = Tag.of("status", "500");
private static final Tag METHOD_UNKNOWN = Tag.of("method", "UNKNOWN");
static final Pattern TRAILING_SLASH_PATTERN = Pattern.compile("/$");
static final Pattern MULTIPLE_SLASH_PATTERN = Pattern.compile("//+");
private JerseyTags() {
}
/**
* Creates a {@code method} tag based on the {@link ContainerRequest#getMethod()
* method} of the given {@code request}.
* @param request the container request
* @return the method tag whose value is a capitalized method (e.g. GET).
*/
public static Tag method(ContainerRequest request) {
return (request != null) ? Tag.of("method", request.getMethod()) : METHOD_UNKNOWN;
}
/**
* Creates a {@code status} tag based on the status of the given {@code response}.
* @param response the container response
* @return the status tag derived from the status of the response
*/
public static Tag status(ContainerResponse response) {
/* In case there is no response we are dealing with an unmapped exception. */
return (response != null) ? Tag.of("status", Integer.toString(response.getStatus())) : STATUS_SERVER_ERROR;
}
/**
* Creates a {@code uri} tag based on the URI of the given {@code event}. Uses the
* {@link ExtendedUriInfo#getMatchedTemplates()} if available. {@code REDIRECTION} for
* 3xx responses, {@code NOT_FOUND} for 404 responses.
* @param event the request event
* @return the uri tag derived from the request event
*/
public static Tag uri(RequestEvent event) {
ContainerResponse response = event.getContainerResponse();
if (response != null) {
int status = response.getStatus();
if (isRedirection(status) && event.getUriInfo().getMatchedResourceMethod() == null) {
return URI_REDIRECTION;
}
if (status == 404 && event.getUriInfo().getMatchedResourceMethod() == null) {
return URI_NOT_FOUND;
}
}
String matchingPattern = getMatchingPattern(event);
if (matchingPattern == null) {
return URI_UNKNOWN;
}
else if (matchingPattern.equals("/")) {
return URI_ROOT;
}
return Tag.of("uri", matchingPattern);
}
static boolean isRedirection(int status) {
return 300 <= status && status < 400;
}
/**
* Gets the pattern for which the request was matched and normalizes it for tagging
* purposes.
* @param event request from which to extract the pattern
* @return normalized matched pattern or {@code null} if nothing matched
*/
@Nullable
static String getMatchingPattern(RequestEvent event) {
ExtendedUriInfo uriInfo = event.getUriInfo();
List<UriTemplate> templates = uriInfo.getMatchedTemplates();
if (templates.isEmpty()) {
return null;
}
StringBuilder sb = new StringBuilder();
sb.append(uriInfo.getBaseUri().getPath());
for (int i = templates.size() - 1; i >= 0; i--) {
sb.append(templates.get(i).getTemplate());
}
String multipleSlashCleaned = MULTIPLE_SLASH_PATTERN.matcher(sb.toString()).replaceAll("/");
if (multipleSlashCleaned.equals("/")) {
return multipleSlashCleaned;
}
return TRAILING_SLASH_PATTERN.matcher(multipleSlashCleaned).replaceAll("");
}
/**
* Creates an {@code exception} tag based on the {@link Class#getSimpleName() simple
* name} of the class of the given {@code exception}.
* @param event the request event
* @return the exception tag derived from the exception
*/
public static Tag exception(RequestEvent event) {
Throwable exception = event.getException();
if (exception == null) {
return EXCEPTION_NONE;
}
ContainerResponse response = event.getContainerResponse();
if (response != null) {
int status = response.getStatus();
if (status == 404 || isRedirection(status)) {
return EXCEPTION_NONE;
}
}
if (exception.getCause() != null) {
exception = exception.getCause();
}
String simpleName = exception.getClass().getSimpleName();
return Tag.of("exception", StringUtils.isNotEmpty(simpleName) ? simpleName : exception.getClass().getName());
}
/**
* Creates an {@code outcome} tag based on the status of the given {@code response}.
* @param response the container response
* @return the outcome tag derived from the status of the response
*/
public static Tag outcome(ContainerResponse response) {
if (response != null) {
return Outcome.forStatus(response.getStatus()).asTag();
}
/* In case there is no response we are dealing with an unmapped exception. */
return Outcome.SERVER_ERROR.asTag();
}
}