2020-05-20 10:27:26 -07:00
|
|
|
// Copyright The OpenTelemetry Authors
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package pull // import "go.opentelemetry.io/otel/sdk/metric/controller/pull"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2020-11-12 16:28:32 +01:00
|
|
|
"go.opentelemetry.io/otel/metric"
|
2020-11-11 16:24:12 +01:00
|
|
|
"go.opentelemetry.io/otel/metric/registry"
|
2020-05-20 10:27:26 -07:00
|
|
|
export "go.opentelemetry.io/otel/sdk/export/metric"
|
|
|
|
sdk "go.opentelemetry.io/otel/sdk/metric"
|
|
|
|
controllerTime "go.opentelemetry.io/otel/sdk/metric/controller/time"
|
|
|
|
"go.opentelemetry.io/otel/sdk/resource"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DefaultCachePeriod determines how long a recently-computed result
|
|
|
|
// will be returned without gathering metric data again.
|
|
|
|
const DefaultCachePeriod time.Duration = 10 * time.Second
|
|
|
|
|
2020-09-23 15:16:13 -07:00
|
|
|
// Controller manages access to a *sdk.Accumulator and *basic.Processor. Use
|
|
|
|
// MeterProvider() for obtaining Meters. Use Foreach() for accessing current
|
|
|
|
// records.
|
2020-05-20 10:27:26 -07:00
|
|
|
type Controller struct {
|
2020-08-13 13:12:32 -07:00
|
|
|
accumulator *sdk.Accumulator
|
|
|
|
checkpointer export.Checkpointer
|
2020-09-23 15:16:13 -07:00
|
|
|
provider *registry.MeterProvider
|
2020-08-13 13:12:32 -07:00
|
|
|
period time.Duration
|
|
|
|
lastCollect time.Time
|
|
|
|
clock controllerTime.Clock
|
|
|
|
checkpoint export.CheckpointSet
|
2020-05-20 10:27:26 -07:00
|
|
|
}
|
|
|
|
|
2020-08-13 13:12:32 -07:00
|
|
|
// New returns a *Controller configured with an export.Checkpointer.
|
|
|
|
//
|
|
|
|
// Pull controllers are typically used in an environment where there
|
|
|
|
// are multiple readers. It is common, therefore, when configuring a
|
|
|
|
// basic Processor for use with this controller, to use a
|
|
|
|
// CumulativeExport strategy and the basic.WithMemory(true) option,
|
|
|
|
// which ensures that every CheckpointSet includes full state.
|
|
|
|
func New(checkpointer export.Checkpointer, options ...Option) *Controller {
|
2020-05-20 10:27:26 -07:00
|
|
|
config := &Config{
|
2020-06-02 11:30:09 -07:00
|
|
|
Resource: resource.Empty(),
|
|
|
|
CachePeriod: DefaultCachePeriod,
|
2020-05-20 10:27:26 -07:00
|
|
|
}
|
|
|
|
for _, opt := range options {
|
|
|
|
opt.Apply(config)
|
|
|
|
}
|
|
|
|
accum := sdk.NewAccumulator(
|
2020-08-13 13:12:32 -07:00
|
|
|
checkpointer,
|
2020-10-31 11:16:55 -07:00
|
|
|
config.Resource,
|
2020-05-20 10:27:26 -07:00
|
|
|
)
|
|
|
|
return &Controller{
|
2020-08-13 13:12:32 -07:00
|
|
|
accumulator: accum,
|
|
|
|
checkpointer: checkpointer,
|
2020-09-23 15:16:13 -07:00
|
|
|
provider: registry.NewMeterProvider(accum),
|
2020-08-13 13:12:32 -07:00
|
|
|
period: config.CachePeriod,
|
|
|
|
checkpoint: checkpointer.CheckpointSet(),
|
|
|
|
clock: controllerTime.RealClock{},
|
2020-05-20 10:27:26 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetClock sets the clock used for caching. For testing purposes.
|
|
|
|
func (c *Controller) SetClock(clock controllerTime.Clock) {
|
2020-08-13 13:12:32 -07:00
|
|
|
c.checkpointer.CheckpointSet().Lock()
|
|
|
|
defer c.checkpointer.CheckpointSet().Unlock()
|
2020-05-20 10:27:26 -07:00
|
|
|
c.clock = clock
|
|
|
|
}
|
|
|
|
|
2020-09-23 15:16:13 -07:00
|
|
|
// MeterProvider returns a MeterProvider for the implementation managed by
|
|
|
|
// this controller.
|
2020-11-12 16:28:32 +01:00
|
|
|
func (c *Controller) MeterProvider() metric.MeterProvider {
|
2020-05-20 10:27:26 -07:00
|
|
|
return c.provider
|
|
|
|
}
|
|
|
|
|
|
|
|
// Foreach gives the caller read-locked access to the current
|
|
|
|
// export.CheckpointSet.
|
2020-06-22 22:59:51 -07:00
|
|
|
func (c *Controller) ForEach(ks export.ExportKindSelector, f func(export.Record) error) error {
|
2020-08-13 13:12:32 -07:00
|
|
|
c.checkpointer.CheckpointSet().RLock()
|
|
|
|
defer c.checkpointer.CheckpointSet().RUnlock()
|
2020-05-20 10:27:26 -07:00
|
|
|
|
2020-06-22 22:59:51 -07:00
|
|
|
return c.checkpoint.ForEach(ks, f)
|
2020-05-20 10:27:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Collect requests a collection. The collection will be skipped if
|
|
|
|
// the last collection is aged less than the CachePeriod.
|
2020-06-18 10:16:33 -07:00
|
|
|
func (c *Controller) Collect(ctx context.Context) error {
|
2020-08-13 13:12:32 -07:00
|
|
|
c.checkpointer.CheckpointSet().Lock()
|
|
|
|
defer c.checkpointer.CheckpointSet().Unlock()
|
2020-05-20 10:27:26 -07:00
|
|
|
|
|
|
|
if c.period > 0 {
|
|
|
|
now := c.clock.Now()
|
|
|
|
elapsed := now.Sub(c.lastCollect)
|
|
|
|
|
|
|
|
if elapsed < c.period {
|
2020-06-18 10:16:33 -07:00
|
|
|
return nil
|
2020-05-20 10:27:26 -07:00
|
|
|
}
|
|
|
|
c.lastCollect = now
|
|
|
|
}
|
|
|
|
|
2020-08-13 13:12:32 -07:00
|
|
|
c.checkpointer.StartCollection()
|
2020-05-20 10:27:26 -07:00
|
|
|
c.accumulator.Collect(ctx)
|
2020-08-13 13:12:32 -07:00
|
|
|
err := c.checkpointer.FinishCollection()
|
|
|
|
c.checkpoint = c.checkpointer.CheckpointSet()
|
2020-06-18 10:16:33 -07:00
|
|
|
return err
|
2020-05-20 10:27:26 -07:00
|
|
|
}
|