Skip to content

Commit

Permalink
PLT-290: Add “uri” as one of the projections for method level metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
n5nk committed Oct 27, 2023
1 parent 1c6e298 commit f755d99
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.micrometer.prometheus.PrometheusConfig;
import io.micrometer.prometheus.PrometheusMeterRegistry;
import org.apache.atlas.ApplicationProperties;
import org.apache.atlas.AtlasException;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -36,18 +37,15 @@ public class MetricUtils {
private static final PrometheusMeterRegistry METER_REGISTRY;

static {
METER_REGISTRY = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
METER_REGISTRY.config().withHighCardinalityTagsDetector().commonTags(SERVICE, ATLAS_METASTORE, INTEGRATION, LOCAL);
Metrics.globalRegistry.add(METER_REGISTRY);
}

public MetricUtils() {
try {
METRIC_URI_PATTERNS_MAP = Arrays.stream(ApplicationProperties.get().getStringArray(ATLAS_METRICS_URI_PATTERNS))
.distinct().collect(Collectors.toMap(uri->uri, uri->uri.replaceAll(REGEX_URI_PLACEHOLDER, "*")));
} catch (Exception e) {
LOG.error("Failed to load 'atlas.metrics.uri_patterns from properties");
}
METER_REGISTRY = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
METER_REGISTRY.config().withHighCardinalityTagsDetector().commonTags(SERVICE, ATLAS_METASTORE, INTEGRATION, LOCAL);
Metrics.globalRegistry.add(METER_REGISTRY);
}

public Timer.Sample start(String uri) {
Expand Down Expand Up @@ -78,7 +76,7 @@ private Tags getTags(String httpMethod, int httpResponseStatus, String uri) {
URI, matchCanonicalPattern(uri).get());
}

private Optional<String> matchCanonicalPattern(String uri) {
public static Optional<String> matchCanonicalPattern(String uri) {
if (Objects.isNull(uri) || uri.isEmpty()) {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public interface MetricsRegistry {

void collect(String requestId, AtlasPerfMetrics metrics);
void collect(String requestId, String requestUri, AtlasPerfMetrics metrics);

void scrape(PrintWriter writer) throws IOException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class MetricsRegistryServiceImpl implements MetricsRegistry {
private static final Logger LOG = LoggerFactory.getLogger(MetricsRegistryServiceImpl.class);

private static final String NAME = "name";
private static final String URI = "uri";
private static final String METHOD_DIST_SUMMARY = "method_dist_summary";
private static final double[] PERCENTILES = {0.99};
private static final String METHOD_LEVEL_METRICS_ENABLE = "atlas.metrics.method_level.enable";
Expand All @@ -40,7 +41,7 @@ public MetricsRegistryServiceImpl() throws AtlasException {
}

@Override
public void collect(String requestId, AtlasPerfMetrics metrics) {
public void collect(String requestId, String requestUri, AtlasPerfMetrics metrics) {
try {
if (!ApplicationProperties.get().getBoolean(METHOD_LEVEL_METRICS_ENABLE, false)) {
return;
Expand All @@ -49,7 +50,7 @@ public void collect(String requestId, AtlasPerfMetrics metrics) {
for (String name : this.filteredMethods) {
if(metrics.hasMetric(name)) {
AtlasPerfMetrics.Metric metric = metrics.getMetric(name);
Timer.builder(METHOD_DIST_SUMMARY).tags(Tags.of(NAME, metric.getName())).publishPercentiles(PERCENTILES)
Timer.builder(METHOD_DIST_SUMMARY).tags(Tags.of(NAME, metric.getName(), URI, requestUri)).publishPercentiles(PERCENTILES)
.register(getMeterRegistry()).record(metric.getTotalTimeMSecs(), TimeUnit.MILLISECONDS);
}
}
Expand Down
12 changes: 10 additions & 2 deletions server-api/src/main/java/org/apache/atlas/RequestContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public class RequestContext {
private MetricsRegistry metricsRegistry;
private boolean skipAuthorizationCheck = false;
private Set<String> deletedEdgesIdsForResetHasLineage = new HashSet<>(0);

private String requestUri;

private RequestContext() {
}
Expand Down Expand Up @@ -159,7 +159,7 @@ public void clearCache() {
if (metrics != null && !metrics.isEmpty()) {
METRICS.debug(metrics.toString());
if (Objects.nonNull(this.metricsRegistry)){
this.metricsRegistry.collect(traceId, metrics);
this.metricsRegistry.collect(traceId, this.requestUri, metrics);
}
metrics.clear();
}
Expand Down Expand Up @@ -642,6 +642,14 @@ public void setMetricRegistry(MetricsRegistry metricsRegistry) {
this.metricsRegistry = metricsRegistry;
}

public void setUri(String uri) {
this.requestUri = uri;
}

public String getRequestUri() {
return this.requestUri;
}

public class EntityGuidPair {
private final Object entity;
private final String guid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.apache.atlas.*;
import org.apache.atlas.authorize.AtlasAuthorizationUtils;
import org.apache.atlas.service.metrics.MetricUtils;
import org.apache.atlas.service.metrics.MetricsRegistry;
import org.apache.atlas.util.AtlasRepositoryConfiguration;
import org.apache.atlas.web.util.DateTimeHelper;
Expand Down Expand Up @@ -95,6 +96,7 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha

RequestContext.clear();
RequestContext requestContext = RequestContext.get();
requestContext.setUri(MetricUtils.matchCanonicalPattern(httpRequest.getRequestURI()).orElse(EMPTY));
requestContext.setTraceId(internalRequestId);
requestContext.setUser(user, userGroups);
requestContext.setClientIPAddress(AtlasAuthorizationUtils.getRequestIpAddress(httpRequest));
Expand Down

0 comments on commit f755d99

Please sign in to comment.