From f755d9978c6334b934b390242c85f9650d4c1aa6 Mon Sep 17 00:00:00 2001 From: naveenaechan Date: Fri, 27 Oct 2023 15:27:51 +0530 Subject: [PATCH] =?UTF-8?q?PLT-290:=20Add=20=E2=80=9Curi=E2=80=9D=20as=20o?= =?UTF-8?q?ne=20of=20the=20projections=20for=20method=20level=20metrics?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apache/atlas/service/metrics/MetricUtils.java | 12 +++++------- .../atlas/service/metrics/MetricsRegistry.java | 2 +- .../service/metrics/MetricsRegistryServiceImpl.java | 5 +++-- .../main/java/org/apache/atlas/RequestContext.java | 12 ++++++++++-- .../org/apache/atlas/web/filters/AuditFilter.java | 2 ++ 5 files changed, 21 insertions(+), 12 deletions(-) diff --git a/common/src/main/java/org/apache/atlas/service/metrics/MetricUtils.java b/common/src/main/java/org/apache/atlas/service/metrics/MetricUtils.java index 60008e5f9d..c309190391 100644 --- a/common/src/main/java/org/apache/atlas/service/metrics/MetricUtils.java +++ b/common/src/main/java/org/apache/atlas/service/metrics/MetricUtils.java @@ -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; @@ -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) { @@ -78,7 +76,7 @@ private Tags getTags(String httpMethod, int httpResponseStatus, String uri) { URI, matchCanonicalPattern(uri).get()); } - private Optional matchCanonicalPattern(String uri) { + public static Optional matchCanonicalPattern(String uri) { if (Objects.isNull(uri) || uri.isEmpty()) { return Optional.empty(); } diff --git a/common/src/main/java/org/apache/atlas/service/metrics/MetricsRegistry.java b/common/src/main/java/org/apache/atlas/service/metrics/MetricsRegistry.java index 19dd1d69fe..1730fb50de 100644 --- a/common/src/main/java/org/apache/atlas/service/metrics/MetricsRegistry.java +++ b/common/src/main/java/org/apache/atlas/service/metrics/MetricsRegistry.java @@ -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; diff --git a/common/src/main/java/org/apache/atlas/service/metrics/MetricsRegistryServiceImpl.java b/common/src/main/java/org/apache/atlas/service/metrics/MetricsRegistryServiceImpl.java index 34493e0d51..65a5be31f9 100644 --- a/common/src/main/java/org/apache/atlas/service/metrics/MetricsRegistryServiceImpl.java +++ b/common/src/main/java/org/apache/atlas/service/metrics/MetricsRegistryServiceImpl.java @@ -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"; @@ -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; @@ -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); } } diff --git a/server-api/src/main/java/org/apache/atlas/RequestContext.java b/server-api/src/main/java/org/apache/atlas/RequestContext.java index 99568e89ee..750d020406 100644 --- a/server-api/src/main/java/org/apache/atlas/RequestContext.java +++ b/server-api/src/main/java/org/apache/atlas/RequestContext.java @@ -95,7 +95,7 @@ public class RequestContext { private MetricsRegistry metricsRegistry; private boolean skipAuthorizationCheck = false; private Set deletedEdgesIdsForResetHasLineage = new HashSet<>(0); - + private String requestUri; private RequestContext() { } @@ -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(); } @@ -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; diff --git a/webapp/src/main/java/org/apache/atlas/web/filters/AuditFilter.java b/webapp/src/main/java/org/apache/atlas/web/filters/AuditFilter.java index 30b3a48edd..d3cb21b94d 100755 --- a/webapp/src/main/java/org/apache/atlas/web/filters/AuditFilter.java +++ b/webapp/src/main/java/org/apache/atlas/web/filters/AuditFilter.java @@ -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; @@ -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));