Skip to content

Commit 7e060ae

Browse files
committed
Update the naming of metrics to be more consistent with other services.
Add a test for the metrics service Align application.properties and application_test.properties
1 parent e17fcd9 commit 7e060ae

File tree

12 files changed

+368
-195
lines changed

12 files changed

+368
-195
lines changed

Dockerfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
FROM openjdk:11-jre
22
WORKDIR /channelfinder
3-
ADD https://repo1.maven.org/maven2/org/phoebus/app-channel-channelfinder/4.7.2/app-channel-channelfinder-4.7.2.jar .
3+
ADD https://repo1.maven.org/maven2/org/phoebus/ChannelFinder/4.7.2/ChannelFinder-4.7.2.jar .
4+
5+
CMD ["java", "-jar", "./ChannelFinder-*.jar", "--spring.config.name=application"]
6+

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

Lines changed: 63 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,80 +11,92 @@
1111
import org.springframework.stereotype.Service;
1212
import org.springframework.util.LinkedMultiValueMap;
1313

14+
import java.util.ArrayList;
1415
import java.util.Arrays;
15-
import java.util.Map;
1616
import java.util.logging.Level;
1717
import java.util.logging.Logger;
18-
import java.util.stream.Collectors;
19-
2018

2119
@Service
2220
@PropertySource(value = "classpath:application.properties")
2321
public class MetricsService {
2422

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+
2540
@Value("${metrics.tags}")
2641
private String[] tags;
2742

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;
4245

43-
private final ChannelRepository channelRepository;
44-
private final PropertyRepository propertyRepository;
4546
@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) {
4752
this.channelRepository = channelRepository;
4853
this.propertyRepository = propertyRepository;
49-
registerGaugeMetrics(meterRegistry, tagRepository);
54+
this.tagRepository = tagRepository;
55+
this.meterRegistry = meterRegistry;
56+
registerGaugeMetrics();
5057
}
5158

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)
5862
.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)
6265
.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)
6668
.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")
7472
.register(meterRegistry);
75-
7673
}
7774

78-
@Scheduled(fixedRate = 10000)
75+
@Scheduled(fixedRate = 5000)
7976
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);
89101
}
90102
}

src/main/resources/application.properties

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,14 @@ server.ssl.key-alias=cf
1313

1414
security.require-ssl=true
1515

16-
logging.level.org.springframework.web=INFO
17-
spring.http.log-request-details=true
18-
19-
# Enable HTTP/2 support, if the current environment supports it
20-
server.http2.enabled=true
21-
2216
server.compression.enabled=true
2317
# opt in to content types
2418
server.compression.mime-types=application/json,application/xml,text/html,text/xml,text/plain,application/javascript,text/css
2519
# not worth the CPU cycles at some point, probably
2620
server.compression.min-response-size=1024
21+
# Enable HTTP/2 support, if the current environment supports it
22+
server.http2.enabled=true
23+
logging.level.org.springframework.web=INFO
2724

2825
############## LDAP - External ##############
2926
ldap.enabled = false
@@ -36,6 +33,7 @@ ldap.groups.search.pattern = (memberUid= {1})
3633
############## LDAP - Embedded ##############
3734
embedded_ldap.enabled = false
3835
embedded_ldap.urls = ldap://localhost:8389/dc=cf,dc=local
36+
embedded_ldap.base.dn = dc=cf,dc=local
3937
embedded_ldap.user.dn.pattern = uid={0},ou=People,dc=cf,dc=local
4038
embedded_ldap.groups.search.base = ou=Group,dc=cf,dc=local
4139
embedded_ldap.groups.search.pattern = (memberUid= {1})
@@ -58,7 +56,7 @@ demo_auth.users = admin,user
5856
demo_auth.pwds = adminPass,userPass
5957
demo_auth.roles = ADMIN,USER
6058

61-
############## Role --> group Mapping ##############
59+
############## Group-->Role Mapping ##############
6260
# Customize group names here
6361
admin-groups=cf-admins,sys-admins,ADMIN
6462
channel-groups=cf-channels,USER
@@ -67,13 +65,44 @@ tag-groups=cf-tags,USER
6765

6866
############################## Elastic Network And HTTP ###############################
6967

