|
11 | 11 | import org.springframework.stereotype.Service;
|
12 | 12 | import org.springframework.util.LinkedMultiValueMap;
|
13 | 13 |
|
| 14 | +import java.util.ArrayList; |
14 | 15 | import java.util.Arrays;
|
15 |
| -import java.util.Map; |
16 | 16 | import java.util.logging.Level;
|
17 | 17 | import java.util.logging.Logger;
|
18 |
| -import java.util.stream.Collectors; |
19 |
| - |
20 | 18 |
|
21 | 19 | @Service
|
22 | 20 | @PropertySource(value = "classpath:application.properties")
|
23 | 21 | public class MetricsService {
|
24 | 22 |
|
| 23 | + private static final Logger logger = Logger.getLogger(MetricsService.class.getName()); |
| 24 | + public static final String CF_TOTAL_CHANNEL_COUNT = "cf.total.channel.count"; |
| 25 | + public static final String CF_PROPERTY_COUNT = "cf.property.count"; |
| 26 | + public static final String CF_TAG_COUNT = "cf.tag.count"; |
| 27 | + public static final String CF_CHANNEL_COUNT = "cf.channel.count"; |
| 28 | + private static final String METRIC_DESCRIPTION_TOTAL_CHANNEL_COUNT = "Count of all ChannelFinder channels"; |
| 29 | + private static final String METRIC_DESCRIPTION_PROPERTY_COUNT = "Count of all ChannelFinder properties"; |
| 30 | + private static final String METRIC_DESCRIPTION_TAG_COUNT = "Count of all ChannelFinder tags"; |
| 31 | + private static final String METRIC_DESCRIPTION_CHANNEL_COUNT = |
| 32 | + "Count of channels with specific property with and specific value"; |
| 33 | + private final ChannelRepository channelRepository; |
| 34 | + private final PropertyRepository propertyRepository; |
| 35 | + private final TagRepository tagRepository; |
| 36 | + private final MeterRegistry meterRegistry; |
| 37 | + |
| 38 | + MultiGauge channelCounts; |
| 39 | + |
25 | 40 | @Value("${metrics.tags}")
|
26 | 41 | private String[] tags;
|
27 | 42 |
|
28 |
| - @Value("#{${metrics.properties:{'pvStatus': 'Active'}}}") |
29 |
| - private Map<String, String> properties; |
30 |
| - |
31 |
| - private static final Logger logger = Logger.getLogger(MetricsService.class.getName()); |
32 |
| - private static final String METRIC_NAME_CHANNEL_COUNT = "cf_channel_count"; |
33 |
| - private static final String METRIC_NAME_PROPERTIES_COUNT = "cf_properties_count"; |
34 |
| - private static final String METRIC_NAME_TAGS_COUNT = "cf_tags_count"; |
35 |
| - private static final String METRIC_NAME_CHANNEL_COUNT_PER_PROPERTY = "cf_channel_count_per_property"; |
36 |
| - private static final String METRIC_NAME_CHANNEL_COUNT_PER_TAG = "cf_channel_count_per_tag"; |
37 |
| - private static final String METRIC_DESCRIPTION_CHANNEL_COUNT = "Count of channel finder channels"; |
38 |
| - private static final String METRIC_DESCRIPTION_PROPERTIES_COUNT = "Count of channel finder properties"; |
39 |
| - private static final String METRIC_DESCRIPTION_TAGS_COUNT = "Count of channel finder tags"; |
40 |
| - private static final String METRIC_DESCRIPTION_CHANNEL_COUNT_PER_PROPERTY = "Count of channels with specific property with and specific value"; |
41 |
| - private static final String METRIC_DESCRIPTION_CHANNEL_COUNT_PER_TAG = "Count of channels with specific tag"; |
| 43 | + @Value("#{${metrics.properties:{{'pvStatus', 'Active'}, {'pvStatus', 'Inactive'}}}}") |
| 44 | + private String[][] properties; |
42 | 45 |
|
43 |
| - private final ChannelRepository channelRepository; |
44 |
| - private final PropertyRepository propertyRepository; |
45 | 46 | @Autowired
|
46 |
| - public MetricsService(final ChannelRepository channelRepository, final PropertyRepository propertyRepository, final TagRepository tagRepository, final MeterRegistry meterRegistry) { |
| 47 | + public MetricsService( |
| 48 | + final ChannelRepository channelRepository, |
| 49 | + final PropertyRepository propertyRepository, |
| 50 | + final TagRepository tagRepository, |
| 51 | + final MeterRegistry meterRegistry) { |
47 | 52 | this.channelRepository = channelRepository;
|
48 | 53 | this.propertyRepository = propertyRepository;
|
49 |
| - registerGaugeMetrics(meterRegistry, tagRepository); |
| 54 | + this.tagRepository = tagRepository; |
| 55 | + this.meterRegistry = meterRegistry; |
| 56 | + registerGaugeMetrics(); |
50 | 57 | }
|
51 | 58 |
|
52 |
| - MultiGauge propertyCounts; |
53 |
| - MultiGauge tagCounts; |
54 |
| - |
55 |
| - private void registerGaugeMetrics(MeterRegistry meterRegistry, TagRepository tagRepository){ |
56 |
| - Gauge.builder(METRIC_NAME_CHANNEL_COUNT, () -> channelRepository.count(new LinkedMultiValueMap<>())) |
57 |
| - .description(METRIC_DESCRIPTION_CHANNEL_COUNT) |
| 59 | + private void registerGaugeMetrics() { |
| 60 | + Gauge.builder(CF_TOTAL_CHANNEL_COUNT, () -> channelRepository.count(new LinkedMultiValueMap<>())) |
| 61 | + .description(METRIC_DESCRIPTION_TOTAL_CHANNEL_COUNT) |
58 | 62 | .register(meterRegistry);
|
59 |
| - Gauge.builder(METRIC_NAME_PROPERTIES_COUNT, |
60 |
| - propertyRepository::count) |
61 |
| - .description(METRIC_DESCRIPTION_PROPERTIES_COUNT) |
| 63 | + Gauge.builder(CF_PROPERTY_COUNT, propertyRepository::count) |
| 64 | + .description(METRIC_DESCRIPTION_PROPERTY_COUNT) |
62 | 65 | .register(meterRegistry);
|
63 |
| - Gauge.builder(METRIC_NAME_TAGS_COUNT, |
64 |
| - tagRepository::count) |
65 |
| - .description(METRIC_DESCRIPTION_TAGS_COUNT) |
| 66 | + Gauge.builder(CF_TAG_COUNT, tagRepository::count) |
| 67 | + .description(METRIC_DESCRIPTION_TAG_COUNT) |
66 | 68 | .register(meterRegistry);
|
67 |
| - |
68 |
| - propertyCounts = MultiGauge.builder(METRIC_NAME_CHANNEL_COUNT_PER_PROPERTY) |
69 |
| - .description(METRIC_DESCRIPTION_CHANNEL_COUNT_PER_PROPERTY) |
70 |
| - .register(meterRegistry); |
71 |
| - |
72 |
| - tagCounts = MultiGauge.builder(METRIC_NAME_CHANNEL_COUNT_PER_TAG) |
73 |
| - .description(METRIC_DESCRIPTION_CHANNEL_COUNT_PER_TAG) |
| 69 | + channelCounts = MultiGauge.builder(CF_CHANNEL_COUNT) |
| 70 | + .description(METRIC_DESCRIPTION_CHANNEL_COUNT) |
| 71 | + .baseUnit("channels") |
74 | 72 | .register(meterRegistry);
|
75 |
| - |
76 | 73 | }
|
77 | 74 |
|
78 |
| - @Scheduled(fixedRate = 10000) |
| 75 | + @Scheduled(fixedRate = 5000) |
79 | 76 | public void updateMetrics() {
|
80 |
| - logger.log(Level.INFO, "Updating metrics"); |
81 |
| - propertyCounts.register( |
82 |
| - properties.entrySet().stream().map(property -> MultiGauge.Row.of(Tags.of(property.getKey(), property.getValue()), |
83 |
| - channelRepository.countByProperty(property.getKey(), property.getValue()))).collect(Collectors.toList()) |
84 |
| - ); |
85 |
| - tagCounts.register( |
86 |
| - Arrays.stream(tags).map(tag -> MultiGauge.Row.of(Tags.of("tag", tag), |
87 |
| - channelRepository.countByTag(tag))).collect(Collectors.toList()) |
88 |
| - ); |
| 77 | + logger.log( |
| 78 | + Level.FINER, |
| 79 | + () -> "Updating metrics for properties " + Arrays.deepToString(properties) + " and tags " + Arrays.toString(tags)); |
| 80 | + ArrayList<MultiGauge.Row<?>> rows = new ArrayList<>(); |
| 81 | + |
| 82 | + // Add tags |
| 83 | + for (String tag: tags) { |
| 84 | + long count = channelRepository.countByTag(tag); |
| 85 | + rows.add(MultiGauge.Row.of(Tags.of("tag", tag), count )); |
| 86 | + logger.log( |
| 87 | + Level.FINER, |
| 88 | + () -> "Updating metrics for tag " + tag + " to " + count); |
| 89 | + } |
| 90 | + |
| 91 | + // Add properties |
| 92 | + for (String[] propertyValue: properties) { |
| 93 | + long count = channelRepository.countByProperty(propertyValue[0], propertyValue[1]); |
| 94 | + rows.add(MultiGauge.Row.of(Tags.of(propertyValue[0], propertyValue[1]), count)); |
| 95 | + logger.log( |
| 96 | + Level.FINER, |
| 97 | + () -> "Updating metrics for property " + propertyValue[0] + ":" + propertyValue[1] + " to " + count); |
| 98 | + } |
| 99 | + |
| 100 | + channelCounts.register(rows, true); |
89 | 101 | }
|
90 | 102 | }
|
0 commit comments