Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 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
0ee6032
initial implementation
harsimar Jan 7, 2025
73f4cc7
merge conflicts and starting to add some logging
harsimar Jan 8, 2025
0c5aae9
logging and testing
harsimar Jan 9, 2025
f1ba015
restructure of error tracking in validator
harsimar Jan 10, 2025
76d8577
pr comments
harsimar Jan 10, 2025
0555332
minor
harsimar Jan 10, 2025
648bc10
some pr comments
harsimar Jan 13, 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
Prev Previous commit
Next Next commit
finish implementation and starting to add tests
  • Loading branch information
harsimar committed Dec 19, 2024
commit 6e81b5520be83a40d2e6b5a7840c0f39a41487eb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@
// Licensed under the MIT License.
package com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.filtering;

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.CollectionConfigurationInfo;
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.DocumentStreamInfo;
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.*;

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


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

// key is the telemetry type
private final Map<TelemetryType, List<DerivedMetricInfo>> validDerivedMetricInfos;
Expand All @@ -32,6 +25,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 @@ -81,11 +76,7 @@ public Map<String, AggregationType> getValidProjectionInitInfo() {
.getDocumentFilterGroups()) {
TelemetryType telemetryType = documentFilterGroupInfo.getTelemetryType();
FilterConjunctionGroupInfo filterGroup = documentFilterGroupInfo.getFilters();

try {
Validator.validateTelemetryType(telemetryType);
Validator.validateDocumentFilters(documentFilterGroupInfo);

if (validator.isValidDocConjunctionGroupInfo(documentFilterGroupInfo)) {
if (!result.containsKey(telemetryType)) {
result.put(telemetryType, new HashMap<>());
}
Expand All @@ -98,8 +89,6 @@ public Map<String, AggregationType> getValidProjectionInitInfo() {
filterGroups.add(filterGroup);
innerMap.put(documentStreamId, filterGroups);
}
} catch (TelemetryTypeException | UnexpectedFilterCreateException e) {
// TODO (harskaur): For ErrorTracker PR: If any validator methods throw an exception, catch the exception and track the error for post request body
}
}
}
Expand All @@ -108,29 +97,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();
try {
if (!seenMetricIds.contains(id)) {
seenMetricIds.add(id);
Validator.validateTelemetryType(telemetryType);
Validator.checkCustomMetricProjection(derivedMetricInfo);

if (!seenMetricIds.contains(id)) {
seenMetricIds.add(id);
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 {
throw new DuplicateMetricIdException("Duplicate Metric Id: " + id);
}
} catch (TelemetryTypeException | UnexpectedFilterCreateException | DuplicateMetricIdException e) {
// TODO (harskaur): For ErrorTracker PR: If any validator methods throw an exception, catch the exception and track the error for post request body
} 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
@@ -1,119 +1,165 @@
package com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.filtering;

import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.*;
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 static final Set<String> knownStringColumns
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 static final Set<String> knownNumericColumns = new HashSet<>(asList(KnownRequestColumns.RESPONSE_CODE,
private final Set<String> knownNumericColumns = new HashSet<>(asList(KnownRequestColumns.RESPONSE_CODE,
KnownRequestColumns.DURATION, KnownDependencyColumns.RESULT_CODE));

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

public static void validateTelemetryType(TelemetryType telemetryType) throws TelemetryTypeException {
if (telemetryType.equals(TelemetryType.PERFORMANCE_COUNTER)) {
throw new TelemetryTypeException(
"The telemetry type PerformanceCounter was specified, but the distro does not send performance counters other than CPU/Mem to quickpulse");
} else if (telemetryType.equals(TelemetryType.EVENT)) {
throw new TelemetryTypeException(
"The telemetry type Event was specified, but the distro does not send events to quickpulse");
} else if (telemetryType.equals(TelemetryType.METRIC)) {
throw new TelemetryTypeException(
"The telemetry type Metric was specified, but the distro does not support sending open telemetry metrics to quickpulse");
// 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;
}
}

public static void checkCustomMetricProjection(DerivedMetricInfo derivedMetricInfo)
throws UnexpectedFilterCreateException {
if (derivedMetricInfo.getProjection().startsWith("CustomMetrics.")) {
throw new UnexpectedFilterCreateException(
"The projection of a customMetric property is not supported in this distro.");
if (!isNotCustomMetricProjection(derivedMetricInfo.getProjection())) {
return false;
}
}

public static void validateMetricFilters(DerivedMetricInfo derivedMetricInfo)
throws UnexpectedFilterCreateException {
for (FilterConjunctionGroupInfo conjunctionGroupInfo : derivedMetricInfo.getFilterGroups()) {
for (FilterInfo filter : conjunctionGroupInfo.getFilters()) {
TelemetryType telemetryType = TelemetryType.fromString(derivedMetricInfo.getTelemetryType());
validateFieldNames(filter.getFieldName(), telemetryType);
validatePredicateAndComparand(filter);
if (!isValidFieldName(filter.getFieldName(), telemetryType)) {
return false;
}
if (!isValidPredicateAndComparand(filter)) {
return false;
}
}
}
return true;
}

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

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

private static boolean isCustomDimOrAnyField(String fieldName) {
return fieldName.startsWith(Filter.CUSTOM_DIM_FIELDNAME_PREFIX) || fieldName.equals(Filter.ANY_FIELD);
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 static void validateFieldNames(String fieldName, TelemetryType telemetryType)
throws UnexpectedFilterCreateException {
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()) {
throw new UnexpectedFilterCreateException("A filter must have a non-empty field name.");
return false;
}
if (fieldName.startsWith("CustomMetrics.")) {
throw new UnexpectedFilterCreateException(
"Filtering of a customMetric property is not supported in this distro.");
return false;
}
return true;
}

private static void validatePredicateAndComparand(FilterInfo filter) throws UnexpectedFilterCreateException {
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.
throw new UnexpectedFilterCreateException("A filter must have a non-empty comparand. FilterName: "
+ filter.getFieldName() + " Predicate: " + filter.getPredicate().getValue());
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.
throw new UnexpectedFilterCreateException(
"The predicate " + filter.getPredicate().getValue() + " is not supported for the field name *");
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) {
throw new UnexpectedFilterCreateException(
"The provided duration timestamp can't be converted to microseconds: " + filter.getComparand());
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) {
throw new UnexpectedFilterCreateException(
"Could not convert the provided result/response code to a numeric value: "
+ filter.getComparand());
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())) {
throw new UnexpectedFilterCreateException("The predicate " + filter.getPredicate().getValue()
+ " is not supported for the field " + filter.getFieldName());
return false;
}
}
return true;
}
}
Loading