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

Live Metrics Filtering Part 5: Validator #43506

Merged
merged 26 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
4aa78d3
adding projection functionality, rethink automic double
harsimar Dec 10, 2024
bf15276
atomic double and some other changes
harsimar Dec 11, 2024
4ace19d
merge from master to get p3 changes
harsimar Dec 11, 2024
75718c7
pr comments and some build stuff
harsimar Dec 11, 2024
27d4979
changing guava version
harsimar Dec 11, 2024
619bb47
spotbug fix
harsimar Dec 11, 2024
915d579
starting to add tests
harsimar Dec 12, 2024
9c8fa37
added unit tests for derived metric projection
harsimar Dec 13, 2024
e3377d7
fix conflict from main
harsimar Dec 13, 2024
c599211
reorganize tests
harsimar Dec 13, 2024
a6ab936
fixing inconsistency with main re okio
harsimar Dec 13, 2024
494a1b9
pr comments & small refactorings
harsimar Dec 16, 2024
1351d2b
remove logging, spotless
harsimar Dec 16, 2024
a77adfe
validator
harsimar Dec 17, 2024
68b9c83
minor
harsimar Dec 17, 2024
d80053e
changes to concurrency handling
harsimar Dec 17, 2024
f83c116
moving synchronization to derivedMetricAggregation
harsimar Dec 17, 2024
d8b618f
moving derivedMetricAggregation to its own file
harsimar Dec 17, 2024
77f176a
merge projection into here
harsimar Dec 18, 2024
96e705f
validator round 1 - need to refactor for validator to store errors
harsimar Dec 18, 2024
6e81b55
finish implementation and starting to add tests
harsimar Dec 19, 2024
568f059
merge from main
harsimar Dec 19, 2024
de6bd93
spotless
harsimar Dec 20, 2024
f492237
remove unused classes
harsimar Dec 20, 2024
191783d
pr comments
harsimar Jan 6, 2025
c3c3c49
fix ci errors
harsimar Jan 6, 2025
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 @@ -167,6 +167,7 @@
<suppress files="com.azure.monitor.opentelemetry.autoconfigure.implementation.preaggregatedmetrics.DependencyExtractor.java" checks="MissingJavadocMethodCheck" />
<suppress files="com.azure.monitor.opentelemetry.autoconfigure.implementation.preaggregatedmetrics.RequestExtractor.java" checks="MissingJavadocMethodCheck" />
<suppress files="com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.QuickPulse.java" checks="MissingJavadocMethodCheck" />
<suppress files="com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.filtering.Validator.java" checks="MissingJavadocMethodCheck" />
<suppress files="com.azure.monitor.opentelemetry.autoconfigure.implementation.statsbeat.FeatureStatsbeat.java" checks="MissingJavadocMethodCheck" />
<suppress files="com.azure.monitor.opentelemetry.autoconfigure.implementation.statsbeat.IntervalMetricsKey.java" checks="MissingJavadocMethodCheck" />
<suppress files="com.azure.monitor.opentelemetry.autoconfigure.implementation.statsbeat.MetadataInstanceResponse.java" checks="MissingJavadocMethodCheck" />
Expand Down Expand Up @@ -250,6 +251,7 @@
<suppress files="com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.filtering.FilteringConfiguration.java" checks="MissingJavadocTypeCheck" />
<suppress files="com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.filtering.DerivedMetricProjections.java" checks="MissingJavadocTypeCheck" />
<suppress files="com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.filtering.DerivedMetricAggregation.java" checks="MissingJavadocTypeCheck" />
<suppress files="com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.filtering.Validator.java" checks="MissingJavadocTypeCheck" />
<suppress files="com.azure.monitor.opentelemetry.autoconfigure.implementation.statsbeat.CustomDimensions.java" checks="MissingJavadocTypeCheck" />
<suppress files="com.azure.monitor.opentelemetry.autoconfigure.implementation.statsbeat.Feature.java" checks="MissingJavadocTypeCheck" />
<suppress files="com.azure.monitor.opentelemetry.autoconfigure.implementation.statsbeat.FeatureStatsbeat.java" checks="MissingJavadocTypeCheck" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ void update(double incrementBy) {
return aggregation / count;
}
return aggregation;

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.DocumentFilterConjunctionGroupInfo;
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.AggregationType;
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.TelemetryType;
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.CollectionConfigurationErrorType;

import java.util.Set;
import java.util.List;
Expand All @@ -18,7 +19,6 @@
import java.util.HashMap;

