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