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

Always collect available metrics in top queries service #205

Merged
merged 1 commit into from
Jan 24, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,6 @@ public void onRequestFailure(final SearchPhaseContext context, final SearchReque
constructSearchQueryRecord(context, searchRequestContext);
}

private boolean shouldCollect(MetricType metricType) {
return queryInsightsService.isSearchQueryMetricsFeatureEnabled() || queryInsightsService.isCollectionEnabled(metricType);
}

private void constructSearchQueryRecord(final SearchPhaseContext context, final SearchRequestContext searchRequestContext) {
SearchTask searchTask = context.getTask();
List<TaskResourceInfo> tasksResourceUsages = searchRequestContext.getPhaseResourceUsage();
Expand All @@ -240,28 +236,22 @@ private void constructSearchQueryRecord(final SearchPhaseContext context, final
final SearchRequest request = context.getRequest();
try {
Map<MetricType, Measurement> measurements = new HashMap<>();
if (shouldCollect(MetricType.LATENCY)) {
measurements.put(
MetricType.LATENCY,
new Measurement(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - searchRequestContext.getAbsoluteStartNanos()))
);
}
if (shouldCollect(MetricType.CPU)) {
measurements.put(
MetricType.CPU,
new Measurement(
tasksResourceUsages.stream().map(a -> a.getTaskResourceUsage().getCpuTimeInNanos()).mapToLong(Long::longValue).sum()
)
);
}
if (shouldCollect(MetricType.MEMORY)) {
measurements.put(
MetricType.MEMORY,
new Measurement(
tasksResourceUsages.stream().map(a -> a.getTaskResourceUsage().getMemoryInBytes()).mapToLong(Long::longValue).sum()
)
);
}
measurements.put(
MetricType.LATENCY,
new Measurement(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - searchRequestContext.getAbsoluteStartNanos()))
);
measurements.put(
MetricType.CPU,
new Measurement(
tasksResourceUsages.stream().map(a -> a.getTaskResourceUsage().getCpuTimeInNanos()).mapToLong(Long::longValue).sum()
)
);
measurements.put(
MetricType.MEMORY,
new Measurement(
tasksResourceUsages.stream().map(a -> a.getTaskResourceUsage().getMemoryInBytes()).mapToLong(Long::longValue).sum()
)
);

Map<Attribute, Object> attributes = new HashMap<>();
attributes.put(Attribute.SEARCH_TYPE, request.searchType().toString().toLowerCase(Locale.ROOT));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,15 @@ public void incrementSortCounter(double value, Tags tags, Map<MetricType, Measur
}

private void incrementAllHistograms(Tags tags, Map<MetricType, Measurement> measurements) {
queryTypeLatencyHistogram.record(measurements.get(MetricType.LATENCY).getMeasurement().doubleValue(), tags);
queryTypeCpuHistogram.record(measurements.get(MetricType.CPU).getMeasurement().doubleValue(), tags);
queryTypeMemoryHistogram.record(measurements.get(MetricType.MEMORY).getMeasurement().doubleValue(), tags);
if (measurements.containsKey(MetricType.LATENCY)) {
queryTypeLatencyHistogram.record(measurements.get(MetricType.LATENCY).getMeasurement().doubleValue(), tags);
}
if (measurements.containsKey(MetricType.CPU)) {
queryTypeCpuHistogram.record(measurements.get(MetricType.CPU).getMeasurement().doubleValue(), tags);
}
if (measurements.containsKey(MetricType.MEMORY)) {
queryTypeMemoryHistogram.record(measurements.get(MetricType.MEMORY).getMeasurement().doubleValue(), tags);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,9 @@ public String getId() {
* @return the measurement object, or null if not found
*/
public Number getMeasurement(final MetricType name) {
if (!measurements.containsKey(name)) {
return null;
}
return measurements.get(name).getMeasurement();
}

Expand All @@ -337,6 +340,10 @@ public Number getMeasurement(final MetricType name) {
* @param numberToAdd The measurement number we want to add to the current measurement.
*/
public void addMeasurement(final MetricType metricType, Number numberToAdd) {
if (!measurements.containsKey(metricType)) {
measurements.put(metricType, new Measurement(numberToAdd));
return;
}
measurements.get(metricType).addMeasurement(numberToAdd);
}

Expand All @@ -346,6 +353,9 @@ public void addMeasurement(final MetricType metricType, Number numberToAdd) {
* @param aggregationType Aggregation type to set
*/
public void setMeasurementAggregation(final MetricType name, AggregationType aggregationType) {
if (!measurements.containsKey(name)) {
return;
}
measurements.get(name).setAggregationType(aggregationType);
}

Expand Down
Loading