Skip to content

Commit

Permalink
log 1 warn then debug
Browse files Browse the repository at this point in the history
  • Loading branch information
pirgeo committed Oct 11, 2023
1 parent 2fbb70c commit 5476be7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
*/
package com.dynatrace.metric.util;

import static com.dynatrace.metric.util.MetricLineConstants.ValidationMessages.THROTTLE_INFO_TEMPLATE;

import com.dynatrace.metric.util.MetricLineConstants.ValidationMessages;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.logging.Logger;

class MetricLineBuilderImpl
Expand All @@ -29,6 +32,9 @@ class MetricLineBuilderImpl
MetricLineBuilder.CounterStep,
MetricLineBuilder.TimestampOrBuildStep,
MetricLineBuilder.BuildStep {
private static boolean metricKeyNormalizationWarnLogged = false;
private static boolean dimensionKeyNormalizationWarnLogged = false;
private static boolean dimensionValueNormalizationWarnLogged = false;
private static final Logger logger = Logger.getLogger(MetricLineBuilderImpl.class.getName());
private static final AtomicInteger timestampWarningCounter = new AtomicInteger(0);
private static final int TIMESTAMP_WARNING_THROTTLE_FACTOR = 1000;
Expand Down Expand Up @@ -81,7 +87,13 @@ public MetricLineBuilder.TypeStep metricKey(String key) throws MetricException {
NormalizationResult normalizationResult = Normalizer.normalizeMetricKey(this.metricKey);

if (normalizationResult.messageType() == NormalizationResult.MessageType.WARNING) {
logger.warning(() -> normalizationResult.getMessage());
if (!metricKeyNormalizationWarnLogged) {
logger.warning(
() -> String.format(THROTTLE_INFO_TEMPLATE, normalizationResult.getMessage()));
metricKeyNormalizationWarnLogged = true;
} else {
logger.fine(() -> normalizationResult.getMessage());
}
} else if (normalizationResult.messageType() == NormalizationResult.MessageType.ERROR) {
throw new MetricException(
String.format(ValidationMessages.METRIC_DROPPED_AFTER_NORMALIZATION_MESSAGE, key));
Expand Down Expand Up @@ -118,8 +130,14 @@ public MetricLineBuilder.TypeStep dimension(String key, String value) throws Met

normalizedKey = normalizationResult.getResult();
if (normalizationResult.messageType() != NormalizationResult.MessageType.NONE) {
logger.warning(
() -> String.format(PREFIX_STRING, this.metricKey, normalizationResult.getMessage()));
Supplier<String> messageSupplier =
() -> String.format(PREFIX_STRING, this.metricKey, normalizationResult.getMessage());
if (!dimensionKeyNormalizationWarnLogged) {
logger.warning(() -> String.format(THROTTLE_INFO_TEMPLATE, messageSupplier.get()));
dimensionKeyNormalizationWarnLogged = true;
} else {
logger.fine(messageSupplier);
}
}
}

Expand All @@ -143,8 +161,14 @@ public MetricLineBuilder.TypeStep dimension(String key, String value) throws Met
Normalizer.normalizeDimensionValue(
value, MetricLineConstants.Limits.MAX_DIMENSION_VALUE_LENGTH);
if (normalizedValue.messageType() != NormalizationResult.MessageType.NONE) {
logger.warning(
() -> String.format(PREFIX_STRING, this.metricKey, normalizedValue.getMessage()));
Supplier<String> messageSupplier =
() -> String.format(PREFIX_STRING, this.metricKey, normalizedValue.getMessage());
if (!dimensionValueNormalizationWarnLogged) {
logger.warning(() -> String.format(THROTTLE_INFO_TEMPLATE, messageSupplier.get()));
dimensionValueNormalizationWarnLogged = true;
} else {
logger.fine(messageSupplier);
}
}

// only increase the dimensionCount if this key doesn't already exist in the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ private ValidationMessages() {}
"[%s] " + TOO_MANY_DIMENSIONS_MESSAGE;

// warnings
static final String METRIC_KEY_NORMALIZED_MESSAGE = "Metric key normalized from: '%s' to: '%s'";
static final String METRIC_KEY_NORMALIZED_MESSAGE = "Metric key normalized from '%s' to '%s'";

static final String DIMENSION_KEY_NORMALIZED_MESSAGE =
"Dimension key normalized from: '%s' to: '%s'";
"Dimension key normalized from '%s' to '%s'";
static final String DIMENSION_VALUE_NORMALIZED_MESSAGE =
"Dimension value normalized from: '%s' to: '%s'";
"Dimension value normalized from '%s' to '%s'";
static final String DIMENSION_DROPPED_KEY_EMPTY_MESSAGE =
"Dimension with empty dimension key has been dropped";
static final String DIMENSION_DROPPED_KEY_EMPTY_WITH_METRIC_KEY_MESSAGE =
Expand All @@ -120,5 +120,8 @@ private ValidationMessages() {}
+ "The timestamp represents a time before the year 2000 or after the year 3000. "
+ "Skipping setting timestamp, the current server time will be added upon ingestion. "
+ "Only one out of every %d of these messages will be printed.";

static final String THROTTLE_INFO_TEMPLATE =
"%s. Further metric key normalization logs will be logged at debug level.";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
*/
package com.dynatrace.metric.util;

import static com.dynatrace.metric.util.MetricLineConstants.ValidationMessages.THROTTLE_INFO_TEMPLATE;

import com.dynatrace.metric.util.MetricLineConstants.ValidationMessages;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.logging.Logger;

/**
Expand All @@ -35,6 +38,8 @@ public class MetricLinePreConfiguration {
private final Map<String, String> defaultDimensions;
private final String prefix;
private final int serializationLength;
private static boolean dimensionValueNormalizationWarnLogged;
private static boolean dimensionKeyNormalizationWarnLogged;

private MetricLinePreConfiguration(
String prefix,
Expand Down Expand Up @@ -175,7 +180,7 @@ public MetricLinePreConfiguration build() throws MetricException {
}

for (Map.Entry<String, String> entry : this.defaultDimensions.entrySet()) {
// key needs to be normalized before checking if it aleady exists, which is why the
// key needs to be normalized before checking if it already exists, which is why the
// `containsKey` method needs to be passed in here.
dimension(
entry.getKey(),
Expand Down Expand Up @@ -223,7 +228,13 @@ private void dimension(

normalizedKey = normalizationResult.getResult();
if (normalizationResult.messageType() != NormalizationResult.MessageType.NONE) {
logger.warning(() -> normalizationResult.getMessage());
Supplier<String> messageSupplier = () -> normalizationResult.getMessage();
if (!dimensionKeyNormalizationWarnLogged) {
logger.warning(() -> String.format(THROTTLE_INFO_TEMPLATE, messageSupplier.get()));
dimensionKeyNormalizationWarnLogged = true;
} else {
logger.fine(messageSupplier);
}
}
}

Expand All @@ -241,7 +252,13 @@ private void dimension(
Normalizer.normalizeDimensionValue(
value, MetricLineConstants.Limits.MAX_DIMENSION_VALUE_LENGTH);
if (normalizationResult.messageType() != NormalizationResult.MessageType.NONE) {
logger.warning(() -> normalizationResult.getMessage());
Supplier<String> messageSupplier = () -> normalizationResult.getMessage();
if (!dimensionValueNormalizationWarnLogged) {
logger.warning(() -> String.format(THROTTLE_INFO_TEMPLATE, messageSupplier.get()));
dimensionValueNormalizationWarnLogged = true;
} else {
logger.fine(messageSupplier);
}
}

tryAddDimensionTo(normalizedKey, normalizationResult.getResult(), targetDimensions);
Expand Down

0 comments on commit 5476be7

Please sign in to comment.