mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2024-12-16 10:19:23 +02:00
4e8d667f6e
* Remove resource from export.Record * fix bridge/opencensus * partial fixes in exporters/otlp/otlpmetric * Use go-cmp to simplify exporter_test * OTLP http/grpc * prometheus * fix stdout (pending PR 2015) * oc bridge cleanups * Lint and changelog * pr num * cleanup * revert * fix multiple * always merge the environment * lint * precommit * qualify package names in changelog
101 lines
3.2 KiB
Go
101 lines
3.2 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 otlpmetrictest
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
"go.opentelemetry.io/otel/metric"
|
|
"go.opentelemetry.io/otel/metric/number"
|
|
"go.opentelemetry.io/otel/metric/sdkapi"
|
|
exportmetric "go.opentelemetry.io/otel/sdk/export/metric"
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/sum"
|
|
)
|
|
|
|
// Used to avoid implementing locking functions for test
|
|
// checkpointsets.
|
|
type noopLocker struct{}
|
|
|
|
// Lock implements sync.Locker, which is needed for
|
|
// exportmetric.CheckpointSet.
|
|
func (noopLocker) Lock() {}
|
|
|
|
// Unlock implements sync.Locker, which is needed for
|
|
// exportmetric.CheckpointSet.
|
|
func (noopLocker) Unlock() {}
|
|
|
|
// RLock implements exportmetric.CheckpointSet.
|
|
func (noopLocker) RLock() {}
|
|
|
|
// RUnlock implements exportmetric.CheckpointSet.
|
|
func (noopLocker) RUnlock() {}
|
|
|
|
// OneRecordCheckpointSet is a CheckpointSet that returns just one
|
|
// filled record. It may be useful for testing driver's metrics
|
|
// export.
|
|
type OneRecordCheckpointSet struct {
|
|
noopLocker
|
|
}
|
|
|
|
var _ exportmetric.CheckpointSet = OneRecordCheckpointSet{}
|
|
|
|
// ForEach implements exportmetric.CheckpointSet. It always invokes
|
|
// the callback once with always the same record.
|
|
func (OneRecordCheckpointSet) ForEach(kindSelector exportmetric.ExportKindSelector, recordFunc func(exportmetric.Record) error) error {
|
|
desc := metric.NewDescriptor(
|
|
"foo",
|
|
sdkapi.CounterInstrumentKind,
|
|
number.Int64Kind,
|
|
)
|
|
agg := sum.New(1)
|
|
if err := agg[0].Update(context.Background(), number.NewInt64Number(42), &desc); err != nil {
|
|
return err
|
|
}
|
|
start := time.Date(2020, time.December, 8, 19, 15, 0, 0, time.UTC)
|
|
end := time.Date(2020, time.December, 8, 19, 16, 0, 0, time.UTC)
|
|
labels := attribute.NewSet(attribute.String("abc", "def"), attribute.Int64("one", 1))
|
|
rec := exportmetric.NewRecord(&desc, &labels, agg[0].Aggregation(), start, end)
|
|
return recordFunc(rec)
|
|
}
|
|
|
|
// EmptyCheckpointSet is a checkpointer that has no records at all.
|
|
type EmptyCheckpointSet struct {
|
|
noopLocker
|
|
}
|
|
|
|
var _ exportmetric.CheckpointSet = EmptyCheckpointSet{}
|
|
|
|
// ForEach implements exportmetric.CheckpointSet. It never invokes the
|
|
// callback.
|
|
func (EmptyCheckpointSet) ForEach(kindSelector exportmetric.ExportKindSelector, recordFunc func(exportmetric.Record) error) error {
|
|
return nil
|
|
}
|
|
|
|
// FailCheckpointSet is a checkpointer that returns an error during
|
|
// ForEach.
|
|
type FailCheckpointSet struct {
|
|
noopLocker
|
|
}
|
|
|
|
var _ exportmetric.CheckpointSet = FailCheckpointSet{}
|
|
|
|
// ForEach implements exportmetric.CheckpointSet. It always fails.
|
|
func (FailCheckpointSet) ForEach(kindSelector exportmetric.ExportKindSelector, recordFunc func(exportmetric.Record) error) error {
|
|
return fmt.Errorf("fail")
|
|
}
|