Skip to content

Commit

Permalink
feat: metrics (prometheus + micrometer)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaansz committed Oct 8, 2024
1 parent c248bea commit 8fc3ff9
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 150 deletions.
15 changes: 13 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ dependencies {
implementation("com.github.Softawii:curupira:v1.0.0") {
changing = true
}

implementation 'io.micrometer:micrometer-registry-prometheus:1.10.13'
implementation 'io.micrometer:micrometer-tracing-bridge-brave:1.0.12'
implementation ('org.springframework.boot:spring-boot-starter-actuator:2.7.0') {
exclude module: 'spring-boot-starter-logging'
}

implementation ('org.springframework.boot:spring-boot-starter-web:2.7.0') {
exclude module: 'spring-boot-starter-logging'
}
}

tasks.register('deploy') {
Expand All @@ -65,6 +75,7 @@ tasks.register('homolog') {
test {
useJUnitPlatform()
}
kotlin {
jvmToolchain(21)
}

sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ public static Button generateDeleteButton(long authorId) {

@DiscordCommand(name = "enable", description = "Enable the automatic Twitter link transformation service")
public TextLocaleResponse enable(Guild guild) {
service.enable(guild.getIdLong());
this.service.enable(guild.getIdLong());
return new TextLocaleResponse("social.twitter.enable.response", guild.getName());
}

@DiscordCommand(name = "disable", description = "Disable the automatic Twitter link transformation service")
public TextLocaleResponse disable(Guild guild) {
service.disable(guild.getIdLong());
this.service.disable(guild.getIdLong());
return new TextLocaleResponse("social.twitter.disable.response", guild.getName());
}

@DiscordButton(name = deleteBotTwitterMessage, ephemeral = true)
public TextLocaleResponse delete(ButtonInteractionEvent event, @RequestInfo Member member) throws MissingPermissionsException {
// Format: ButtonID:Owner
String ownerId = event.getComponentId().split(":")[1];
String ownerId = event.getComponentId().split(":")[1];
String messageOwner = member.getId();

MessageChannelUnion channel = event.getChannel();
Expand All @@ -56,7 +56,7 @@ public TextLocaleResponse delete(ButtonInteractionEvent event, @RequestInfo Memb
}

channel.deleteMessageById(event.getMessageId()).queue();
return new TextLocaleResponse("twitter.delete.response");
}

return new TextLocaleResponse("social.twitter.delete.response");
}
}
132 changes: 0 additions & 132 deletions src/main/java/com/softawii/capivara/core/EmbedManager.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.softawii.capivara.events;

import com.softawii.capivara.controller.SocialTwitterGroup;
import com.softawii.capivara.metrics.SocialMetrics;
import com.softawii.capivara.services.TwitterParserConfigService;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Message;
Expand All @@ -20,13 +21,15 @@
@Component
@SuppressWarnings("unused")
public class TwitterListener extends ListenerAdapter {
private final SocialMetrics metrics;
private final Pattern twitterPattern;
private final TwitterParserConfigService service;
private static final char invisibleChar = '⠀'; // https://www.compart.com/en/unicode/U+2800

public TwitterListener(JDA jda, TwitterParserConfigService service) {
this.service = service;
public TwitterListener(JDA jda, TwitterParserConfigService service, SocialMetrics metrics) {
this.twitterPattern = Pattern.compile("^https://(twitter|x)\\.com/(?<username>\\w+)/status/(?<postId>\\d+)([-a-zA-Z0-9()@:%_+.~#?&/=]*)$"); // https://stackoverflow.com/a/17773849
this.service = service;
this.metrics = metrics;
jda.addEventListener(this);
}

Expand Down Expand Up @@ -63,6 +66,8 @@ private void createTweetMessage(Long guildId, String replacementMessage, Message
.addActionRow(SocialTwitterGroup.generateDeleteButton(originalMessage.getAuthor().getIdLong()))
.setSuppressedNotifications(true)
).queue();

this.metrics.newTwitterParse();
}

private Optional<String> parseMessage(String twitterLink, User author) {
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/softawii/capivara/metrics/SocialMetrics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.softawii.capivara.metrics;

import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.stereotype.Component;

import java.util.concurrent.atomic.AtomicLong;

@Component
public class SocialMetrics {

// General Count
private final AtomicLong parseCount;

public SocialMetrics(MeterRegistry registry) {
// Injected
this.parseCount = registry.gauge("social.twitter.parse", new AtomicLong(0L));
}

public void newTwitterParse() {
this.parseCount.addAndGet(1L);
}
}
69 changes: 69 additions & 0 deletions src/main/java/com/softawii/capivara/metrics/VoiceMetrics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.softawii.capivara.metrics;

import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.stereotype.Component;

import java.util.concurrent.atomic.AtomicLong;

@Component
public class VoiceMetrics {

private final MeterRegistry registry;

// Agent
private final AtomicLong agentCount;
private final AtomicLong agentAdd;
private final AtomicLong agentRemove;
private final AtomicLong agentUpdate;

// Master
private final AtomicLong masterCount;
private final AtomicLong masterAdd;
private final AtomicLong masterRemove;
private final AtomicLong masterUpdate;

public VoiceMetrics(MeterRegistry registry) {
this.registry = registry;
this.agentCount = this.registry.gauge("voice.agents.active", new AtomicLong(0L));
this.agentAdd = this.registry.gauge("voice.agents.add", new AtomicLong(0L));
this.agentRemove = this.registry.gauge("voice.agents.remove", new AtomicLong(0L));
this.agentUpdate = this.registry.gauge("voice.agents.update", new AtomicLong(0L));

this.masterCount = this.registry.gauge("voice.master.active", new AtomicLong(0L));
this.masterAdd = this.registry.gauge("voice.master.add", new AtomicLong(0L));
this.masterRemove = this.registry.gauge("voice.master.remove", new AtomicLong(0L));
this.masterUpdate = this.registry.gauge("voice.master.update", new AtomicLong(0L));
}

public void agentCount(Long count) {
this.agentCount.set(count);
}

public void agentCreated() {
this.agentAdd.addAndGet(1L);
}

public void agentDestroyed() {
this.agentRemove.addAndGet(1L);
}

public void agentUpdate() {
this.agentUpdate.addAndGet(1L);
}

public void masterCount(Long count) {
this.masterCount.set(count);
}

public void masterCreated() {
this.masterAdd.addAndGet(1L);
}

public void masterDestroyed() {
this.masterRemove.addAndGet(1L);
}

public void masterUpdate() {
this.masterUpdate.addAndGet(1L);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.softawii.capivara.entity.TwitterParserConfig;
import com.softawii.capivara.repository.TwitterParserConfigRepository;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Service;

@Service
Expand All @@ -22,6 +23,10 @@ public void enable(Long guildId) {
}

public void disable(Long guildId) {
repository.deleteById(guildId);
try {
repository.deleteById(guildId);
} catch(EmptyResultDataAccessException e) {
// Nothing
}
}
}
Loading

0 comments on commit 8fc3ff9

Please sign in to comment.