70-
# Elasticsearch host
71-
#elasticsearch.network.host: 169.254.42.56
68+
# Elasticsearch, by default, binds itself to the 0.0.0.0 address, and listens
69+
# on port [9200-9300] for HTTP traffic and on port [9300-9400] for node-to-node
70+
# communication. (the range means that if the port is busy, it will automatically
71+
# try the next port).
72+
73+
# Set the bind address specifically (IPv4 or IPv6):
74+
#
75+
# network.bind_host: 169.254.42.56
76+
77+
# Set the address other nodes will use to communicate with this node. If not
78+
# set, it is automatically derived. It must point to an actual IP address.
79+
#
80+
# network.publish_host: 192.168.0.1
81+
82+
# Set both 'bind_host' and 'publish_host':
83+
#
84+
elasticsearch.network.host: localhost
85+
7286
# Set a custom port for the node to node communication (9300 by default):
87+
#
7388
#elasticsearch.transport.tcp.port: 9300
89+
90+
# Enable compression for all communication between nodes (disabled by default):
91+
#
92+
#transport.tcp.compress: true
93+
7494
# Set a custom port to listen for HTTP traffic:
95+
#
7596
elasticsearch.http.port: 9200
7697

98+
# Set a custom allowed content length:
99+
#
100+
#http.max_content_length: 100mb
101+
102+
# Disable HTTP completely:
103+
#
104+
#http.enabled: false
105+
77106
# Elasticsearch index names and types used by channelfinder, ensure that any changes here should be replicated in the mapping_definitions.sh
78107
elasticsearch.tag.index = cf_tags
79108
elasticsearch.property.index = cf_properties
@@ -101,8 +130,18 @@ aa.pva=false
101130
aa.archive_property_name=archive
102131
aa.archiver_property_name=archiver
103132

133+
# Set the auto pause behaviour
134+
#
135+
# Empty for no auto pause
136+
# Or pvStatus to pause on pvStatus=Inactive
137+
# Or match archive_property_name to pause on archive_property_name not existing
138+
# Or both, i.e. aa.auto_pause=pvStatus,archive
139+
#
140+
aa.auto_pause=pvStatus,archive
141+
142+
104143
############################## Metrics ###############################
105144
#actuator
106-
management.endpoints.web.exposure.include=prometheus
145+
management.endpoints.web.exposure.include=prometheus, metrics, health, info
107146
metrics.tags=
108-
metrics.properties={'pvStatus': 'Active'}
147+
metrics.properties={{'pvStatus', 'Active'}, {'pvStatus', 'Inactive'}}

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

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -476,40 +476,12 @@ public void setup() {
476476

477477
@AfterEach
478478
public void cleanup() {
479-
// clean up
480-
testTags.forEach(tag -> {
481-
try {
482-
tagRepository.deleteById(tag.getName());
483-
} catch (Exception e) {
484-
System.out.println("Failed to clean up tag: " + tag.getName());
485-
}
486-
});
487-
testUpdatedTags.forEach(tag -> {
488-
try {
489-
tagRepository.deleteById(tag.getName());
490-
} catch (Exception e) {
491-
System.out.println("Failed to clean up tag: " + tag.getName());
492-
}
493-
});
494-
testProperties.forEach(property -> {
495-
try {
496-
propertyRepository.deleteById(property.getName());
497-
} catch (Exception e) {
498-
System.out.println("Failed to clean up property: " + property.getName());
499-
}
500-
});
501-
testUpdatedProperties.forEach(property -> {
502-
try {
503-
propertyRepository.deleteById(property.getName());
504-
} catch (Exception e) {
505-
System.out.println("Failed to clean up property: " + property.getName());
506-
}
507-
});
508-
cleanupTestChannels.forEach(channel -> {
509-
if (channelRepository.existsById(channel.getName())) {
510-
channelRepository.deleteById(channel.getName());
511-
}
512-
});
479+
480+
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
481+
map.set("~name", "*");
482+
channelRepository.search(map).getChannels().forEach(c -> channelRepository.deleteById(c.getName()));
483+
tagRepository.findAll().forEach(t -> tagRepository.deleteById(t.getName()));
484+
propertyRepository.findAll().forEach(p -> propertyRepository.deleteById(p.getName()));
513485
}
514486

515487
@AfterAll

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ public void setup() throws InterruptedException {
6363
@AfterEach
6464
public void cleanup() throws InterruptedException {
6565
populateService.cleanupDB();
66-
Thread.sleep(5000);
6766
}
6867

6968
/**

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ public void setup() throws InterruptedException {
4747
@AfterEach
4848
public void cleanup() throws InterruptedException {
4949
populateService.cleanupDB();
50-
Thread.sleep(10000);
5150
}
5251

5352
@BeforeAll

0 commit comments

Comments
 (0)