mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2024-12-12 10:04:29 +02:00
b5292b8459
* Add the cache type * Add cache unit tests * Test cache concurrency * Add the instrumentCache * Use the instrumentCache to deduplicate creation * Drop unique check from addAggregator * Fix aggregatorCache* docs * Update cachedAggregator and aggregator method docs * Remove unnecessary type constraint * Remove unused errAlreadyRegistered * Rename to not shadow imports * Add changes to changelog * Fix changelog English * Store resolvers in the meter instead of caches * Test all Aggregator[N] impls are comparable * Fix lint * Add documentation that Aggregators need to be comparable * Update sdk/metric/internal/aggregator.go Co-authored-by: Anthony Mirabella <a9@aneurysm9.com> * Update sdk/metric/instrument.go Co-authored-by: Anthony Mirabella <a9@aneurysm9.com> * Update sdk/metric/instrument.go Co-authored-by: Anthony Mirabella <a9@aneurysm9.com> * Update sdk/metric/internal/aggregator_test.go Co-authored-by: Anthony Mirabella <a9@aneurysm9.com> * Fix pipeline_test.go use of newInstrumentCache Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>
137 lines
4.3 KiB
Go
137 lines
4.3 KiB
Go
// 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 metric // import "go.opentelemetry.io/otel/sdk/metric"
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
|
|
"go.opentelemetry.io/otel/metric"
|
|
"go.opentelemetry.io/otel/metric/instrument"
|
|
"go.opentelemetry.io/otel/metric/instrument/asyncfloat64"
|
|
"go.opentelemetry.io/otel/metric/instrument/asyncint64"
|
|
"go.opentelemetry.io/otel/metric/instrument/syncfloat64"
|
|
"go.opentelemetry.io/otel/metric/instrument/syncint64"
|
|
"go.opentelemetry.io/otel/sdk/instrumentation"
|
|
)
|
|
|
|
// meterRegistry keeps a record of initialized meters for instrumentation
|
|
// scopes. A meter is unique to an instrumentation scope and if multiple
|
|
// requests for that meter are made a meterRegistry ensure the same instance
|
|
// is used.
|
|
//
|
|
// The zero meterRegistry is empty and ready for use.
|
|
//
|
|
// A meterRegistry must not be copied after first use.
|
|
//
|
|
// All methods of a meterRegistry are safe to call concurrently.
|
|
type meterRegistry struct {
|
|
sync.Mutex
|
|
|
|
meters map[instrumentation.Scope]*meter
|
|
|
|
pipes pipelines
|
|
}
|
|
|
|
// Get returns a registered meter matching the instrumentation scope if it
|
|
// exists in the meterRegistry. Otherwise, a new meter configured for the
|
|
// instrumentation scope is registered and then returned.
|
|
//
|
|
// Get is safe to call concurrently.
|
|
func (r *meterRegistry) Get(s instrumentation.Scope) *meter {
|
|
r.Lock()
|
|
defer r.Unlock()
|
|
|
|
if r.meters == nil {
|
|
m := newMeter(s, r.pipes)
|
|
r.meters = map[instrumentation.Scope]*meter{s: m}
|
|
return m
|
|
}
|
|
|
|
m, ok := r.meters[s]
|
|
if ok {
|
|
return m
|
|
}
|
|
|
|
m = newMeter(s, r.pipes)
|
|
r.meters[s] = m
|
|
return m
|
|
}
|
|
|
|
// meter handles the creation and coordination of all metric instruments. A
|
|
// meter represents a single instrumentation scope; all metric telemetry
|
|
// produced by an instrumentation scope will use metric instruments from a
|
|
// single meter.
|
|
type meter struct {
|
|
instrumentation.Scope
|
|
|
|
// *Resolvers are used by the provided instrument providers to resolve new
|
|
// instruments aggregators and maintain a cache across instruments this
|
|
// meter owns.
|
|
int64Resolver resolver[int64]
|
|
float64Resolver resolver[float64]
|
|
|
|
pipes pipelines
|
|
}
|
|
|
|
func newMeter(s instrumentation.Scope, p pipelines) *meter {
|
|
// viewCache ensures instrument conflicts, including number conflicts, this
|
|
// meter is asked to create are logged to the user.
|
|
var viewCache cache[string, instrumentID]
|
|
|
|
// Passing nil as the ac parameter to newInstrumentCache will have each
|
|
// create its own aggregator cache.
|
|
ic := newInstrumentCache[int64](nil, &viewCache)
|
|
fc := newInstrumentCache[float64](nil, &viewCache)
|
|
|
|
return &meter{
|
|
Scope: s,
|
|
pipes: p,
|
|
|
|
int64Resolver: newResolver(p, ic),
|
|
float64Resolver: newResolver(p, fc),
|
|
}
|
|
}
|
|
|
|
// Compile-time check meter implements metric.Meter.
|
|
var _ metric.Meter = (*meter)(nil)
|
|
|
|
// AsyncInt64 returns the asynchronous integer instrument provider.
|
|
func (m *meter) AsyncInt64() asyncint64.InstrumentProvider {
|
|
return asyncInt64Provider{scope: m.Scope, resolve: &m.int64Resolver}
|
|
}
|
|
|
|
// AsyncFloat64 returns the asynchronous floating-point instrument provider.
|
|
func (m *meter) AsyncFloat64() asyncfloat64.InstrumentProvider {
|
|
return asyncFloat64Provider{scope: m.Scope, resolve: &m.float64Resolver}
|
|
}
|
|
|
|
// RegisterCallback registers the function f to be called when any of the
|
|
// insts Collect method is called.
|
|
func (m *meter) RegisterCallback(insts []instrument.Asynchronous, f func(context.Context)) error {
|
|
m.pipes.registerCallback(f)
|
|
return nil
|
|
}
|
|
|
|
// SyncInt64 returns the synchronous integer instrument provider.
|
|
func (m *meter) SyncInt64() syncint64.InstrumentProvider {
|
|
return syncInt64Provider{scope: m.Scope, resolve: &m.int64Resolver}
|
|
}
|
|
|
|
// SyncFloat64 returns the synchronous floating-point instrument provider.
|
|
func (m *meter) SyncFloat64() syncfloat64.InstrumentProvider {
|
|
return syncFloat64Provider{scope: m.Scope, resolve: &m.float64Resolver}
|
|
}
|