Skip to content

Commit

Permalink
Merge pull request #2342 from atlanhq/PLT-1879
Browse files Browse the repository at this point in the history
PLT-1879: enable method level metrics in metastore to show in prometheus
  • Loading branch information
n5nk authored Sep 4, 2023
2 parents 95f400d + 4c3c9d7 commit 8763d1a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package org.apache.atlas.service.metrics;

import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.binder.BaseUnits;
import io.micrometer.core.instrument.distribution.DistributionStatisticConfig;
import io.micrometer.core.instrument.Timer;
import io.micrometer.prometheus.PrometheusMeterRegistry;
import org.apache.atlas.ApplicationProperties;
import org.apache.atlas.AtlasException;
Expand All @@ -16,7 +14,10 @@
import javax.inject.Inject;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static org.apache.atlas.service.metrics.MetricUtils.getMeterRegistry;

Expand All @@ -31,18 +32,12 @@ public class MetricsRegistryServiceImpl implements MetricsRegistry {
private static final double[] PERCENTILES = {0.99};
private static final int SEC_MILLIS_SCALE = 1;
private static final String METHOD_LEVEL_METRICS_ENABLE = "atlas.metrics.method_level.enable";

private final DistributionStatisticConfig distributionStatisticConfig;
private static final String ATLAS_METRICS_METHOD_PATTERNS = "atlas.metrics.method_patterns";
private final List<String> filteredMethods;

@Inject
public MetricsRegistryServiceImpl() {
this.distributionStatisticConfig = DistributionStatisticConfig.builder().percentilePrecision(2)
.percentiles(PERCENTILES)
.bufferLength(3)
.percentilesHistogram(false)
.minimumExpectedValue(1.0)
.maximumExpectedValue(Double.MAX_VALUE)
.expiry(Duration.ofMinutes(2)).build();
public MetricsRegistryServiceImpl() throws AtlasException {
this.filteredMethods = Arrays.stream(ApplicationProperties.get().getStringArray(ATLAS_METRICS_METHOD_PATTERNS)).collect(Collectors.toList());
}

@Override
Expand All @@ -55,12 +50,11 @@ public void collect(String requestId, AtlasPerfMetrics metrics) {
LOG.error("Failed to read {} property from atlas config", METHOD_LEVEL_METRICS_ENABLE, e);
return;
}
for (String name : metrics.getMetricsNames()) {

for (String name : this.filteredMethods) {
AtlasPerfMetrics.Metric metric = metrics.getMetric(name);
getMeterRegistry().newDistributionSummary(new Meter.Id(METHOD_DIST_SUMMARY,
Tags.of(NAME, metric.getName()), BaseUnits.MILLISECONDS, METHOD_DIST_SUMMARY,
Meter.Type.TIMER), distributionStatisticConfig, SEC_MILLIS_SCALE)
.record(metric.getTotalTimeMSecs());
Timer.builder(METHOD_DIST_SUMMARY).tags(Tags.of(NAME, metric.getName())).publishPercentiles(PERCENTILES)
.register(getMeterRegistry()).record(metric.getTotalTimeMSecs(), TimeUnit.MILLISECONDS);
}
}

Expand All @@ -72,7 +66,7 @@ public void scrape(PrintWriter writer) {
writer.flush();
} catch (IOException e) {
LOG.warn("Failed to write metrics while scraping", e);
}finally {
} finally {
writer.close();
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.apache.atlas.util.AtlasGremlinQueryProvider.AtlasGremlinQuery;
import org.apache.atlas.util.SearchPredicateUtil;
import org.apache.atlas.util.SearchTracker;
import org.apache.atlas.utils.AtlasPerfMetrics;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections4.IteratorUtils;
Expand Down Expand Up @@ -933,7 +934,9 @@ private void scrubSearchResults(AtlasSearchResult result) throws AtlasBaseExcept
}

private void scrubSearchResults(AtlasSearchResult result, boolean suppressLogs) throws AtlasBaseException {
AtlasPerfMetrics.MetricRecorder scrubSearchResultsMetrics = RequestContext.get().startMetricRecord("scrubSearchResults");
AtlasAuthorizationUtils.scrubSearchResults(new AtlasSearchResultScrubRequest(typeRegistry, result), suppressLogs);
RequestContext.get().endMetricRecord(scrubSearchResultsMetrics);
}

private Set<String> getAggregationFields() {
Expand Down Expand Up @@ -993,9 +996,9 @@ public AtlasSearchResult directIndexSearch(SearchParams searchParams) throws Atl
String indexName = getIndexName(params);

indexQuery = graph.elasticsearchQuery(indexName);

AtlasPerfMetrics.MetricRecorder elasticSearchQueryMetric = RequestContext.get().startMetricRecord("elasticSearchQuery");
DirectIndexQueryResult indexQueryResult = indexQuery.vertices(searchParams);

RequestContext.get().endMetricRecord(elasticSearchQueryMetric);
prepareSearchResult(ret, indexQueryResult, resultAttributes, true);

ret.setAggregations(indexQueryResult.getAggregationMap());
Expand Down

0 comments on commit 8763d1a

Please sign in to comment.