forked from ChannelFinder/ChannelFinderService
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetricsService.java
More file actions
110 lines (96 loc) · 4.46 KB
/
MetricsService.java
File metadata and controls
110 lines (96 loc) · 4.46 KB
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
package org.phoebus.channelfinder;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.MultiGauge;
import io.micrometer.core.instrument.Tags;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
@Service
@PropertySource(value = "classpath:application.properties")
public class MetricsService {
public static final String CF_TOTAL_CHANNEL_COUNT = "cf.total.channel.count";
public static final String CF_PROPERTY_COUNT = "cf.property.count";
public static final String CF_TAG_COUNT = "cf.tag.count";
public static final String CF_PROPERTY_FORMAT_STRING = "cf.%s.channel.count";
public static final String CF_CHANNEL_COUNT = "cf.channel.count";
private static final String METRIC_DESCRIPTION_TOTAL_CHANNEL_COUNT = "Count of all ChannelFinder channels";
private static final String METRIC_DESCRIPTION_PROPERTY_COUNT = "Count of all ChannelFinder properties";
private static final String METRIC_DESCRIPTION_TAG_COUNT = "Count of all ChannelFinder tags";
private final ChannelRepository channelRepository;
private final PropertyRepository propertyRepository;
private final TagRepository tagRepository;
private final MeterRegistry meterRegistry;
@Value("${metrics.tags}")
private String[] tags;
@Value("${metrics.properties}")
private String metricProperties;
Map<String, List<String>> parseProperties() {
if (metricProperties == null || metricProperties.isEmpty()) {
return new LinkedMultiValueMap<>();
}
return Arrays.stream(metricProperties.split(";")).map(s ->
{
String[] split = s.split(":");
String k = split[0].trim();
List<String> v = Arrays.stream(split[1].split(",")).map(String::trim).toList();
return Map.entry(k, v);
}).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
@Autowired
public MetricsService(
final ChannelRepository channelRepository,
final PropertyRepository propertyRepository,
final TagRepository tagRepository,
final MeterRegistry meterRegistry) {
this.channelRepository = channelRepository;
this.propertyRepository = propertyRepository;
this.tagRepository = tagRepository;
this.meterRegistry = meterRegistry;
}
@PostConstruct
private void registerGaugeMetrics() {
Gauge.builder(CF_TOTAL_CHANNEL_COUNT, () -> channelRepository.count(new LinkedMultiValueMap<>()))
.description(METRIC_DESCRIPTION_TOTAL_CHANNEL_COUNT)
.register(meterRegistry);
Gauge.builder(CF_PROPERTY_COUNT, propertyRepository::count)
.description(METRIC_DESCRIPTION_PROPERTY_COUNT)
.register(meterRegistry);
Gauge.builder(CF_TAG_COUNT, tagRepository::count)
.description(METRIC_DESCRIPTION_TAG_COUNT)
.register(meterRegistry);
registerTagMetrics();
registerPropertyMetrics();
}
private void registerTagMetrics() {
// Add tags
for (String tag : tags) {
Gauge.builder(CF_CHANNEL_COUNT, () -> channelRepository.countByTag(tag))
.description("Number of channels with tag")
.tag("tag", tag)
.baseUnit("channels")
.register(meterRegistry);
}
}
private void registerPropertyMetrics() {
Map<String, List<String>> properties = parseProperties();
properties.forEach((propertyName, propertyValues) -> propertyValues.forEach(propertyValue ->
Gauge.builder(String.format(CF_PROPERTY_FORMAT_STRING, propertyName), () -> channelRepository.countByProperty(propertyName, propertyValue))
.description(String.format("Number of channels with property '%s'", propertyName))
.tag(propertyName, propertyValue)
.baseUnit("channels")
.register(meterRegistry))
);
}
}