From 595833ac2c56de0f6ed248e72141be1661d50eaa Mon Sep 17 00:00:00 2001 From: Sergey Yeranosyan Date: Wed, 31 Jul 2024 09:44:29 +0400 Subject: [PATCH] Added metrics for successfully published messages --- pom.xml | 4 +- slack-java-client/pom.xml | 8 +++ .../hubspot/slack/client/SlackWebClient.java | 49 +++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index b437aee0..0a70210f 100644 --- a/pom.xml +++ b/pom.xml @@ -4,8 +4,8 @@ com.hubspot - basepom - 59.9 + parent-pom + 1.0-SNAPSHOT com.hubspot.slack diff --git a/slack-java-client/pom.xml b/slack-java-client/pom.xml index 09ddedcc..1a218cf1 100644 --- a/slack-java-client/pom.xml +++ b/slack-java-client/pom.xml @@ -31,6 +31,14 @@ com.google.guava guava + + com.yammer.metrics + metrics-core + + + com.hubspot.utils + hubspot-context + diff --git a/slack-java-client/src/main/java/com/hubspot/slack/client/SlackWebClient.java b/slack-java-client/src/main/java/com/hubspot/slack/client/SlackWebClient.java index c4a90b80..1b4be9ba 100644 --- a/slack-java-client/src/main/java/com/hubspot/slack/client/SlackWebClient.java +++ b/slack-java-client/src/main/java/com/hubspot/slack/client/SlackWebClient.java @@ -184,6 +184,10 @@ import com.hubspot.slack.client.paging.LazyLoadingPage; import com.hubspot.slack.client.ratelimiting.ByMethodRateLimiter; import com.hubspot.slack.client.ratelimiting.SlackRateLimiter; +import com.hubspot.utils.hubspot.context.HubSpotContext; +import com.hubspot.utils.hubspot.context.PlatformProperties; +import com.yammer.metrics.Metrics; +import com.yammer.metrics.core.MetricName; import java.io.File; import java.io.IOException; import java.util.Collections; @@ -211,6 +215,11 @@ public class SlackWebClient implements SlackClient { .setObjectMapper(ObjectMapperUtils.mapper()) .build(); private static final AtomicLong REQUEST_COUNTER = new AtomicLong(0); + private static final String UNKNOWN = "UNKNOWN"; + private static final String METRIC_NAME = "SLACK_NOTIFICATIONS"; + private static final String DEPLOYABLE_NAME_TAG = "DEPLOYABLE_NAME"; + private static final String HUB_SPOT_TEAM_TAG = "HUB_SPOT_TEAM"; + private static final String SLACK_MESSAGE_TAG = "SLACK_MESSAGE"; private final NioHttpClient nioHttpClient; private final CloseableExecutorService recursingExecutor; @@ -1766,6 +1775,7 @@ private Result parseSlackResponse( JsonNode responseJson = response.getAsJsonNode(); boolean isOk = responseJson.get("ok").asBoolean(); if (isOk) { + publishMetrics(request); return Result.ok( ObjectMapperUtils.mapper().treeToValue(responseJson, responseType) ); @@ -1780,6 +1790,45 @@ private Result parseSlackResponse( ); } + private void publishMetrics(final HttpRequest request) { + ImmutableMap tags = buildMetrics(request); + if (!tags.isEmpty()) { + incrementMetrics(tags); + } + } + + private void incrementMetrics(final ImmutableMap tags) { + try { + Metrics.newCounter(new MetricName(SlackWebClient.class, METRIC_NAME, tags)).inc(); + } catch (Exception ex) { + LOG.warn("Failed to increment metric with name {}.", METRIC_NAME, ex); + } + } + + private ImmutableMap buildMetrics(HttpRequest request) { + try { + PlatformProperties platformProperties = HubSpotContext + .current() + .getPlatformProperties(); + LOG.info("Building metrics for platform properties {}", platformProperties); + return ImmutableMap + .builder() + .put(DEPLOYABLE_NAME_TAG, platformProperties.getDeployable().orElse(UNKNOWN)) + .put(HUB_SPOT_TEAM_TAG, platformProperties.getHubSpotTeam().orElse(UNKNOWN)) + .put( + SLACK_MESSAGE_TAG, + Optional + .ofNullable(request.getBody(ObjectMapperUtils.mapper())) + .map(String::new) + .orElse(UNKNOWN) + ) + .build(); + } catch (Exception ex) { + LOG.warn("Failed to build metrics for request {}", request, ex); + return ImmutableMap.of(); + } + } + private CompletableFuture> postSlackCommandUrlEncoded( SlackMethod method, Multimap params,