summaryrefslogtreecommitdiffstats
path: root/src/jaegertracing/opentelemetry-cpp/api/include/opentelemetry/_metrics/instrument.h
blob: d0cffac4b143036118ce3748a343118c049a7396 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once
#ifdef ENABLE_METRICS_PREVIEW

#  include <iostream>
#  include "opentelemetry/common/attribute_value.h"
#  include "opentelemetry/common/key_value_iterable_view.h"
#  include "opentelemetry/nostd/shared_ptr.h"
#  include "opentelemetry/nostd/string_view.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace metrics
{

// Enum classes to help determine instrument types in other parts of the API
enum class InstrumentKind
{
  Counter           = 0,
  UpDownCounter     = 1,
  ValueRecorder     = 2,
  ValueObserver     = 3,
  SumObserver       = 4,
  UpDownSumObserver = 5,
};

class Instrument
{

public:
  // Note that Instruments should be created using the Meter class.
  // Please refer to meter.h for documentation.
  Instrument() = default;

  /**
   * Base class constructor for all other instrument types.  Whether or not
   * an instrument is synchronous or bound, it requires a name, description,
   * unit, and enabled flag.
   *
   * @param name is the identifier of the instrumenting library
   * @param description explains what the metric captures
   * @param unit specifies the data type held in the instrument
   * @param enabled determines if the metric is currently capturing data
   * @return Instrument type with the specified attributes
   */
  Instrument(nostd::string_view name,
             nostd::string_view description,
             nostd::string_view unit,
             bool enabled)
  {}

  // Returns true if the instrument is enabled and collecting data
  virtual bool IsEnabled() = 0;

  // Return the instrument name
  virtual nostd::string_view GetName() = 0;

  // Return the instrument description
  virtual nostd::string_view GetDescription() = 0;

  // Return the insrument's units of measurement
  virtual nostd::string_view GetUnits() = 0;

  // Return the intrument's kind
  virtual InstrumentKind GetKind() = 0;

  virtual ~Instrument() = default;
};

template <class T>
class BoundSynchronousInstrument : virtual public Instrument
{

public:
  BoundSynchronousInstrument() = default;

  BoundSynchronousInstrument(nostd::string_view name,
                             nostd::string_view description,
                             nostd::string_view unit,
                             bool enabled);

  /**
   * Frees the resources associated with this Bound Instrument.
   * The Metric from which this instrument was created is not impacted.
   *
   * @param none
   * @return void
   */
  virtual void unbind() {}

  /**
   * Incremements the reference count of this bound object when a new instance is
   * either created or the same instance is returned as a result of binding
   *
   * @param none
   * @return void
   */
  virtual void inc_ref() {}

  /**
   * Return the object's current reference count.  This information is used to remove
   * stale objects from instrument registries.
   */
  virtual int get_ref() { return 0; }

  /**
   * Records a single synchronous metric event; a call to the aggregator
   * Since this is a bound synchronous instrument, labels are not required in  * metric capture
   * calls.
   *
   * @param value is the numerical representation of the metric being captured
   * @return void
   */
  virtual void update(T value) {}
};

template <class T>
class SynchronousInstrument : virtual public Instrument
{

public:
  SynchronousInstrument() = default;

  SynchronousInstrument(nostd::string_view name,
                        nostd::string_view description,
                        nostd::string_view unit,
                        bool enabled)
  {}

  /**
   * Returns a Bound Instrument associated with the specified labels.         * Multiples requests
   * with the same set of labels may return the same Bound Instrument instance.
   *
   * It is recommended that callers keep a reference to the Bound Instrument
   * instead of repeatedly calling this operation.
   *
   * @param labels the set of labels, as key-value pairs
   * @return a Bound Instrument
   */
  virtual nostd::shared_ptr<BoundSynchronousInstrument<T>> bind(
      const common::KeyValueIterable &labels)
  {
    return nostd::shared_ptr<BoundSynchronousInstrument<T>>();
  }

  /**
   * Records a single synchronous metric event.
   * Since this is an unbound synchronous instrument, labels are required in  * metric capture
   * calls.
   *
   * update can be used in instruments with both add or record since it simply
   * activated the aggregator
   *
   * @param labels the set of labels, as key-value pairs
   * @param value is the numerical representation of the metric being captured
   * @return void
   */
  virtual void update(T value, const common::KeyValueIterable &labels) = 0;
};

template <class T>
class ObserverResult;

template <class T>
class AsynchronousInstrument : virtual public Instrument
{

public:
  AsynchronousInstrument() = default;

  AsynchronousInstrument(nostd::string_view name,
                         nostd::string_view description,
                         nostd::string_view unit,
                         bool enabled,
                         void(callback)(ObserverResult<T>))
  {}

  /**
   * Captures data through a manual call rather than the automatic collection process instituted
   * in the run function.  Asynchronous instruments are generally expected to obtain data from
   * their callbacks rather than direct calls.  This function is used by the callback to store data.
   *
   * @param value is the numerical representation of the metric being captured
   * @param labels is the numerical representation of the metric being captured
   * @return none
   */
  virtual void observe(T value, const common::KeyValueIterable &labels) = 0;

  /**
   * Captures data by activating the callback function associated with the
   * instrument and storing its return value.  Callbacks for asynchronous
   * instruments are defined during construction.
   *
   * @param none
   * @return none
   */
  virtual void run() = 0;

protected:
  // Callback function which takes a pointer to an Asynchronous instrument (this) type which is
  // stored in an observer result type and returns nothing.  This function calls the instrument's
  // observe.
  void (*callback_)(ObserverResult<T>);
};

}  // namespace metrics
OPENTELEMETRY_END_NAMESPACE
#endif