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

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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 @@ -45,12 +45,12 @@ void update(double incrementBy) {
synchronized (lock) {
if (count == 0) {
return 0.0;

}
if (aggregationType.equals(AggregationType.AVG)) {
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,166 @@
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
= 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

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,
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
}

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;
} else {
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