Skip to content

Commit

Permalink
#1806 make a common metric prefix configurable for all gathered Ditto…
Browse files Browse the repository at this point in the history
… custom metrics

Signed-off-by: Thomas Jäckle <thomas.jaeckle@beyonnex.io>
  • Loading branch information
thjaeckle committed Nov 30, 2023
1 parent 404178e commit 9a28a35
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 24 deletions.
Expand Up @@ -39,6 +39,11 @@ public final class DittoSystemProperties {
*/
public static final String DITTO_LIMITS_POLICY_IMPORTS_LIMIT = "ditto.limits.policy.imports-limit";

/**
* System property name of the property defining a common prefix for all metrics Ditto reports via Prometheus.
*/
public static final String DITTO_METRICS_METRIC_PREFIX = "ditto.metrics.metric-prefix";

private DittoSystemProperties() {
throw new AssertionError();
}
Expand Down
Expand Up @@ -24,6 +24,17 @@
import javax.annotation.Nullable;
import javax.annotation.concurrent.NotThreadSafe;

import org.apache.pekko.Done;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.actor.CoordinatedShutdown;
import org.apache.pekko.actor.Props;
import org.apache.pekko.cluster.Cluster;
import org.apache.pekko.cluster.pubsub.DistributedPubSub;
import org.apache.pekko.http.javadsl.Http;
import org.apache.pekko.http.javadsl.model.Uri;
import org.apache.pekko.management.cluster.bootstrap.ClusterBootstrap;
import org.apache.pekko.management.javadsl.PekkoManagement;
import org.eclipse.ditto.base.model.common.DittoSystemProperties;
import org.eclipse.ditto.base.model.signals.FeatureToggle;
import org.eclipse.ditto.base.service.config.ServiceSpecificConfig;
Expand All @@ -35,6 +46,7 @@
import org.eclipse.ditto.internal.utils.config.ScopedConfig;
import org.eclipse.ditto.internal.utils.config.raw.RawConfigSupplier;
import org.eclipse.ditto.internal.utils.health.status.StatusSupplierActor;
import org.eclipse.ditto.internal.utils.metrics.config.MetricsConfig;
import org.eclipse.ditto.internal.utils.metrics.prometheus.PrometheusReporterRoute;
import org.eclipse.ditto.internal.utils.tracing.DittoTracing;
import org.slf4j.Logger;
Expand All @@ -47,17 +59,6 @@
import com.typesafe.config.ConfigValue;
import com.typesafe.config.ConfigValueFactory;

import org.apache.pekko.Done;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.actor.CoordinatedShutdown;
import org.apache.pekko.actor.Props;
import org.apache.pekko.cluster.Cluster;
import org.apache.pekko.cluster.pubsub.DistributedPubSub;
import org.apache.pekko.http.javadsl.Http;
import org.apache.pekko.http.javadsl.model.Uri;
import org.apache.pekko.management.cluster.bootstrap.ClusterBootstrap;
import org.apache.pekko.management.javadsl.PekkoManagement;
import ch.qos.logback.classic.LoggerContext;
import kamon.Kamon;
import kamon.prometheus.PrometheusReporter;
Expand Down Expand Up @@ -121,10 +122,6 @@ protected DittoService(final Logger logger, final String serviceName, final Stri
this.rootActorName = argumentNotEmpty(rootActorName, "root actor name");
rawConfig = determineRawConfig();
serviceSpecificConfig = getServiceSpecificConfig(tryToGetDittoConfigOrEmpty(rawConfig));
if (null == serviceSpecificConfig) {
throw new DittoConfigError("The service specific config must not be null!");
}

logger.debug("Using service specific config: <{}>.", serviceSpecificConfig);
}

Expand Down Expand Up @@ -208,6 +205,7 @@ private static ScopedConfig getDittoConfigOrEmpty(final Config rawConfig) {
private ActorSystem doStart() {
logRuntimeParameters();
final var actorSystemConfig = appendDittoInfo(appendPekkoPersistenceMongoUriToRawConfig());
injectSystemPropertiesLimits(serviceSpecificConfig);
startKamon();
final var actorSystem = createActorSystem(actorSystemConfig);
initializeActorSystem(actorSystem);
Expand Down Expand Up @@ -390,8 +388,6 @@ private void startServiceRootActors(final ActorSystem actorSystem, final C servi

final ActorRef pubSubMediator = getDistributedPubSubMediatorActor(actorSystem);

injectSystemPropertiesLimits(serviceSpecificConfig);

startMainRootActor(actorSystem, getMainRootActorProps(serviceSpecificConfig, pubSubMediator));
RootActorStarter.get(actorSystem, ScopedConfig.dittoExtension(actorSystem.settings().config())).execute();
});
Expand All @@ -418,6 +414,8 @@ private void injectSystemPropertiesLimits(final C serviceSpecificConfig) {
Boolean.toString(rawConfig.getBoolean(FeatureToggle.MERGE_THINGS_ENABLED)));
System.setProperty(DittoSystemProperties.DITTO_LIMITS_POLICY_IMPORTS_LIMIT,
Integer.toString(limitsConfig.getPolicyImportsLimit()));
final MetricsConfig metricsConfig = serviceSpecificConfig.getMetricsConfig();
System.setProperty(DittoSystemProperties.DITTO_METRICS_METRIC_PREFIX, metricsConfig.getMetricPrefix());
}

private static ActorRef getDistributedPubSubMediatorActor(final ActorSystem actorSystem) {
Expand Down
Expand Up @@ -123,7 +123,7 @@ public int hashCode() {
@Override
public String toString() {
return getClass().getSimpleName() + " [" +
", thingsMaxSize=" + thingsMaxSize +
"thingsMaxSize=" + thingsMaxSize +
", policiesMaxSize=" + policiesMaxSize +
", messagesMaxSize=" + messagesMaxSize +
", thingsSearchDefaultPageSize=" + thingsSearchDefaultPageSize +
Expand Down
4 changes: 4 additions & 0 deletions internal/utils/config/src/main/resources/ditto-metrics.conf
Expand Up @@ -2,6 +2,10 @@ ditto.metrics {
systemMetrics.enabled = true
systemMetrics.enabled = ${?SYSTEM_METRICS_ENABLED}

# the metric prefix to apply for all gathered metrics in Ditto provided to Prometheus
metric-prefix = ""
metric-prefix = ${?DITTO_METRICS_METRIC_PREFIX}

prometheus {
enabled = true
enabled = ${?PROMETHEUS_ENABLED}
Expand Down
Expand Up @@ -30,12 +30,14 @@ public final class DefaultMetricsConfig implements MetricsConfig {
private static final String CONFIG_PATH = "metrics";

private final boolean systemMetricEnabled;
private final String metricPrefix;
private final boolean prometheusEnabled;
private final String prometheusHostname;
private final int prometheusPort;

private DefaultMetricsConfig(final ConfigWithFallback metricsScopedConfig) {
systemMetricEnabled = metricsScopedConfig.getBoolean(MetricsConfigValue.SYSTEM_METRICS_ENABLED.getConfigPath());
metricPrefix = metricsScopedConfig.getString(MetricsConfigValue.METRIC_PREFIX.getConfigPath());
prometheusEnabled = metricsScopedConfig.getBoolean(MetricsConfigValue.PROMETHEUS_ENABLED.getConfigPath());
prometheusHostname = metricsScopedConfig.getString(MetricsConfigValue.PROMETHEUS_HOSTNAME.getConfigPath());
prometheusPort = metricsScopedConfig.getNonNegativeIntOrThrow(MetricsConfigValue.PROMETHEUS_PORT);
Expand All @@ -58,6 +60,11 @@ public boolean isSystemMetricsEnabled() {
return systemMetricEnabled;
}

@Override
public String getMetricPrefix() {
return metricPrefix;
}

@Override
public boolean isPrometheusEnabled() {
return prometheusEnabled;
Expand All @@ -83,20 +90,22 @@ public boolean equals(@Nullable final Object o) {
}
final DefaultMetricsConfig that = (DefaultMetricsConfig) o;
return systemMetricEnabled == that.systemMetricEnabled &&
Objects.equals(metricPrefix, that.metricPrefix) &&
prometheusEnabled == that.prometheusEnabled &&
prometheusPort == that.prometheusPort &&
Objects.equals(prometheusHostname, that.prometheusHostname);
}

@Override
public int hashCode() {
return Objects.hash(systemMetricEnabled, prometheusEnabled, prometheusHostname, prometheusPort);
return Objects.hash(systemMetricEnabled, metricPrefix, prometheusEnabled, prometheusHostname, prometheusPort);
}

@Override
public String toString() {
return getClass().getSimpleName() + " [" +
"systemMetricEnabled=" + systemMetricEnabled +
", metricPrefix=" + metricPrefix +
", prometheusEnabled=" + prometheusEnabled +
", prometheusHostname=" + prometheusHostname +
", prometheusPort=" + prometheusPort +
Expand Down
Expand Up @@ -29,6 +29,13 @@ public interface MetricsConfig {
*/
boolean isSystemMetricsEnabled();

/**
* Returns the metric prefix to apply for all gathered metrics in Ditto provided to Prometheus.
*
* @return the metric prefix to apply or empty string for no prefix.
*/
String getMetricPrefix();

/**
* Indicates whether Prometheus is enabled.
*
Expand Down Expand Up @@ -61,6 +68,11 @@ enum MetricsConfigValue implements KnownConfigValue {
*/
SYSTEM_METRICS_ENABLED("systemMetrics.enabled", false),

/**
* The metric prefix to apply for all gathered Metrics in Ditto.
*/
METRIC_PREFIX("metric-prefix", ""),

/**
* Determines whether Prometheus is enabled.
*/
Expand All @@ -79,7 +91,7 @@ enum MetricsConfigValue implements KnownConfigValue {
private final String path;
private final Object defaultValue;

private MetricsConfigValue(final String thePath, final Object theDefaultValue) {
MetricsConfigValue(final String thePath, final Object theDefaultValue) {
path = thePath;
defaultValue = theDefaultValue;
}
Expand Down
@@ -0,0 +1,14 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
@org.eclipse.ditto.utils.jsr305.annotations.AllParametersAndReturnValuesAreNonnullByDefault
package org.eclipse.ditto.internal.utils.metrics.config;
Expand Up @@ -17,6 +17,7 @@

import javax.annotation.concurrent.Immutable;

import org.eclipse.ditto.base.model.common.DittoSystemProperties;
import org.eclipse.ditto.internal.utils.metrics.instruments.tag.KamonTagSetConverter;
import org.eclipse.ditto.internal.utils.metrics.instruments.tag.Tag;
import org.eclipse.ditto.internal.utils.metrics.instruments.tag.TagSet;
Expand Down Expand Up @@ -55,7 +56,8 @@ public static KamonCounter newCounter(final String name) {
* @throws IllegalArgumentException if {@code name} is empty.
*/
public static KamonCounter newCounter(final String name, final TagSet tags) {
return new KamonCounter(name, tags);
final String metricPrefix = System.getProperty(DittoSystemProperties.DITTO_METRICS_METRIC_PREFIX, "");
return new KamonCounter(metricPrefix + name, tags);
}

@Override
Expand Down
Expand Up @@ -14,6 +14,7 @@

import javax.annotation.concurrent.Immutable;

import org.eclipse.ditto.base.model.common.DittoSystemProperties;
import org.eclipse.ditto.internal.utils.metrics.instruments.tag.KamonTagSetConverter;
import org.eclipse.ditto.internal.utils.metrics.instruments.tag.Tag;
import org.eclipse.ditto.internal.utils.metrics.instruments.tag.TagSet;
Expand All @@ -39,7 +40,8 @@ private KamonGauge(final String name, final TagSet tags) {
}

public static Gauge newGauge(final String name) {
return new KamonGauge(name, TagSet.empty());
final String metricPrefix = System.getProperty(DittoSystemProperties.DITTO_METRICS_METRIC_PREFIX, "");
return new KamonGauge(metricPrefix + name, TagSet.empty());
}

@Override
Expand Down
Expand Up @@ -19,6 +19,7 @@

import javax.annotation.concurrent.Immutable;

import org.eclipse.ditto.base.model.common.DittoSystemProperties;
import org.eclipse.ditto.internal.utils.metrics.instruments.tag.KamonTagSetConverter;
import org.eclipse.ditto.internal.utils.metrics.instruments.tag.Tag;
import org.eclipse.ditto.internal.utils.metrics.instruments.tag.TagSet;
Expand Down Expand Up @@ -47,7 +48,8 @@ private KamonHistogram(final String name, final TagSet tags) {
}

public static Histogram newHistogram(final String name) {
return new KamonHistogram(name, TagSet.empty());
final String metricPrefix = System.getProperty(DittoSystemProperties.DITTO_METRICS_METRIC_PREFIX, "");
return new KamonHistogram(metricPrefix + name, TagSet.empty());
}

@Override
Expand Down
Expand Up @@ -22,6 +22,7 @@

import javax.annotation.Nullable;

import org.eclipse.ditto.base.model.common.DittoSystemProperties;
import org.eclipse.ditto.internal.utils.metrics.instruments.tag.KamonTagSetConverter;
import org.eclipse.ditto.internal.utils.metrics.instruments.tag.Tag;
import org.eclipse.ditto.internal.utils.metrics.instruments.tag.TagSet;
Expand Down Expand Up @@ -61,7 +62,8 @@ private PreparedKamonTimer(
}

static PreparedTimer newTimer(final String name) {
return new PreparedKamonTimer(name);
final String metricPrefix = System.getProperty(DittoSystemProperties.DITTO_METRICS_METRIC_PREFIX, "");
return new PreparedKamonTimer(metricPrefix + name);
}

private static void defaultExpirationHandling(
Expand Down

0 comments on commit 9a28a35

Please sign in to comment.