-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement exporting matches via prometheus metrics
- Loading branch information
Showing
9 changed files
with
237 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package dev.vrba.dubs.domain; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Id; | ||
import jakarta.validation.constraints.Size; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.math.BigInteger; | ||
|
||
@Getter | ||
@Entity(name = "matches") | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class Match { | ||
@Id | ||
@Column(name = "row_id") | ||
private Integer id; | ||
|
||
@Size(max = 64) | ||
@Column(name = "pattern_name", length = 64) | ||
private String patternName; | ||
|
||
@Column(name = "pattern_points") | ||
private Long patternPoints; | ||
|
||
@Column(name = "pattern_is_rare") | ||
private Boolean patternIsRare; | ||
|
||
@Size(max = 32) | ||
@Column(name = "user_id", length = 32) | ||
private String userId; | ||
|
||
@Size(max = 128) | ||
@Column(name = "user_name", length = 128) | ||
private String userName; | ||
|
||
@Size(max = 32) | ||
@Column(name = "channel_id", length = 32) | ||
private String channelId; | ||
|
||
@Size(max = 128) | ||
@Column(name = "channel_name", length = 128) | ||
private String channelName; | ||
|
||
@Size(max = 32) | ||
@Column(name = "guild_id", length = 32) | ||
private String guildId; | ||
|
||
@Size(max = 128) | ||
@Column(name = "guild_name", length = 128) | ||
private String guildName; | ||
|
||
@Column(name = "count") | ||
private BigInteger count; | ||
} |
41 changes: 41 additions & 0 deletions
41
api/src/main/java/dev/vrba/dubs/metrics/MatchedPatternsMetric.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package dev.vrba.dubs.metrics; | ||
|
||
import dev.vrba.dubs.domain.Channel; | ||
import dev.vrba.dubs.domain.Guild; | ||
import dev.vrba.dubs.domain.Pattern; | ||
import dev.vrba.dubs.domain.User; | ||
import io.micrometer.core.instrument.MeterRegistry; | ||
import io.micrometer.core.instrument.Tag; | ||
import io.micrometer.core.instrument.Tags; | ||
import io.micronaut.core.annotation.NonNull; | ||
import jakarta.inject.Singleton; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Singleton | ||
@RequiredArgsConstructor | ||
public class MatchedPatternsMetric { | ||
|
||
@NonNull | ||
private final MeterRegistry registry; | ||
|
||
public void record( | ||
@NonNull User user, | ||
@NonNull Channel channel, | ||
@NonNull Guild guild, | ||
@NonNull Pattern pattern | ||
) { | ||
registry.counter("discord.messages.patterns", | ||
Tags.of( | ||
Tag.of("guild.id", guild.getId()), | ||
Tag.of("guild.name", guild.getName()), | ||
Tag.of("channel.id", channel.getId()), | ||
Tag.of("channel.name", channel.getName()), | ||
Tag.of("guild.id", guild.getId()), | ||
Tag.of("guild.name", guild.getName()), | ||
Tag.of("user.id", user.getId()), | ||
Tag.of("user.name", user.getName()), | ||
Tag.of("pattern", pattern.getName()) | ||
) | ||
).increment(); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
api/src/main/java/dev/vrba/dubs/metrics/MatchesMetricConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package dev.vrba.dubs.metrics; | ||
|
||
import dev.vrba.dubs.domain.Match; | ||
import dev.vrba.dubs.repository.MatchRepository; | ||
import io.micrometer.core.instrument.MeterRegistry; | ||
import io.micrometer.core.instrument.MultiGauge; | ||
import io.micrometer.core.instrument.Tag; | ||
import io.micrometer.core.instrument.Tags; | ||
import io.micronaut.core.annotation.NonNull; | ||
import io.micronaut.scheduling.annotation.Scheduled; | ||
import jakarta.inject.Singleton; | ||
|
||
import java.util.stream.Collectors; | ||
|
||
@Singleton | ||
public class MatchesMetricConfiguration { | ||
|
||
@NonNull | ||
private final MultiGauge gauge; | ||
|
||
@NonNull | ||
private final MatchRepository repository; | ||
|
||
public MatchesMetricConfiguration( | ||
final @NonNull MeterRegistry registry, | ||
final @NonNull MatchRepository repository | ||
) { | ||
this.gauge = MultiGauge.builder("pattern.matches").register(registry); | ||
this.repository = repository; | ||
} | ||
|
||
@Scheduled(fixedRate = "PT1M") | ||
public void refresh() { | ||
gauge.register( | ||
repository.findAll() | ||
.stream() | ||
.map(this::mapPatternToGaugeRow) | ||
.collect(Collectors.toList()), | ||
true | ||
); | ||
} | ||
|
||
private MultiGauge.Row<Number> mapPatternToGaugeRow(final @NonNull Match match) { | ||
return MultiGauge.Row.of( | ||
Tags.of( | ||
Tag.of("user.id", match.getUserId()), | ||
Tag.of("user.name", match.getUserName()), | ||
Tag.of("channel.id", match.getChannelId()), | ||
Tag.of("channel.name", match.getChannelName()), | ||
Tag.of("guild.id", match.getGuildId()), | ||
Tag.of("guild.name", match.getGuildName()), | ||
Tag.of("pattern.name", match.getPatternName()), | ||
Tag.of("pattern.rare", match.getPatternIsRare().toString()), | ||
Tag.of("pattern.points", match.getPatternPoints().toString()) | ||
), | ||
match.getCount() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
api/src/main/java/dev/vrba/dubs/repository/MatchRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package dev.vrba.dubs.repository; | ||
|
||
import dev.vrba.dubs.domain.Match; | ||
import io.micronaut.data.annotation.Repository; | ||
import io.micronaut.data.repository.CrudRepository; | ||
|
||
@Repository | ||
public interface MatchRepository extends CrudRepository<Match, Integer> { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
api/src/main/resources/migrations/V6__add_row_id_to_matches.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
drop view if exists matches; | ||
create or replace view matches as | ||
( | ||
select row_number() over (order by users.id) as row_id, | ||
pattern_name, | ||
pattern_points, | ||
pattern_is_rare, | ||
users.id as user_id, | ||
users.name as user_name, | ||
channels.id as channel_id, | ||
channels.name as channel_name, | ||
guilds.id as guild_id, | ||
guilds.name as guild_name, | ||
count(*) as count | ||
from matched_patterns | ||
left join users on matched_patterns.user_id = users.id | ||
left join channels on matched_patterns.channel_id = channels.id | ||
left join guilds on channels.guild_id = guilds.id | ||
group by users.id, | ||
channels.id, | ||
guilds.id, | ||
pattern_name, | ||
pattern_points, | ||
pattern_is_rare | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters