Skip to content

Commit 4bd712d

Browse files
authored
Merge pull request #1171 from yue9944882/follow-up-prom
Instrumenting follow-up: metrics labelling w/ finer granularity
2 parents d106084 + 007f613 commit 4bd712d

File tree

9 files changed

+439
-12
lines changed

9 files changed

+439
-12
lines changed

pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
<common.codec.version>1.14</common.codec.version>
5050
<spring.boot.version>2.3.3.RELEASE</spring.boot.version>
5151
<spring.version>5.2.8.RELEASE</spring.version>
52+
<prometheus.client.version>0.9.0</prometheus.client.version>
5253

5354
<gpg.keyname>48540ECBBF00A28EACCF04E720FD12AFB0C9EBA9</gpg.keyname>
5455
<gpg.passphrase>${env.GPG_PASSPHRASE}</gpg.passphrase>
@@ -154,6 +155,18 @@
154155
<artifactId>spring-boot-autoconfigure</artifactId>
155156
<version>${spring.boot.version}</version>
156157
</dependency>
158+
<dependency>
159+
<groupId>io.prometheus</groupId>
160+
<artifactId>simpleclient</artifactId>
161+
<version>${prometheus.client.version}</version>
162+
<optional>true</optional>
163+
</dependency>
164+
<dependency>
165+
<groupId>io.prometheus</groupId>
166+
<artifactId>simpleclient_httpserver</artifactId>
167+
<version>${prometheus.client.version}</version>
168+
<optional>true</optional>
169+
</dependency>
157170

158171

159172
<!-- tests -->

