Skip to content

Commit 7e53182

Browse files
authored
Merge pull request #181 from jacomago/addtimeronmetricsagain
Adds an update interval to the property and tag metrics
2 parents a1eb2db + d31538b commit 7e53182

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
lines changed

src/main/java/org/phoebus/channelfinder/MetricsService.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.springframework.beans.factory.annotation.Autowired;
88
import org.springframework.beans.factory.annotation.Value;
99
import org.springframework.context.annotation.PropertySource;
10+
import org.springframework.scheduling.annotation.Scheduled;
1011
import org.springframework.stereotype.Service;
1112
import org.springframework.util.LinkedMultiValueMap;
1213
import org.springframework.util.MultiValueMap;
@@ -16,6 +17,8 @@
1617
import java.util.Arrays;
1718
import java.util.List;
1819
import java.util.Map;
20+
import java.util.concurrent.TimeUnit;
21+
import java.util.concurrent.atomic.AtomicLong;
1922
import java.util.stream.Collectors;
2023
import java.util.Map.Entry;
2124

@@ -41,6 +44,9 @@ public class MetricsService {
4144
private final TagRepository tagRepository;
4245
private final MeterRegistry meterRegistry;
4346

47+
private Map<MultiValueMap<String, String>, AtomicLong> propertyMetrics;
48+
private Map<String, AtomicLong> tagMetrics;
49+
4450
@Value("${metrics.tags}")
4551
private String[] tags;
4652

@@ -87,10 +93,14 @@ private void registerGaugeMetrics() {
8793
registerPropertyMetrics();
8894
}
8995

96+
9097
private void registerTagMetrics() {
9198
// Add tags
99+
tagMetrics = Arrays.stream(tags)
100+
.map(t -> Map.entry(t, new AtomicLong(0)))
101+
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
92102
for (String tag : tags) {
93-
Gauge.builder(CF_TAG_ON_CHANNELS_COUNT, () -> channelRepository.countByTag(tag))
103+
Gauge.builder(CF_TAG_ON_CHANNELS_COUNT, tagMetrics, m -> m.get(tag).doubleValue())
94104
.description("Number of channels with tag")
95105
.tag("tag", tag)
96106
.baseUnit(BASE_UNIT)
@@ -154,14 +164,39 @@ private List<Tag> metricTagsFromMultiValueMap(MultiValueMap<String, String> mult
154164
}
155165
return metricTags;
156166
}
167+
157168
private void registerPropertyMetrics() {
158169
Map<String, List<String>> properties = parseProperties();
159170

160171
List<MultiValueMap<String, String>> combinations = generateAllMultiValueMaps(properties);
161-
combinations.forEach(map -> Gauge.builder(CF_CHANNEL_COUNT, () -> channelRepository.count(map))
172+
173+
propertyMetrics = combinations.stream()
174+
.map(t -> Map.entry(t, new AtomicLong(0)))
175+
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
176+
combinations.forEach(map -> Gauge.builder(CF_CHANNEL_COUNT, propertyMetrics, m -> m.get(map).doubleValue())
162177
.description(METRIC_DESCRIPTION_CHANNEL_COUNT)
163178
.tags(metricTagsFromMultiValueMap(map))
164179
.register(meterRegistry)
165180
);
166181
}
182+
183+
private void updateTagMetrics() {
184+
for (Map.Entry<String, AtomicLong> tagMetricEntry : tagMetrics.entrySet()) {
185+
tagMetricEntry.getValue()
186+
.set(channelRepository.countByTag(tagMetricEntry.getKey()));
187+
}
188+
}
189+
190+
private void updatePropertyMetrics() {
191+
for (Map.Entry<MultiValueMap<String, String>, AtomicLong> propertyMetricEntry : propertyMetrics.entrySet()) {
192+
propertyMetricEntry.getValue()
193+
.set(channelRepository.count(propertyMetricEntry.getKey()));
194+
}
195+
}
196+
197+
@Scheduled(fixedRateString = "${metrics.updateInterval}", timeUnit = TimeUnit.SECONDS)
198+
public void updateMetrics() {
199+
updateTagMetrics();
200+
updatePropertyMetrics();
201+
}
167202
}

src/main/resources/application.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,4 @@ aa.auto_pause=
142142
management.endpoints.web.exposure.include=prometheus, metrics, health, info
143143
metrics.tags=
144144
metrics.properties=pvStatus:Active, Inactive
145+
metrics.updateInterval=60

src/test/java/org/phoebus/channelfinder/MetricsServiceIT.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.springframework.beans.factory.annotation.Autowired;
1313
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
1414
import org.springframework.boot.test.context.SpringBootTest;
15-
import org.springframework.security.core.parameters.P;
1615
import org.springframework.test.context.ActiveProfiles;
1716
import org.springframework.test.context.TestPropertySource;
1817
import org.springframework.test.web.servlet.MockMvc;
@@ -35,7 +34,8 @@
3534
locations = "classpath:application_test.properties",
3635
properties = {
3736
"metrics.tags=testTag0, testTag1",
38-
"metrics.properties=testProperty0: value0, value1; testProperty1: value0, !*"
37+
"metrics.properties=testProperty0: value0, value1; testProperty1: value0, !*",
38+
"metrics.updateInterval=1"
3939
})
4040
class MetricsServiceIT {
4141

@@ -142,6 +142,7 @@ void testTagMultiGaugeMetrics() throws Exception {
142142
Channel testChannel1 = new Channel("testChannelTag1", "testOwner", List.of(), List.of(testTags.get(0)));
143143
channelRepository.save(testChannel1);
144144

145+
Thread.sleep(2000); // Update interval is 1 second
145146
getAndExpectTagMetric(testTags.get(0), 2);
146147
getAndExpectTagMetric(testTags.get(1), 1);
147148
getAndExpectMetricParent(MetricsService.CF_TAG_ON_CHANNELS_COUNT, 3);
@@ -189,6 +190,8 @@ void testPropertyMultiGaugeMetrics() throws Exception {
189190
channelRepository.save(testChannel);
190191
channelRepository.save(testChannel1);
191192

193+
Thread.sleep(2000); // Update interval is 1 second
194+
192195
getAndExpectMetricParent(MetricsService.CF_CHANNEL_COUNT, 2);
193196
getAndExpectPropertyMetric(testChannel.getProperties().get(0), 1);
194197
getAndExpectPropertyMetric(testChannel1.getProperties().get(0), 1);

src/test/resources/application_test.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,5 @@ aa.auto_pause=pvStatus,archive
107107
#actuator
108108
management.endpoints.web.exposure.include=prometheus, metrics, health, info
109109
metrics.tags=group4_10
110-
metrics.properties=group4: 10; group5: 10
110+
metrics.properties=group4: 10; group5: 10
111+
metrics.updateInterval=1

0 commit comments

Comments
 (0)