Skip to content

Commit

Permalink
Implement a processed messages metrics exposed via prometheus
Browse files Browse the repository at this point in the history
  • Loading branch information
jirkavrba committed Oct 4, 2024
1 parent 449401c commit 213f446
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package dev.vrba.dubs.metrics;

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 ProcessedMessagesMetric {

@NonNull
private final MeterRegistry registry;

public void record(
@NonNull String user,
@NonNull String channel,
@NonNull String guild
) {
registry.counter("discord.messages.processes",
Tags.of(
Tag.of("guild", guild),
Tag.of("channel", channel),
Tag.of("user", user)
)
).increment();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import dev.vrba.dubs.dto.ChannelDto;
import dev.vrba.dubs.dto.GuildDto;
import dev.vrba.dubs.dto.UserDto;
import dev.vrba.dubs.metrics.ProcessedMessagesMetric;
import dev.vrba.dubs.repository.MatchedPatternRepository;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.transaction.annotation.Transactional;
import jakarta.inject.Singleton;
import lombok.RequiredArgsConstructor;

import java.math.BigInteger;
import java.util.Collections;
import java.util.List;

Expand All @@ -32,6 +34,9 @@ public class MessageProcessingService {
@NonNull
private final PatternMatchingService patternMatchingService;

@NonNull
private final ProcessedMessagesMetric metric;

@NonNull
@Transactional
public List<Pattern> processMessage(
Expand All @@ -40,6 +45,8 @@ public List<Pattern> processMessage(
final @NonNull ChannelDto channel,
final @NonNull GuildDto guild
) {
metric.record(user.id(), channel.id(), guild.id());

final Guild guildEntity = guildService.upsertGuild(guild.id(), guild.name(), guild.icon());
final User userEntity = userService.upsertUser(user.id(), user.name(), user.avatar());
final Channel channelEntity = channelService.upsertChannel(channel.id(), channel.name(), guildEntity);
Expand All @@ -57,6 +64,12 @@ public List<Pattern> processMessage(
))
.toList();

final BigInteger totalPoints = patterns.stream()
.map(Pattern::getPoints)
.map(BigInteger::valueOf)
.reduce(BigInteger.ZERO, BigInteger::add);

userService.incrementUserPoints(userEntity, totalPoints);
matchedPatternRepository.saveAll(mappedPatterns);

return patterns;
Expand Down
8 changes: 8 additions & 0 deletions api/src/main/java/dev/vrba/dubs/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,12 @@ public User upsertUser(
.map(entity -> repository.update(entity.withName(name).withAvatar(avatar)))
.orElseGet(() -> repository.save(new User(id, name, avatar, BigInteger.ZERO)));
}

@NonNull
public User incrementUserPoints(
@NonNull User user,
@NonNull BigInteger points
) {
return repository.update(user.withPoints(user.getPoints().add(points)));
}
}

0 comments on commit 213f446

Please sign in to comment.