public class FilteringConfiguration {
private final Set<String> seenMetricIds = new HashSet<>();

// key is the telemetry type
private final Map<TelemetryType, List<DerivedMetricInfo>> validDerivedMetricInfos;
Expand All @@ -31,6 +31,8 @@ public class FilteringConfiguration {
// key is the derived metric id
private final Map<String, AggregationType> validProjectionInfo;

private final Validator validator = new Validator();

public FilteringConfiguration() {
validDerivedMetricInfos = new HashMap<>();
validDocumentFilterConjunctionGroupInfos = new HashMap<>();
Expand Down Expand Up @@ -80,21 +82,19 @@ public Map<String, AggregationType> getValidProjectionInitInfo() {
.getDocumentFilterGroups()) {
TelemetryType telemetryType = documentFilterGroupInfo.getTelemetryType();
FilterConjunctionGroupInfo filterGroup = documentFilterGroupInfo.getFilters();

// TODO (harskaur): In later PR, validate input before adding it to newValidDocumentsConfig
// TODO (harskaur): If any validator methods throw an exception, catch the exception and track the error for post request body

if (!result.containsKey(telemetryType)) {
result.put(telemetryType, new HashMap<>());
}

Map<String, List<FilterConjunctionGroupInfo>> innerMap = result.get(telemetryType);
if (innerMap.containsKey(documentStreamId)) {
innerMap.get(documentStreamId).add(filterGroup);
} else {
List<FilterConjunctionGroupInfo> filterGroups = new ArrayList<>();
filterGroups.add(filterGroup);
innerMap.put(documentStreamId, filterGroups);
if (validator.isValidDocConjunctionGroupInfo(documentFilterGroupInfo)) {
if (!result.containsKey(telemetryType)) {
result.put(telemetryType, new HashMap<>());
}

Map<String, List<FilterConjunctionGroupInfo>> innerMap = result.get(telemetryType);
if (innerMap.containsKey(documentStreamId)) {
innerMap.get(documentStreamId).add(filterGroup);
} else {
List<FilterConjunctionGroupInfo> filterGroups = new ArrayList<>();
filterGroups.add(filterGroup);
innerMap.put(documentStreamId, filterGroups);
}
}
}
}
Expand All @@ -103,23 +103,29 @@ public Map<String, AggregationType> getValidProjectionInitInfo() {

private Map<TelemetryType, List<DerivedMetricInfo>>
parseMetricFilterConfiguration(CollectionConfigurationInfo configuration) {
Set<String> seenMetricIds = new HashSet<>();
Map<TelemetryType, List<DerivedMetricInfo>> result = new HashMap<>();
for (DerivedMetricInfo derivedMetricInfo : configuration.getMetrics()) {
TelemetryType telemetryType = TelemetryType.fromString(derivedMetricInfo.getTelemetryType());
String id = derivedMetricInfo.getId();

if (!seenMetricIds.contains(id)) {
seenMetricIds.add(id);
// TODO (harskaur): In later PR, validate input before adding it to newValidConfig
// TODO (harskaur): If any validator methods throw an exception, catch the exception and track the error for post request body

if (result.containsKey(telemetryType)) {
result.get(telemetryType).add(derivedMetricInfo);
} else {
List<DerivedMetricInfo> infos = new ArrayList<>();
infos.add(derivedMetricInfo);
result.put(telemetryType, infos);
if (validator.isValidDerivedMetricInfo(derivedMetricInfo)) {
if (result.containsKey(telemetryType)) {
result.get(telemetryType).add(derivedMetricInfo);
} else {
List<DerivedMetricInfo> infos = new ArrayList<>();
infos.add(derivedMetricInfo);
result.put(telemetryType, infos);
}
}
} else {
validator.constructAndTrackCollectionConfigurationError(
CollectionConfigurationErrorType.METRIC_DUPLICATE_IDS,
"A duplicate metric id was found in this configuration", configuration.getETag(), id, true);
}

}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.filtering;

import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.PredicateType;
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.TelemetryType;
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.DerivedMetricInfo;
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.FilterConjunctionGroupInfo;
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.DocumentFilterConjunctionGroupInfo;
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.FilterInfo;
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.CollectionConfigurationError;
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.CollectionConfigurationErrorType;
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.KeyValuePairString;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static java.util.Arrays.asList;

public class Validator {
private final Set<String> knownStringColumns
harsimar marked this conversation as resolved.
Show resolved Hide resolved
= new HashSet<String>(asList(KnownRequestColumns.URL, KnownRequestColumns.NAME, KnownDependencyColumns.DATA,
KnownDependencyColumns.TARGET, KnownDependencyColumns.TYPE, KnownTraceColumns.MESSAGE,
KnownExceptionColumns.MESSAGE, KnownExceptionColumns.STACK));

private final Set<String> knownNumericColumns = new HashSet<>(
asList(KnownRequestColumns.RESPONSE_CODE, KnownRequestColumns.DURATION, KnownDependencyColumns.RESULT_CODE));

private final Set<PredicateType> validStringPredicates = new HashSet<>(
asList(PredicateType.CONTAINS, PredicateType.DOES_NOT_CONTAIN, PredicateType.EQUAL, PredicateType.NOT_EQUAL));

// TODO (harskaur): In ErrorTracker PR, track a list of configuration validation errors here
harsimar marked this conversation as resolved.
Show resolved Hide resolved
private final List<CollectionConfigurationError> errors = new ArrayList<>();

public boolean isValidDerivedMetricInfo(DerivedMetricInfo derivedMetricInfo) {
TelemetryType telemetryType = TelemetryType.fromString(derivedMetricInfo.getTelemetryType());
if (!isValidTelemetryType(telemetryType)) {
return false;
}

if (!isNotCustomMetricProjection(derivedMetricInfo.getProjection())) {
return false;
}

for (FilterConjunctionGroupInfo conjunctionGroupInfo : derivedMetricInfo.getFilterGroups()) {
for (FilterInfo filter : conjunctionGroupInfo.getFilters()) {
if (!isValidFieldName(filter.getFieldName(), telemetryType)) {
return false;
}
if (!isValidPredicateAndComparand(filter)) {
return false;
}
}
}
return true;
}

public boolean
isValidDocConjunctionGroupInfo(DocumentFilterConjunctionGroupInfo documentFilterConjunctionGroupInfo) {
TelemetryType telemetryType = documentFilterConjunctionGroupInfo.getTelemetryType();
if (!isValidTelemetryType(telemetryType)) {
return false;
}

FilterConjunctionGroupInfo conjunctionGroupInfo = documentFilterConjunctionGroupInfo.getFilters();
for (FilterInfo filter : conjunctionGroupInfo.getFilters()) {
if (!isValidFieldName(filter.getFieldName(), telemetryType)) {
return false;
}
if (!isValidPredicateAndComparand(filter)) {
return false;
}
}
return true;
}

public void constructAndTrackCollectionConfigurationError(CollectionConfigurationErrorType errorType,
harsimar marked this conversation as resolved.
Show resolved Hide resolved
String message, String eTag, String id, boolean isDerivedMetricId) {
CollectionConfigurationError error = new CollectionConfigurationError();
error.setMessage(message);
error.setCollectionConfigurationErrorType(errorType);

KeyValuePairString keyValuePair1 = new KeyValuePairString();
keyValuePair1.setKey("ETag");
keyValuePair1.setValue(eTag);

KeyValuePairString keyValuePair2 = new KeyValuePairString();
keyValuePair2.setKey(isDerivedMetricId ? "DerivedMetricInfoId" : "DocumentStreamInfoId");
keyValuePair2.setValue(id);

List<KeyValuePairString> data = new ArrayList<>();
data.add(keyValuePair1);
data.add(keyValuePair2);

error.setData(data);

// TODO (harskaur): For ErrorTracker PR, add this error to list of tracked errors
errors.add(error);
}

private boolean isValidTelemetryType(TelemetryType telemetryType) {
// TODO (harskaur): In ErrorTracker PR, create an error message & track an error for each false case
if (telemetryType.equals(TelemetryType.PERFORMANCE_COUNTER)) {
return false;
} else if (telemetryType.equals(TelemetryType.EVENT)) {
return false;
} else if (telemetryType.equals(TelemetryType.METRIC)) {
return false;
}
return true;

}

private boolean isNotCustomMetricProjection(String projection) {
if (projection.startsWith("CustomMetrics.")) {
// TODO (harskaur): In ErrorTracker PR, create an error message & track an error for this case
return false;
}
return true;
}

private boolean isValidFieldName(String fieldName, TelemetryType telemetryType) {
// TODO (harskaur): In ErrorTracker PR, create an error message & track an error for each false case
if (fieldName.isEmpty()) {
return false;
}
if (fieldName.startsWith("CustomMetrics.")) {
return false;
}
return true;
}

private boolean isValidPredicateAndComparand(FilterInfo filter) {
// TODO (harskaur): In ErrorTracker PR, create an error message & track an error for each false case
if (filter.getComparand().isEmpty()) {
// It is possible to not type in a comparand and the service side to send us empty string.
return false;
} else if (Filter.ANY_FIELD.equals(filter.getFieldName())
&& !(filter.getPredicate().equals(PredicateType.CONTAINS)
|| filter.getPredicate().equals(PredicateType.DOES_NOT_CONTAIN))) {
// While the UI allows != and == for the ANY_FIELD fieldName, .net classic code only allows contains/not contains & the spec follows
// .net classic behavior for this particular condition.
return false;
} else if (knownNumericColumns.contains(filter.getFieldName())) {
// Just in case a strange timestamp value is passed from the service side. The service side should send a duration with a specific
// format ([days].[hours]:[minutes]:[seconds] - the seconds may be a whole number or something like 7.89).
if (KnownDependencyColumns.DURATION.equals(filter.getFieldName())) {
if (Filter.getMicroSecondsFromFilterTimestampString(filter.getComparand()) == Long.MIN_VALUE) {
return false;
}
} else { // The service side not does not validate if resultcode or responsecode is a numeric value
try {
Long.parseLong(filter.getComparand());
} catch (NumberFormatException e) {
return false;
}
}
} else if (knownStringColumns.contains(filter.getFieldName())
|| filter.getFieldName().startsWith(Filter.CUSTOM_DIM_FIELDNAME_PREFIX)) {
// While the UI allows a user to select any predicate for a custom dimension filter, .net classic treats all custom dimensions like
// String values. therefore we validate for predicates applicable to String. This is called out in the spec as well.
if (!validStringPredicates.contains(filter.getPredicate())) {
return false;
}
}
return true;
}
}
Loading
Loading