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.sample;
10 import static java.util.concurrent.TimeUnit.MILLISECONDS;
11 import static org.opendaylight.infrautils.utils.concurrent.JdkFutures.addErrorLogging;
13 import java.util.Random;
14 import java.util.concurrent.ScheduledExecutorService;
15 import javax.annotation.PostConstruct;
16 import javax.annotation.PreDestroy;
17 import javax.inject.Inject;
18 import javax.inject.Singleton;
19 import org.opendaylight.infrautils.metrics.Labeled;
20 import org.opendaylight.infrautils.metrics.Meter;
21 import org.opendaylight.infrautils.metrics.MetricDescriptor;
22 import org.opendaylight.infrautils.metrics.MetricProvider;
23 import org.opendaylight.infrautils.utils.concurrent.Executors;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Example illustrating usage of metrics API, and demo.
30 * @author Michael Vorburger.ch
33 public class MetricsExample implements Runnable {
35 private static final Logger LOG = LoggerFactory.getLogger(MetricsExample.class);
37 private final Meter meterWithoutLabel;
38 private final Meter meterWithOneFixedLabel;
39 private final Meter meterWithTwoFixedLabels;
40 private final Labeled<Meter> meterWithOneDynamicLabel;
41 private final Labeled<Labeled<Meter>> meterWithTwoDynamicLabels;
43 private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor("cron", LOG);
44 private final Random random = new Random();
47 public MetricsExample(MetricProvider metricProvider) {
48 meterWithoutLabel = metricProvider.newMeter(MetricDescriptor.builder().anchor(this)
49 .project("infrautils").module("metrics").id("example_meter_without_labels")
50 .description("Example meter metric without any labels").build());
52 meterWithOneFixedLabel = metricProvider.newMeter(MetricDescriptor.builder().anchor(this)
53 .project("infrautils").module("metrics").id("example_meter_1_label")
54 .description("Example meter metric with 1 label and a fixed label value").build(),
57 meterWithTwoFixedLabels = metricProvider.newMeter(MetricDescriptor.builder().anchor(this)
58 .project("infrautils").module("metrics").id("example_meter_2_labels")
59 .description("Example meter metric with 2 labels and fixed label values").build(),
60 "port", "mac").label("123").label("6C:0D:E6:67:7E:68");
62 meterWithOneDynamicLabel = metricProvider.newMeter(MetricDescriptor.builder().anchor(this)
63 .project("infrautils").module("metrics").id("example_meter_1_dynlabel")
64 .description("Example meter metric with 1 label and label value set in using code").build(),
67 meterWithTwoDynamicLabels = metricProvider.newMeter(MetricDescriptor.builder().anchor(this)
68 .project("infrautils").module("metrics").id("example_meter_2_dynlabels")
69 .description("Example meter metric with 2 labels and its label values set in using code").build(),
75 addErrorLogging(executor.scheduleWithFixedDelay(this, 0, 500, MILLISECONDS), LOG, "schedule interrupted");
80 meterWithoutLabel.close();
81 meterWithOneFixedLabel.close();
82 meterWithTwoFixedLabels.close();
83 // TODO meterWithOneDynamicLabel.close() how to?
84 // TODO meterWithTwoDynamicLabels.close() how to?
86 executor.shutdownNow();
91 meterWithoutLabel.mark(random.nextInt(100));
92 meterWithOneFixedLabel.mark(random.nextInt(100));
93 meterWithTwoFixedLabels.mark(random.nextInt(100));
95 meterWithOneDynamicLabel.label(/* port */ "456").mark(random.nextInt(100));
96 meterWithTwoDynamicLabels
97 .label(/* port */ "456").label(/* MAC */ "1A:0B:F2:25:1C:68")
98 .mark(random.nextInt(100));
100 // see the MetricsAdvancedExample for how to do meter.port(456).mac("1A:0B:F2:25:1C:68").mark();