2 * Copyright (c) 2017 Red Hat, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.infrautils.metrics.internal;
10 import com.google.common.base.MoreObjects;
12 import java.util.function.Consumer;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
17 * Configuration properties for the metrics implementation.
19 * <p>Karaf's OSGi ConfigAdmin service, via the cm blueprint extension, sets this
20 * from the etc/org.opendaylight.infrautils.metrics.cfg configuration file.
22 * @author Michael Vorburger.ch
24 public final class Configuration {
26 private static final Logger LOG = LoggerFactory.getLogger(Configuration.class);
28 private final MetricProviderImpl metricProvider;
30 // Apply any change to these defaults also to org.opendaylight.infrautils.metrics.cfg
31 // (Just for clarity; they are commented out there, so these are the real defaults.)
32 private int threadsWatcherIntervalMS = 500;
33 private int maxThreads = 1000;
34 private int fileReporterIntervalSecs = 0;
35 private int maxThreadsMaxLogIntervalSecs = 60;
36 private int deadlockedThreadsMaxLogIntervalSecs = 60;
38 public Configuration(MetricProviderImpl metricProvider, Map<String, String> initialProperties) {
40 updateProperties(initialProperties);
43 public Configuration(MetricProviderImpl metricProvider) {
44 this.metricProvider = metricProvider;
47 public void updateProperties(Map<String, String> properties) {
48 LOG.info("updateProperties({})", properties);
49 doIfIntPropertyIsPresent(properties, "threadsWatcherIntervalMS", this::setThreadsWatcherIntervalMS);
50 doIfIntPropertyIsPresent(properties, "maxThreads", this::setMaxThreads);
51 doIfIntPropertyIsPresent(properties, "fileReporterIntervalSecs", this::setFileReporterIntervalSecs);
52 doIfIntPropertyIsPresent(properties, "maxThreadsMaxLogIntervalSecs", this::setMaxThreadsMaxLogIntervalSecs);
53 doIfIntPropertyIsPresent(properties, "deadlockedThreadsMaxLogIntervalSecs",
54 this::setDeadlockedThreadsMaxLogIntervalSecs);
56 metricProvider.updateConfiguration(this);
59 public void setFileReporterIntervalSecs(int fileReporterIntervalSecs) {
60 this.fileReporterIntervalSecs = fileReporterIntervalSecs;
63 public int getFileReporterIntervalSecs() {
64 return fileReporterIntervalSecs;
67 public void setThreadsWatcherIntervalMS(int ms) {
68 this.threadsWatcherIntervalMS = ms;
71 public int getThreadsWatcherIntervalMS() {
72 return this.threadsWatcherIntervalMS;
75 public void setMaxThreads(int maxThreads) {
76 this.maxThreads = maxThreads;
79 public int getMaxThreads() {
80 return this.maxThreads;
83 public void setMaxThreadsMaxLogIntervalSecs(int maxThreadsMaxLogIntervalSecs) {
84 this.maxThreadsMaxLogIntervalSecs = maxThreadsMaxLogIntervalSecs;
87 public int getMaxThreadsMaxLogIntervalSecs() {
88 return maxThreadsMaxLogIntervalSecs;
91 public void setDeadlockedThreadsMaxLogIntervalSecs(int deadlockedThreadsMaxLogIntervalSecs) {
92 this.deadlockedThreadsMaxLogIntervalSecs = deadlockedThreadsMaxLogIntervalSecs;
95 public int getDeadlockedThreadsMaxLogIntervalSecs() {
96 return deadlockedThreadsMaxLogIntervalSecs;
100 public String toString() {
101 return MoreObjects.toStringHelper(this)
102 .add("threadsWatcherIntervalMS", threadsWatcherIntervalMS)
103 .add("maxThreads", maxThreads)
104 .add("maxThreadsMaxLogIntervalSecs", maxThreadsMaxLogIntervalSecs)
105 .add("deadlockedThreadsMaxLogIntervalSecs", deadlockedThreadsMaxLogIntervalSecs)
106 .add("fileReporterIntervalSecs", fileReporterIntervalSecs)
110 // When any other project want to deal with Configuration like this, perhaps this could be moved somewhere re-usable
111 private static void doIfIntPropertyIsPresent(
112 Map<String, String> properties, String propertyName, Consumer<Integer> consumer) {
113 String propertyValueAsString = properties.get(propertyName);
114 if (propertyValueAsString != null) {
116 Integer propertyValueAsInt = Integer.parseInt(propertyValueAsString);
117 consumer.accept(propertyValueAsInt);
118 } catch (NumberFormatException nfe) {
119 LOG.warn("Ignored property '{}' that was expected to be an Integer but was not: {}", propertyName,
120 propertyValueAsString, nfe);