util/pom.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,10 @@
1515
<dependency>
1616
<groupId>io.prometheus</groupId>
1717
<artifactId>simpleclient</artifactId>
18-
<version>0.9.0</version>
19-
<optional>true</optional>
2018
</dependency>
2119
<dependency>
2220
<groupId>io.prometheus</groupId>
2321
<artifactId>simpleclient_httpserver</artifactId>
24-
<version>0.9.0</version>
25-
<optional>true</optional>
2622
</dependency>
2723
<dependency>
2824
<groupId>io.kubernetes</groupId>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.apimachinery;
14+
15+
public class GroupVersionKind {
16+
17+
private final String group;
18+
private final String version;
19+
private final String kind;
20+
21+
public GroupVersionKind(String group, String version, String kind) {
22+
this.group = group;
23+
this.version = version;
24+
this.kind = kind;
25+
}
26+
27+
public String getGroup() {
28+
return group;
29+
}
30+
31+
public String getVersion() {
32+
return version;
33+
}
34+
35+
public String getKind() {
36+
return kind;
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.apimachinery;
14+
15+
public class GroupVersionResource {
16+
17+
private final String group;
18+
private final String version;
19+
private final String resource;
20+
21+
public GroupVersionResource(String group, String version, String resource) {
22+
this.group = group;
23+
this.version = version;
24+
this.resource = resource;
25+
}
26+
27+
public String getGroup() {
28+
return group;
29+
}
30+
31+
public String getVersion() {
32+
return version;
33+
}
34+
35+
public String getResource() {
36+
return resource;
37+
}
38+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.apimachinery;
14+
15+
import com.google.common.base.Strings;
16+
import java.util.regex.Pattern;
17+
import okhttp3.Request;
18+
19+
public class KubernetesRequestDigest {
20+
21+
public static final Pattern RESOURCE_URL_PATH_PATTERN =
22+
Pattern.compile("^/(api|apis)(/\\S+)?/v\\d\\w*/\\S+");
23+
24+
KubernetesRequestDigest(
25+
String urlPath,
26+
boolean isNonResourceRequest,
27+
KubernetesResource resourceMeta,
28+
KubernetesVerb verb) {
29+
this.urlPath = urlPath;
30+
this.isNonResourceRequest = isNonResourceRequest;
31+
this.resourceMeta = resourceMeta;
32+
this.verb = verb;
33+
}
34+
35+
public static KubernetesRequestDigest parse(Request request) {
36+
String urlPath = request.url().encodedPath();
37+
if (!isResourceRequest(urlPath)) {
38+
return nonResource(urlPath);
39+
}
40+
try {
41+
KubernetesResource resourceMeta;
42+
if (urlPath.startsWith("/api/v1")) {
43+
resourceMeta = KubernetesResource.parseCoreResource(urlPath);
44+
} else {
45+
resourceMeta = KubernetesResource.parseRegularResource(urlPath);
46+
}
47+
48+
return new KubernetesRequestDigest(
49+
urlPath,
50+
false,
51+
resourceMeta,
52+
KubernetesVerb.of(
53+
request.method(), hasNamePathParameter(resourceMeta), hasWatchParameter(request)));
54+
} catch (ParseKubernetesResourceException e) {
55+
return nonResource(urlPath);
56+
}
57+
}
58+
59+
private static KubernetesRequestDigest nonResource(String urlPath) {
60+
KubernetesRequestDigest digest = new KubernetesRequestDigest(urlPath, true, null, null);
61+
return digest;
62+
}
63+
64+
public static boolean isResourceRequest(String urlPath) {
65+
return RESOURCE_URL_PATH_PATTERN.matcher(urlPath).matches();
66+
}
67+
68+
private static boolean hasWatchParameter(Request request) {
69+
return !Strings.isNullOrEmpty(request.url().queryParameter("watch"));
70+
}
71+
72+
private static boolean hasNamePathParameter(KubernetesResource resource) {
73+
return !Strings.isNullOrEmpty(resource.getName());
74+
}
75+
76+
private final String urlPath;
77+
private final boolean isNonResourceRequest;
78+
79+
private final KubernetesResource resourceMeta;
80+
private final KubernetesVerb verb;
81+
82+
public String getUrlPath() {
83+
return urlPath;
84+
}
85+
86+
public boolean isNonResourceRequest() {
87+
return isNonResourceRequest;
88+
}
89+
90+
public KubernetesResource getResourceMeta() {
91+
return resourceMeta;
92+
}
93+
94+
public KubernetesVerb getVerb() {
95+
return verb;
96+
}
97+
98+
@Override
99+
public String toString() {
100+
if (isNonResourceRequest) {
101+
return new StringBuilder().append(verb).append(' ').append(urlPath).toString();
102+
}
103+
104+
String groupVersion;
105+
if (Strings.isNullOrEmpty(resourceMeta.getGroupVersionResource().getGroup())) { // core resource
106+
groupVersion = "";
107+
} else { // regular resource
108+
groupVersion =
109+
resourceMeta.getGroupVersionResource().getGroup()
110+
+ "/"
111+
+ resourceMeta.getGroupVersionResource().getVersion();
112+
}
113+
114+
String targetResourceName;
115+
if (Strings.isNullOrEmpty(resourceMeta.getSubResource())) {
116+
targetResourceName = resourceMeta.getGroupVersionResource().getResource();
117+
} else { // subresource
118+
targetResourceName =
119+
resourceMeta.getGroupVersionResource().getResource()
120+
+ "/"
121+
+ resourceMeta.getSubResource();
122+
}
123+
124+
return new StringBuilder()
125+
.append(verb.value())
126+
.append(' ')
127+
.append(groupVersion)
128+
.append(' ')
129+
.append(targetResourceName)
130+
.toString();
131+
}
132+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.apimachinery;
14+
15+
import java.util.regex.Matcher;
16+
import java.util.regex.Pattern;
17+
18+
public class KubernetesResource {
19+
20+
public static final Pattern CORE_RESOURCE_URL_PATH_PATTERN =
21+
Pattern.compile(
22+
"^/api/v1(/namespaces/(?<namespace>[\\w-]+))?/(?<resource>[\\w-]+)(/(?<name>[\\w-]+))?(/(?<subresource>[\\w-]+))?");
23+
24+
public static final Pattern REGULAR_RESOURCE_URL_PATH_PATTERN =
25+
Pattern.compile(
26+
"^/apis/(?<group>\\S+?)/(?<version>\\S+?)(/namespaces/(?<namespace>[\\w-]+))?/(?<resource>[\\w-]+)(/(?<name>[\\w-]+))?(/(?<subresource>[\\w-]+))?");
27+
28+
public static KubernetesResource parseCoreResource(String urlPath)
29+
throws ParseKubernetesResourceException {
30+
Matcher matcher = CORE_RESOURCE_URL_PATH_PATTERN.matcher(urlPath);
31+
if (!matcher.matches()) {
32+
throw new ParseKubernetesResourceException();
33+
}
34+
KubernetesResource resource =
35+
new KubernetesResource(
36+
new GroupVersionResource("", "v1", matcher.group("resource")),
37+
matcher.group("subresource"),
38+
matcher.group("namespace"),
39+
matcher.group("name"));
40+
return resource;
41+
}
42+
43+
public static KubernetesResource parseRegularResource(String urlPath)
44+
throws ParseKubernetesResourceException {
45+
Matcher matcher = REGULAR_RESOURCE_URL_PATH_PATTERN.matcher(urlPath);
46+
if (!matcher.matches()) {
47+
throw new ParseKubernetesResourceException();
48+
}
49+
KubernetesResource resource =
50+
new KubernetesResource(
51+
new GroupVersionResource(
52+
matcher.group("group"), matcher.group("version"), matcher.group("resource")),
53+
matcher.group("subresource"),
54+
matcher.group("namespace"),
55+
matcher.group("name"));
56+
return resource;
57+
}
58+
59+
KubernetesResource(GroupVersionResource gvr, String subResource, String namespace, String name) {
60+
this.groupVersionResource = gvr;
61+
this.subResource = subResource;
62+
this.namespace = namespace;
63+
this.name = name;
64+
}
65+
66+
private final GroupVersionResource groupVersionResource;
67+
private final String subResource;
68+
69+
private final String namespace;
70+
private final String name;
71+
72+
public GroupVersionResource getGroupVersionResource() {
73+
return groupVersionResource;
74+
}
75+
76+
public String getSubResource() {
77+
return subResource;
78+
}
79+
80+
public String getNamespace() {
81+
return namespace;
82+
}
83+
84+
public String getName() {
85+
return name;
86+
}
87+
}

0 commit comments

Comments
 (0)