[AB-8] adding slash command and message context commands to metrics

This commit is contained in:
Sheldan
2022-07-25 18:23:44 +02:00
parent e3e0ad98ba
commit 43a78a4989
3 changed files with 42 additions and 4 deletions

View File

@@ -109,15 +109,16 @@ public class CommandReceivedHandler extends ListenerAdapter {
public static final String COMMAND_CONFIRMATION_MESSAGE_TEMPLATE_KEY = "command_confirmation_message";
public static final String COMMAND_PROCESSED = "command.processed";
public static final String STATUS_TAG = "status";
public static final String TYPE_TAG = "type";
public static final CounterMetric COMMANDS_PROCESSED_COUNTER = CounterMetric
.builder()
.name(COMMAND_PROCESSED)
.tagList(Arrays.asList(MetricTag.getTag(STATUS_TAG, "processed")))
.tagList(Arrays.asList(MetricTag.getTag(STATUS_TAG, "processed"), MetricTag.getTag(TYPE_TAG, "message")))
.build();
public static final CounterMetric COMMANDS_WRONG_PARAMETER_COUNTER = CounterMetric
.builder()
.name(COMMAND_PROCESSED)
.tagList(Arrays.asList(MetricTag.getTag(STATUS_TAG, "parameter.wrong")))
.tagList(Arrays.asList(MetricTag.getTag(STATUS_TAG, "parameter.wrong"), MetricTag.getTag(TYPE_TAG, "message")))
.build();
@Override

View File

@@ -1,8 +1,12 @@
package dev.sheldan.abstracto.core.interaction.context.message;
import dev.sheldan.abstracto.core.command.CommandReceivedHandler;
import dev.sheldan.abstracto.core.config.FeatureConfig;
import dev.sheldan.abstracto.core.interaction.InteractionResult;
import dev.sheldan.abstracto.core.interaction.context.message.listener.MessageContextCommandListener;
import dev.sheldan.abstracto.core.metric.service.CounterMetric;
import dev.sheldan.abstracto.core.metric.service.MetricService;
import dev.sheldan.abstracto.core.metric.service.MetricTag;
import dev.sheldan.abstracto.core.models.listener.interaction.MessageContextInteractionModel;
import dev.sheldan.abstracto.core.service.FeatureConfigService;
import dev.sheldan.abstracto.core.service.FeatureFlagService;
@@ -17,6 +21,8 @@ import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
@@ -48,11 +54,20 @@ public class MessageContextCommandListenerBean extends ListenerAdapter {
@Autowired
private FeatureModeService featureModeService;
@Autowired
private MetricService metricService;
public static final CounterMetric MESSAGE_CONTEXT_COMMANDS_PROCESSED_COUNTER = CounterMetric
.builder()
.name(CommandReceivedHandler.COMMAND_PROCESSED)
.tagList(Arrays.asList(MetricTag.getTag(CommandReceivedHandler.STATUS_TAG, "processed"), MetricTag.getTag(CommandReceivedHandler.TYPE_TAG, "context.message")))
.build();
@Override
public void onMessageContextInteraction(@NotNull MessageContextInteractionEvent event) {
if(listenerList == null) return;
CompletableFuture.runAsync(() -> self.executeListenerLogic(event), messageContextCommandExecutor).exceptionally(throwable -> {
log.error("Failed to execute listener logic in async button event.", throwable);
log.error("Failed to execute listener logic in async message context event.", throwable);
return null;
});
}
@@ -68,6 +83,7 @@ public class MessageContextCommandListenerBean extends ListenerAdapter {
List<MessageContextCommandListener> validListener = filterFeatureAwareListener(listenerList, model);
Optional<MessageContextCommandListener> listenerOptional = findListener(validListener, model);
if(listenerOptional.isPresent()) {
metricService.incrementCounter(MESSAGE_CONTEXT_COMMANDS_PROCESSED_COUNTER);
listener = listenerOptional.get();
listener.execute(model);
InteractionResult result = InteractionResult.fromSuccess();
@@ -116,4 +132,9 @@ public class MessageContextCommandListenerBean extends ListenerAdapter {
return listenerList;
}
@PostConstruct
public void filterPostProcessors() {
metricService.registerCounter(MESSAGE_CONTEXT_COMMANDS_PROCESSED_COUNTER, "Message context commands processed");
}
}

View File

@@ -1,11 +1,15 @@
package dev.sheldan.abstracto.core.interaction.slash;
import dev.sheldan.abstracto.core.command.Command;
import dev.sheldan.abstracto.core.command.CommandReceivedHandler;
import dev.sheldan.abstracto.core.command.condition.ConditionResult;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.command.service.CommandService;
import dev.sheldan.abstracto.core.command.service.PostCommandExecution;
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
import dev.sheldan.abstracto.core.metric.service.CounterMetric;
import dev.sheldan.abstracto.core.metric.service.MetricService;
import dev.sheldan.abstracto.core.metric.service.MetricTag;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
@@ -19,6 +23,7 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
@@ -45,6 +50,15 @@ public class SlashCommandListenerBean extends ListenerAdapter {
@Autowired
private List<PostCommandExecution> executions;
@Autowired
private MetricService metricService;
public static final CounterMetric SLASH_COMMANDS_PROCESSED_COUNTER = CounterMetric
.builder()
.name(CommandReceivedHandler.COMMAND_PROCESSED)
.tagList(Arrays.asList(MetricTag.getTag(CommandReceivedHandler.STATUS_TAG, "processed"), MetricTag.getTag(CommandReceivedHandler.TYPE_TAG, "slash")))
.build();
public List<Command> getSlashCommands() {
if(commands == null || commands.isEmpty()) {
return new ArrayList<>();
@@ -59,7 +73,7 @@ public class SlashCommandListenerBean extends ListenerAdapter {
public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) {
if(commands == null || commands.isEmpty()) return;
CompletableFuture.runAsync(() -> self.executeListenerLogic(event), slashCommandExecutor).exceptionally(throwable -> {
log.error("Failed to execute listener logic in async button event.", throwable);
log.error("Failed to execute listener logic in async slash command event.", throwable);
return null;
});
}
@@ -68,6 +82,7 @@ public class SlashCommandListenerBean extends ListenerAdapter {
public void executeListenerLogic(SlashCommandInteractionEvent event) {
Optional<Command> potentialCommand = findCommand(event);
potentialCommand.ifPresent(command -> {
metricService.incrementCounter(SLASH_COMMANDS_PROCESSED_COUNTER);
try {
commandService.isCommandExecutable(command, event).thenAccept(conditionResult -> {
self.executeCommand(event, command, conditionResult);
@@ -123,6 +138,7 @@ public class SlashCommandListenerBean extends ListenerAdapter {
@PostConstruct
public void filterPostProcessors() {
metricService.registerCounter(SLASH_COMMANDS_PROCESSED_COUNTER, "Slash Commands processed");
executions = executions
.stream()
.filter(PostCommandExecution::supportsSlash)