private validatePredicateAndComparand()

in sdk/monitor/monitor-opentelemetry/src/metrics/quickpulse/filtering/validator.ts [125:197]


  private validatePredicateAndComparand(filter: FilterInfo): void {
    if (!(filter.predicate in KnownPredicateType)) {
      throw new UnexpectedFilterCreateError(`'${filter.predicate}' is not a valid predicate.`);
    } else if (filter.comparand === "") {
      throw new UnexpectedFilterCreateError(
        `A filter must have a comparand. FilterName: '${filter.fieldName}' Predicate: '${filter.predicate}' Comparand: '${filter.comparand}'`,
      );
    } else if (
      filter.fieldName === "*" &&
      !(
        filter.predicate === KnownPredicateType.Contains.toString() ||
        filter.predicate === KnownPredicateType.DoesNotContain.toString()
      )
    ) {
      throw new UnexpectedFilterCreateError(
        `The predicate '${filter.predicate}' is not supported for the field name '*'`,
      );
    } else if (
      filter.fieldName === KnownDependencyColumns.ResultCode.toString() ||
      filter.fieldName === KnownRequestColumns.ResponseCode.toString() ||
      filter.fieldName === KnownDependencyColumns.Duration.toString()
    ) {
      if (
        filter.predicate === KnownPredicateType.Contains.toString() ||
        filter.predicate === KnownPredicateType.DoesNotContain.toString()
      ) {
        throw new UnexpectedFilterCreateError(
          `The predicate '${filter.predicate}' is not supported for the field name '${filter.fieldName}'`,
        );
      }
      // Duration comparand should be a timestamp; Response/ResultCode comparand should be interpreted as double.
      if (filter.fieldName === KnownDependencyColumns.Duration.toString()) {
        if (isNaN(getMsFromFilterTimestampString(filter.comparand))) {
          throw new UnexpectedFilterCreateError(
            `The comparand '${filter.comparand}' can't be converted to a double (ms).`,
          );
        }
      } else if (isNaN(parseFloat(filter.comparand))) {
        throw new UnexpectedFilterCreateError(
          `The comparand '${filter.comparand}' can't be converted to a double.`,
        );
      }
    } else if (
      knownStringColumns.has(filter.fieldName) ||
      filter.fieldName.startsWith("CustomDimensions.")
    ) {
      if (
        filter.predicate === KnownPredicateType.GreaterThan.toString() ||
        filter.predicate === KnownPredicateType.GreaterThanOrEqual.toString() ||
        filter.predicate === KnownPredicateType.LessThan.toString() ||
        filter.predicate === KnownPredicateType.LessThanOrEqual.toString()
      ) {
        throw new UnexpectedFilterCreateError(
          `The predicate '${filter.predicate}' is not supported for the field name '${filter.fieldName}'. If this is a custom dimension, it would be treated as string.`,
        );
      }
    } else if (filter.fieldName === KnownRequestColumns.Success.toString()) {
      if (
        filter.predicate !== KnownPredicateType.Equal.toString() &&
        filter.predicate !== KnownPredicateType.NotEqual.toString()
      ) {
        throw new UnexpectedFilterCreateError(
          `The predicate '${filter.predicate}' is not supported for the field name '${filter.fieldName}'.`,
        );
      }
      filter.comparand = filter.comparand.toLowerCase();
      if (filter.comparand !== "true" && filter.comparand !== "false") {
        throw new UnexpectedFilterCreateError(
          `The comparand '${filter.comparand}' is not a valid boolean value for the fieldName Success.`,
        );
      }
    }
  }