Metrics implementation based on http://Prometheus.io
[serviceutils.git] / metrics / sample / impl / src / main / java / org / opendaylight / infrautils / metrics / sample / MetricsExample.java
1 /*
2  * Copyright (c) 2017 Red Hat, Inc. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.infrautils.metrics.sample;
9
10 import static java.util.concurrent.TimeUnit.MILLISECONDS;
11 import static org.opendaylight.infrautils.utils.concurrent.JdkFutures.addErrorLogging;
12
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;
26
27 /**
28  * Example illustrating usage of metrics API, and demo.
29  *
30  * @author Michael Vorburger.ch
31  */
32 @Singleton
33 public class MetricsExample implements Runnable {
34
35     private static final Logger LOG = LoggerFactory.getLogger(MetricsExample.class);
36
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;
42
43     private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor("cron", LOG);
44     private final Random random = new Random();
45
46     @Inject
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());
51
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(),
55                 "port").label("123");
56
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");
61
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(),
65                 "port");
66
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(),
70                 "port", "mac");
71     }
72
73     @PostConstruct
74     public void init() {
75         addErrorLogging(executor.scheduleWithFixedDelay(this, 0, 500, MILLISECONDS), LOG, "schedule interrupted");
76     }
77
78     @PreDestroy
79     public void close() {
80         meterWithoutLabel.close();
81         meterWithOneFixedLabel.close();
82         meterWithTwoFixedLabels.close();
83         // TODO meterWithOneDynamicLabel.close() how to?
84         // TODO meterWithTwoDynamicLabels.close() how to?
85
86         executor.shutdownNow();
87     }
88
89     @Override
90     public void run() {
91         meterWithoutLabel.mark(random.nextInt(100));
92         meterWithOneFixedLabel.mark(random.nextInt(100));
93         meterWithTwoFixedLabels.mark(random.nextInt(100));
94
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));
99
100         // see the MetricsAdvancedExample for how to do meter.port(456).mac("1A:0B:F2:25:1C:68").mark();
101     }
102
103 }