opentelemetry.metrics package¶
Module contents¶
The OpenTelemetry metrics API describes the classes used to report raw measurements, as well as metrics with known aggregation and labels.
The Meter
class is used to construct Metric
s to record raw statistics
as well as metrics with predefined aggregation.
Meter
s can be obtained via the MeterProvider
, corresponding to the name
of the instrumenting library and (optionally) a version.
See the metrics api spec for terminology and context clarification.
-
class
opentelemetry.metrics.
DefaultBoundCounter
[source]¶ Bases:
opentelemetry.metrics.BoundCounter
The default bound counter instrument.
Used when no bound counter implementation is available.
-
class
opentelemetry.metrics.
DefaultBoundUpDownCounter
[source]¶ Bases:
opentelemetry.metrics.BoundUpDownCounter
The default bound updowncounter instrument.
Used when no bound updowncounter implementation is available.
-
class
opentelemetry.metrics.
DefaultBoundValueRecorder
[source]¶ Bases:
opentelemetry.metrics.BoundValueRecorder
The default bound valuerecorder instrument.
Used when no bound valuerecorder implementation is available.
-
class
opentelemetry.metrics.
Metric
[source]¶ Bases:
abc.ABC
Base class for various types of metrics.
Metric class that inherit from this class are specialized with the type of bound metric instrument that the metric holds.
-
class
opentelemetry.metrics.
Counter
[source]¶ Bases:
opentelemetry.metrics.Metric
A counter type metric that expresses the computation of a sum.
-
abstract
add
(value, labels)[source]¶ Increases the value of the counter by
value
.- Parameters
value (~ValueT) – The value to add to the counter metric. Should be positive or zero. For a Counter that can decrease, use
UpDownCounter
.labels (
Dict
[str
,str
]) – Labels to associate with the bound instrument.
- Return type
None
-
abstract
-
class
opentelemetry.metrics.
DefaultCounter
[source]¶ Bases:
opentelemetry.metrics.Counter
The default counter instrument.
Used when no
Counter
implementation is available.-
bind
(labels)[source]¶ Gets a bound metric instrument.
Bound metric instruments are useful to reduce the cost of repeatedly recording a metric with a pre-defined set of label values.
-
add
(value, labels)[source]¶ Increases the value of the counter by
value
.- Parameters
value (~ValueT) – The value to add to the counter metric. Should be positive or zero. For a Counter that can decrease, use
UpDownCounter
.labels (
Dict
[str
,str
]) – Labels to associate with the bound instrument.
- Return type
None
-
-
class
opentelemetry.metrics.
UpDownCounter
[source]¶ Bases:
opentelemetry.metrics.Metric
A counter type metric that expresses the computation of a sum, allowing negative increments.
-
class
opentelemetry.metrics.
DefaultUpDownCounter
[source]¶ Bases:
opentelemetry.metrics.UpDownCounter
The default updowncounter instrument.
Used when no
UpDownCounter
implementation is available.-
bind
(labels)[source]¶ Gets a bound metric instrument.
Bound metric instruments are useful to reduce the cost of repeatedly recording a metric with a pre-defined set of label values.
-
-
class
opentelemetry.metrics.
ValueRecorder
[source]¶ Bases:
opentelemetry.metrics.Metric
A valuerecorder type metric that represent raw stats.
-
class
opentelemetry.metrics.
DefaultValueRecorder
[source]¶ Bases:
opentelemetry.metrics.ValueRecorder
The default valuerecorder instrument.
Used when no
ValueRecorder
implementation is available.
-
class
opentelemetry.metrics.
Observer
[source]¶ Bases:
abc.ABC
An observer type metric instrument used to capture a current set of values.
Observer instruments are asynchronous, a callback is invoked with the observer instrument as argument allowing the user to capture multiple values per collection interval.
-
class
opentelemetry.metrics.
SumObserver
[source]¶ Bases:
opentelemetry.metrics.Observer
Asynchronous instrument used to capture a monotonic sum.
-
class
opentelemetry.metrics.
DefaultSumObserver
[source]¶ Bases:
opentelemetry.metrics.SumObserver
No-op implementation of
SumObserver
.
-
class
opentelemetry.metrics.
UpDownSumObserver
[source]¶ Bases:
opentelemetry.metrics.Observer
Asynchronous instrument used to capture a non-monotonic count.
-
class
opentelemetry.metrics.
DefaultUpDownSumObserver
[source]¶ Bases:
opentelemetry.metrics.UpDownSumObserver
No-op implementation of
UpDownSumObserver
.
-
class
opentelemetry.metrics.
ValueObserver
[source]¶ Bases:
opentelemetry.metrics.Observer
Asynchronous instrument used to capture grouping measurements.
-
class
opentelemetry.metrics.
DefaultValueObserver
[source]¶ Bases:
opentelemetry.metrics.ValueObserver
No-op implementation of
ValueObserver
.
-
class
opentelemetry.metrics.
MeterProvider
[source]¶ Bases:
abc.ABC
-
abstract
get_meter
(instrumenting_module_name, instrumenting_library_version='')[source]¶ Returns a
Meter
for use by the given instrumentation library.This function may return different
Meter
types (e.g. a no-op meter vs. a functional meter).- Parameters
instrumenting_module_name (
str
) –The name of the instrumenting module (usually just
__name__
).This should not be the name of the module that is instrumented but the name of the module doing the instrumentation. E.g., instead of
"requests"
, use"opentelemetry.instrumentation.requests"
.instrumenting_library_version (
str
) – Optional. The version string of the instrumenting library. Usually this should be the same aspkg_resources.get_distribution(instrumenting_library_name).version
.
- Return type
-
abstract
-
class
opentelemetry.metrics.
DefaultMeterProvider
[source]¶ Bases:
opentelemetry.metrics.MeterProvider
The default MeterProvider, used when no implementation is available.
All operations are no-op.
-
get_meter
(instrumenting_module_name, instrumenting_library_version='')[source]¶ Returns a
Meter
for use by the given instrumentation library.This function may return different
Meter
types (e.g. a no-op meter vs. a functional meter).- Parameters
instrumenting_module_name (
str
) –The name of the instrumenting module (usually just
__name__
).This should not be the name of the module that is instrumented but the name of the module doing the instrumentation. E.g., instead of
"requests"
, use"opentelemetry.instrumentation.requests"
.instrumenting_library_version (
str
) – Optional. The version string of the instrumenting library. Usually this should be the same aspkg_resources.get_distribution(instrumenting_library_name).version
.
- Return type
-
-
class
opentelemetry.metrics.
Meter
[source]¶ Bases:
abc.ABC
An interface to allow the recording of metrics.
Metric
s or metric instruments, are devices used for capturing raw measurements. Each metric instrument supports a single method, each with fixed interpretation to capture measurements.-
abstract
record_batch
(labels, record_tuples)[source]¶ Atomically records a batch of
Metric
and value pairs.Allows the functionality of acting upon multiple metrics with a single API call. Implementations should find bound metric instruments that match the key-value pairs in the labels.
-
abstract
create_counter
(name, description, unit, value_type, enabled=True)[source]¶ Creates a
Counter
metric with typevalue_type
.- Parameters
name (
str
) – The name of the metric.description (
str
) – Human-readable description of the metric.unit (
str
) – Unit of the metric values following the UCUM convention (https://unitsofmeasure.org/ucum.html).value_type (
Type
[~ValueT]) – The type of values being recorded by the metric.enabled (
bool
) – Whether to report the metric by default.
- Return type
-
abstract
create_updowncounter
(name, description, unit, value_type, enabled=True)[source]¶ Creates a
UpDownCounter
metric with typevalue_type
.- Parameters
name (
str
) – The name of the metric.description (
str
) – Human-readable description of the metric.unit (
str
) – Unit of the metric values following the UCUM convention (https://unitsofmeasure.org/ucum.html).value_type (
Type
[~ValueT]) – The type of values being recorded by the metric.enabled (
bool
) – Whether to report the metric by default.
- Return type
-
abstract
create_valuerecorder
(name, description, unit, value_type, enabled=True)[source]¶ Creates a
ValueRecorder
metric with typevalue_type
.- Parameters
name (
str
) – The name of the metric.description (
str
) – Human-readable description of the metric.unit (
str
) – Unit of the metric values following the UCUM convention (https://unitsofmeasure.org/ucum.html).value_type (
Type
[~ValueT]) – The type of values being recorded by the metric.enabled (
bool
) – Whether to report the metric by default.
- Return type
-
abstract
register_sumobserver
(callback, name, description, unit, value_type, label_keys=(), enabled=True)[source]¶ Registers an
SumObserver
metric instrument.- Parameters
callback (
Callable
[[Observer
],None
]) – Callback invoked each collection interval with the observer as argument.name (
str
) – The name of the metric.description (
str
) – Human-readable description of the metric.unit (
str
) – Unit of the metric values following the UCUM convention (https://unitsofmeasure.org/ucum.html).value_type (
Type
[~ValueT]) – The type of values being recorded by the metric.label_keys (
Sequence
[str
]) – The keys for the labels with dynamic values.enabled (
bool
) – Whether to report the metric by default.
Returns: A new
SumObserver
metric instrument.- Return type
-
abstract
register_updownsumobserver
(callback, name, description, unit, value_type, label_keys=(), enabled=True)[source]¶ Registers an
UpDownSumObserver
metric instrument.- Parameters
callback (
Callable
[[Observer
],None
]) – Callback invoked each collection interval with the observer as argument.name (
str
) – The name of the metric.description (
str
) – Human-readable description of the metric.unit (
str
) – Unit of the metric values following the UCUM convention (https://unitsofmeasure.org/ucum.html).value_type (
Type
[~ValueT]) – The type of values being recorded by the metric.label_keys (
Sequence
[str
]) – The keys for the labels with dynamic values.enabled (
bool
) – Whether to report the metric by default.
Returns: A new
UpDownSumObserver
metric instrument.- Return type
-
abstract
register_valueobserver
(callback, name, description, unit, value_type, label_keys=(), enabled=True)[source]¶ Registers an
ValueObserver
metric instrument.- Parameters
callback (
Callable
[[Observer
],None
]) – Callback invoked each collection interval with the observer as argument.name (
str
) – The name of the metric.description (
str
) – Human-readable description of the metric.unit (
str
) – Unit of the metric values following the UCUM convention (https://unitsofmeasure.org/ucum.html).value_type (
Type
[~ValueT]) – The type of values being recorded by the metric.label_keys (
Sequence
[str
]) – The keys for the labels with dynamic values.enabled (
bool
) – Whether to report the metric by default.
Returns: A new
ValueObserver
metric instrument.- Return type
-
abstract
-
class
opentelemetry.metrics.
DefaultMeter
[source]¶ Bases:
opentelemetry.metrics.Meter
The default Meter used when no Meter implementation is available.
-
record_batch
(labels, record_tuples)[source]¶ Atomically records a batch of
Metric
and value pairs.Allows the functionality of acting upon multiple metrics with a single API call. Implementations should find bound metric instruments that match the key-value pairs in the labels.
-
create_counter
(name, description, unit, value_type, enabled=True)[source]¶ Creates a
Counter
metric with typevalue_type
.- Parameters
name (
str
) – The name of the metric.description (
str
) – Human-readable description of the metric.unit (
str
) – Unit of the metric values following the UCUM convention (https://unitsofmeasure.org/ucum.html).value_type (
Type
[~ValueT]) – The type of values being recorded by the metric.enabled (
bool
) – Whether to report the metric by default.
- Return type
-
create_updowncounter
(name, description, unit, value_type, enabled=True)[source]¶ Creates a
UpDownCounter
metric with typevalue_type
.- Parameters
name (
str
) – The name of the metric.description (
str
) – Human-readable description of the metric.unit (
str
) – Unit of the metric values following the UCUM convention (https://unitsofmeasure.org/ucum.html).value_type (
Type
[~ValueT]) – The type of values being recorded by the metric.enabled (
bool
) – Whether to report the metric by default.
- Return type
-
create_valuerecorder
(name, description, unit, value_type, enabled=True)[source]¶ Creates a
ValueRecorder
metric with typevalue_type
.- Parameters
name (
str
) – The name of the metric.description (
str
) – Human-readable description of the metric.unit (
str
) – Unit of the metric values following the UCUM convention (https://unitsofmeasure.org/ucum.html).value_type (
Type
[~ValueT]) – The type of values being recorded by the metric.enabled (
bool
) – Whether to report the metric by default.
- Return type
-
register_sumobserver
(callback, name, description, unit, value_type, label_keys=(), enabled=True)[source]¶ Registers an
SumObserver
metric instrument.- Parameters
callback (
Callable
[[Observer
],None
]) – Callback invoked each collection interval with the observer as argument.name (
str
) – The name of the metric.description (
str
) – Human-readable description of the metric.unit (
str
) – Unit of the metric values following the UCUM convention (https://unitsofmeasure.org/ucum.html).value_type (
Type
[~ValueT]) – The type of values being recorded by the metric.label_keys (
Sequence
[str
]) – The keys for the labels with dynamic values.enabled (
bool
) – Whether to report the metric by default.
Returns: A new
SumObserver
metric instrument.- Return type
-
register_updownsumobserver
(callback, name, description, unit, value_type, label_keys=(), enabled=True)[source]¶ Registers an
UpDownSumObserver
metric instrument.- Parameters
callback (
Callable
[[Observer
],None
]) – Callback invoked each collection interval with the observer as argument.name (
str
) – The name of the metric.description (
str
) – Human-readable description of the metric.unit (
str
) – Unit of the metric values following the UCUM convention (https://unitsofmeasure.org/ucum.html).value_type (
Type
[~ValueT]) – The type of values being recorded by the metric.label_keys (
Sequence
[str
]) – The keys for the labels with dynamic values.enabled (
bool
) – Whether to report the metric by default.
Returns: A new
UpDownSumObserver
metric instrument.- Return type
-
register_valueobserver
(callback, name, description, unit, value_type, label_keys=(), enabled=True)[source]¶ Registers an
ValueObserver
metric instrument.- Parameters
callback (
Callable
[[Observer
],None
]) – Callback invoked each collection interval with the observer as argument.name (
str
) – The name of the metric.description (
str
) – Human-readable description of the metric.unit (
str
) – Unit of the metric values following the UCUM convention (https://unitsofmeasure.org/ucum.html).value_type (
Type
[~ValueT]) – The type of values being recorded by the metric.label_keys (
Sequence
[str
]) – The keys for the labels with dynamic values.enabled (
bool
) – Whether to report the metric by default.
Returns: A new
ValueObserver
metric instrument.- Return type
-
-
opentelemetry.metrics.
get_meter
(instrumenting_module_name, instrumenting_library_version='', meter_provider=None)[source]¶ Returns a
Meter
for use by the given instrumentation library. This function is a convenience wrapper for opentelemetry.metrics.get_meter_provider().get_meterIf meter_provider is omitted the current configured one is used.
- Return type
-
opentelemetry.metrics.
set_meter_provider
(meter_provider)[source]¶ Sets the current global
MeterProvider
object.- Return type
None
-
opentelemetry.metrics.
get_meter_provider
()[source]¶ Gets the current global
MeterProvider
object.- Return type