2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-10-29 22:27:22 +02:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
/*
|
2020-10-29 18:23:13 +02:00
|
|
|
Package metric implements the OpenTelemetry metric API.
|
|
|
|
|
|
|
|
This package is currently in a pre-GA phase. Backwards incompatible changes
|
|
|
|
may be introduced in subsequent minor version releases as we work to track the
|
|
|
|
evolving OpenTelemetry specification and user feedback.
|
|
|
|
|
|
|
|
The Accumulator type supports configurable metrics export behavior through a
|
|
|
|
collection of export interfaces that support various export strategies,
|
|
|
|
described below.
|
|
|
|
|
|
|
|
The OpenTelemetry metric API consists of methods for constructing synchronous
|
|
|
|
and asynchronous instruments. There are two constructors per instrument for
|
|
|
|
the two kinds of number (int64, float64).
|
2020-05-16 07:11:12 +02:00
|
|
|
|
|
|
|
Synchronous instruments are managed by a sync.Map containing a *record
|
2021-11-17 17:51:04 +02:00
|
|
|
with the current state for each synchronous instrument. A lock-free
|
2020-05-16 07:11:12 +02:00
|
|
|
algorithm is used to protect against races when adding and removing
|
|
|
|
items from the sync.Map.
|
|
|
|
|
|
|
|
Asynchronous instruments are managed by an internal
|
|
|
|
AsyncInstrumentState, which coordinates calling batch and single
|
|
|
|
instrument callbacks.
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2022-08-25 04:42:28 +02:00
|
|
|
# Internal Structure
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-03-05 22:15:30 +02:00
|
|
|
Each observer also has its own kind of record stored in the SDK. This
|
2022-04-18 16:31:31 +02:00
|
|
|
record contains a set of recorders for every specific attribute set used in
|
|
|
|
the callback.
|
2020-03-05 22:15:30 +02:00
|
|
|
|
2022-04-18 16:31:31 +02:00
|
|
|
A sync.Map maintains the mapping of current instruments and attribute sets to
|
|
|
|
internal records. To find a record, the SDK consults the Map to locate an
|
|
|
|
existing record, otherwise it constructs a new record. The SDK maintains a
|
|
|
|
count of the number of references to each record, ensuring that records are
|
|
|
|
not reclaimed from the Map while they are still active from the user's
|
|
|
|
perspective.
|
2019-11-15 23:01:20 +02:00
|
|
|
|
|
|
|
Metric collection is performed via a single-threaded call to Collect that
|
|
|
|
sweeps through all records in the SDK, checkpointing their state. When a
|
|
|
|
record is discovered that has no references and has not been updated since
|
2020-03-05 22:15:30 +02:00
|
|
|
the prior collection pass, it is removed from the Map.
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-05-16 07:11:12 +02:00
|
|
|
Both synchronous and asynchronous instruments have an associated
|
2020-03-05 22:15:30 +02:00
|
|
|
aggregator, which maintains the current state resulting from all metric
|
|
|
|
events since its last checkpoint. Aggregators may be lock-free or they may
|
|
|
|
use locking, but they should expect to be called concurrently. Aggregators
|
|
|
|
must be capable of merging with another aggregator of the same type.
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2022-08-25 04:42:28 +02:00
|
|
|
# Export Pipeline
|
2019-11-15 23:01:20 +02:00
|
|
|
|
|
|
|
While the SDK serves to maintain a current set of records and
|
|
|
|
coordinate collection, the behavior of a metrics export pipeline is
|
|
|
|
configured through the export types in
|
2021-12-13 22:13:03 +02:00
|
|
|
go.opentelemetry.io/otel/sdk/metric/export. It is important to keep
|
2019-11-15 23:01:20 +02:00
|
|
|
in mind the context these interfaces are called from. There are two
|
|
|
|
contexts, instrumentation context, where a user-level goroutine that
|
|
|
|
enters the SDK resulting in a new record, and collection context,
|
|
|
|
where a system-level thread performs a collection pass through the
|
|
|
|
SDK.
|
|
|
|
|
2020-05-16 07:11:12 +02:00
|
|
|
Descriptor is a struct that describes the metric instrument to the
|
|
|
|
export pipeline, containing the name, units, description, metric kind,
|
|
|
|
number kind (int64 or float64). A Descriptor accompanies metric data
|
|
|
|
as it passes through the export pipeline.
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-06-23 19:51:15 +02:00
|
|
|
The AggregatorSelector interface supports choosing the method of
|
|
|
|
aggregation to apply to a particular instrument, by delegating the
|
|
|
|
construction of an Aggregator to this interface. Given the Descriptor,
|
|
|
|
the AggregatorFor method returns an implementation of Aggregator. If this
|
2020-03-11 01:00:37 +02:00
|
|
|
interface returns nil, the metric will be disabled. The aggregator should
|
|
|
|
be matched to the capabilities of the exporter. Selecting the aggregator
|
2020-06-23 19:51:15 +02:00
|
|
|
for Adding instruments is relatively straightforward, but many options
|
|
|
|
are available for aggregating distributions from Grouping instruments.
|
2019-11-15 23:01:20 +02:00
|
|
|
|
|
|
|
Aggregator is an interface which implements a concrete strategy for
|
|
|
|
aggregating metric updates. Several Aggregator implementations are
|
|
|
|
provided by the SDK. Aggregators may be lock-free or use locking,
|
|
|
|
depending on their structure and semantics. Aggregators implement an
|
|
|
|
Update method, called in instrumentation context, to receive a single
|
|
|
|
metric event. Aggregators implement a Checkpoint method, called in
|
|
|
|
collection context, to save a checkpoint of the current state.
|
|
|
|
Aggregators implement a Merge method, also called in collection
|
|
|
|
context, that combines state from two aggregators into one. Each SDK
|
|
|
|
record has an associated aggregator.
|
|
|
|
|
2020-06-23 21:00:15 +02:00
|
|
|
Processor is an interface which sits between the SDK and an exporter.
|
|
|
|
The Processor embeds an AggregatorSelector, used by the SDK to assign
|
|
|
|
new Aggregators. The Processor supports a Process() API for submitting
|
2021-09-27 17:51:47 +02:00
|
|
|
checkpointed aggregators to the processor, and a Reader() API
|
2019-11-15 23:01:20 +02:00
|
|
|
for producing a complete checkpoint for the exporter. Two default
|
2020-06-23 21:00:15 +02:00
|
|
|
Processor implementations are provided, the "defaultkeys" Processor groups
|
2019-11-15 23:01:20 +02:00
|
|
|
aggregate metrics by their recommended Descriptor.Keys(), the
|
2020-06-23 21:00:15 +02:00
|
|
|
"simple" Processor aggregates metrics at full dimensionality.
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2021-09-27 17:51:47 +02:00
|
|
|
Reader is an interface between the Processor and the Exporter.
|
|
|
|
After completing a collection pass, the Processor.Reader() method
|
|
|
|
returns a Reader, which the Exporter uses to iterate over all
|
2019-11-15 23:01:20 +02:00
|
|
|
the updated metrics.
|
|
|
|
|
|
|
|
Record is a struct containing the state of an individual exported
|
|
|
|
metric. This is the result of one collection interface for one
|
2022-04-18 16:31:31 +02:00
|
|
|
instrument and one attribute set.
|
2019-11-15 23:01:20 +02:00
|
|
|
|
|
|
|
Exporter is the final stage of an export pipeline. It is called with
|
2021-09-27 17:51:47 +02:00
|
|
|
a Reader capable of enumerating all the updated metrics.
|
2019-11-15 23:01:20 +02:00
|
|
|
|
|
|
|
Controller is not an export interface per se, but it orchestrates the
|
|
|
|
export pipeline. For example, a "push" controller will establish a
|
|
|
|
periodic timer to regularly collect and export metrics. A "pull"
|
|
|
|
controller will await a pull request before initiating metric
|
|
|
|
collection. Either way, the job of the controller is to call the SDK
|
|
|
|
Collect() method, then read the checkpoint, then invoke the exporter.
|
|
|
|
Controllers are expected to implement the public metric.MeterProvider
|
|
|
|
API, meaning they can be installed as the global Meter provider.
|
2019-10-29 22:27:22 +02:00
|
|
|
*/
|
2019-11-15 23:01:20 +02:00
|
|
|
package metric // import "go.opentelemetry.io/otel/sdk/metric"
|