Skip to content

Commit b53b991

Browse files
committed
Allow to set prefix for Micrometer prefixes
1 parent cde09e8 commit b53b991

2 files changed

Lines changed: 75 additions & 3 deletions

File tree

src/main/java/eu/openanalytics/containerproxy/stat/impl/Micrometer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,15 @@ public void init() {
8181
userLogouts = registry.counter("userLogouts");
8282
appStartFailedCounter = registry.counter("startFailed");
8383
authFailedCounter = registry.counter("authFailed");
84-
registry.gauge("shinyproxy_absolute_users_logged_in", Tags.empty(), sessionService, ISessionService::getLoggedInUsersCount);
85-
registry.gauge("shinyproxy_absolute_users_active", Tags.empty(), sessionService, ISessionService::getActiveUsersCount);
84+
registry.gauge("absolute_users_logged_in", Tags.empty(), sessionService, ISessionService::getLoggedInUsersCount);
85+
registry.gauge("absolute_users_active", Tags.empty(), sessionService, ISessionService::getActiveUsersCount);
8686

8787
for (ProxySpec spec : proxyService.getProxySpecs(null, true)) {
8888
registry.counter("appStarts", "spec.id", spec.getId());
8989
registry.counter("appStops", "spec.id", spec.getId());
9090
ProxyCounter proxyCounter = new ProxyCounter(spec.getId());
9191
proxyCounters.add(proxyCounter);
92-
registry.gauge("shinyproxy_absolute_apps_running", Tags.of("spec.id", spec.getId()), proxyCounter, ProxyCounter::getProxyCount);
92+
registry.gauge("absolute_apps_running", Tags.of("spec.id", spec.getId()), proxyCounter, ProxyCounter::getProxyCount);
9393
registry.timer("startupTime", "spec.id", spec.getId());
9494
registry.timer("usageTime", "spec.id", spec.getId());
9595
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* ContainerProxy
3+
*
4+
* Copyright (C) 2016-2021 Open Analytics
5+
*
6+
* ===========================================================================
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the Apache License as published by
10+
* The Apache Software Foundation, either version 2 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* Apache License for more details.
17+
*
18+
* You should have received a copy of the Apache License
19+
* along with this program. If not, see <http://www.apache.org/licenses/>
20+
*/
21+
package eu.openanalytics.containerproxy.stat.impl;
22+
23+
import io.micrometer.core.instrument.Meter;
24+
import io.micrometer.core.instrument.MeterRegistry;
25+
import io.micrometer.core.lang.NonNull;
26+
import io.micrometer.core.lang.Nullable;
27+
import io.micrometer.prometheus.PrometheusNamingConvention;
28+
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
29+
import org.springframework.context.annotation.Bean;
30+
import org.springframework.context.annotation.Configuration;
31+
import org.springframework.core.env.Environment;
32+
33+
import javax.inject.Inject;
34+
35+
@Configuration(proxyBeanMethods = false)
36+
public class MicrometerRegistryConfiguration {
37+
38+
private static final String PROP_METRIC_PREFIX = "proxy.usage-stats-micrometer-prefix";
39+
40+
@Inject
41+
private Environment environment;
42+
43+
private String getPrefix() {
44+
String prefix = environment.getProperty(PROP_METRIC_PREFIX, "").trim();
45+
if (!prefix.isEmpty()) {
46+
prefix += ".";
47+
}
48+
return prefix;
49+
}
50+
51+
@Bean
52+
public MeterRegistryCustomizer<MeterRegistry> metricsCommonTags() {
53+
String prefix = getPrefix();
54+
return (registry) -> registry
55+
.config()
56+
.namingConvention(new PrometheusNamingConvention() {
57+
@NonNull
58+
@Override
59+
public String name(@NonNull String name, @NonNull Meter.Type type, @Nullable String baseUnit) {
60+
return super.name(prefix + name, type, baseUnit);
61+
}
62+
63+
@NonNull
64+
@Override
65+
public String tagKey(@NonNull String key) {
66+
return super.tagKey(key);
67+
}
68+
});
69+
}
70+
71+
72+
}

0 commit comments

Comments
 (0)