Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added metrics for successfully published messages #350

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

<parent>
<groupId>com.hubspot</groupId>
<artifactId>basepom</artifactId>
<version>59.9</version>
<artifactId>parent-pom</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<groupId>com.hubspot.slack</groupId>
Expand Down
8 changes: 8 additions & 0 deletions slack-java-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>com.yammer.metrics</groupId>
<artifactId>metrics-core</artifactId>
</dependency>
<dependency>
<groupId>com.hubspot.utils</groupId>
<artifactId>hubspot-context</artifactId>
</dependency>

<!-- temporary -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1766,6 +1775,7 @@ private <T extends SlackResponse> Result<T, SlackError> parseSlackResponse(
JsonNode responseJson = response.getAsJsonNode();
boolean isOk = responseJson.get("ok").asBoolean();
if (isOk) {
publishMetrics(request);
return Result.ok(
ObjectMapperUtils.mapper().treeToValue(responseJson, responseType)
);
Expand All @@ -1780,6 +1790,45 @@ private <T extends SlackResponse> Result<T, SlackError> parseSlackResponse(
);
}

private void publishMetrics(final HttpRequest request) {
ImmutableMap<String, String> tags = buildMetrics(request);
if (!tags.isEmpty()) {
incrementMetrics(tags);
}
}

private void incrementMetrics(final ImmutableMap<String, String> 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<String, String> buildMetrics(HttpRequest request) {
try {
PlatformProperties platformProperties = HubSpotContext
.current()
.getPlatformProperties();
LOG.info("Building metrics for platform properties {}", platformProperties);
return ImmutableMap
.<String, String>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 <T extends SlackResponse> CompletableFuture<Result<T, SlackError>> postSlackCommandUrlEncoded(
SlackMethod method,
Multimap<String, String> params,
Expand Down
Loading