package metrictest
import "go.opentelemetry.io/otel/sdk/metric/metrictest"
Index ¶
- func NewDescriptor(name string, ikind sdkapi.InstrumentKind, nkind number.Kind, opts ...instrument.Option) sdkapi.Descriptor
- type ExportRecord
- type Exporter
- func NewTestMeterProvider(opts ...Option) (metric.MeterProvider, *Exporter)
- func (e *Exporter) Collect(ctx context.Context) error
- func (e *Exporter) GetByName(name string) (ExportRecord, error)
- func (e *Exporter) GetByNameAndAttributes(name string, attributes []attribute.KeyValue) (ExportRecord, error)
- func (e *Exporter) GetRecords() []ExportRecord
- type Library
- type Option
- type Scope
Examples ¶
Functions ¶
func NewDescriptor ¶
func NewDescriptor(name string, ikind sdkapi.InstrumentKind, nkind number.Kind, opts ...instrument.Option) sdkapi.Descriptor
NewDescriptor is a test helper for constructing test metric descriptors using standard options.
Types ¶
type ExportRecord ¶
type ExportRecord struct { InstrumentName string InstrumentationLibrary Library Attributes []attribute.KeyValue AggregationKind aggregation.Kind NumberKind number.Kind Sum number.Number Count uint64 Histogram aggregation.Buckets LastValue number.Number }
ExportRecord represents one collected datapoint from the Exporter.
type Exporter ¶
type Exporter struct { // Records contains the last metrics collected. Records []ExportRecord // contains filtered or unexported fields }
Exporter is a manually collected exporter for testing the SDK. It does not satisfy the `export.Exporter` interface because it is not intended to be used with the periodic collection of the SDK, instead the test should manually call `Collect()`
Exporters are not thread safe, and should only be used for testing.
func NewTestMeterProvider ¶
func NewTestMeterProvider(opts ...Option) (metric.MeterProvider, *Exporter)
NewTestMeterProvider creates a MeterProvider and Exporter to be used in tests.
func (*Exporter) Collect ¶
Collect triggers the SDK's collect methods and then aggregates the data into ExportRecords. This will overwrite any previous collected metrics.
func (*Exporter) GetByName ¶
func (e *Exporter) GetByName(name string) (ExportRecord, error)
GetByName returns the first Record with a matching instrument name.
Code:play
Output:Example¶
package main
import (
"context"
"fmt"
"go.opentelemetry.io/otel/sdk/metric/metrictest"
)
func main() {
mp, exp := metrictest.NewTestMeterProvider()
meter := mp.Meter("go.opentelemetry.io/otel/sdk/metric/metrictest/exporter_ExampleExporter_GetByName")
cnt, err := meter.SyncFloat64().Counter("fCount")
if err != nil {
panic("could not acquire counter")
}
cnt.Add(context.Background(), 2.5)
err = exp.Collect(context.Background())
if err != nil {
panic("collection failed")
}
out, _ := exp.GetByName("fCount")
fmt.Println(out.Sum.AsFloat64())
}
2.5
func (*Exporter) GetByNameAndAttributes ¶
func (e *Exporter) GetByNameAndAttributes(name string, attributes []attribute.KeyValue) (ExportRecord, error)
GetByNameAndAttributes returns the first Record with a matching name and the sub-set of attributes.
Code:play
Output:Example¶
package main
import (
"context"
"fmt"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/metric/metrictest"
)
func main() {
mp, exp := metrictest.NewTestMeterProvider()
meter := mp.Meter("go.opentelemetry.io/otel/sdk/metric/metrictest/exporter_ExampleExporter_GetByNameAndAttributes")
cnt, err := meter.SyncFloat64().Counter("fCount")
if err != nil {
panic("could not acquire counter")
}
cnt.Add(context.Background(), 4, attribute.String("foo", "bar"), attribute.Bool("found", false))
err = exp.Collect(context.Background())
if err != nil {
panic("collection failed")
}
out, err := exp.GetByNameAndAttributes("fCount", []attribute.KeyValue{attribute.String("foo", "bar")})
if err != nil {
println(err.Error())
}
fmt.Println(out.Sum.AsFloat64())
}
4
func (*Exporter) GetRecords ¶
func (e *Exporter) GetRecords() []ExportRecord
GetRecords returns all Records found by the SDK.
type Library ¶
type Library = Scope
Deprecated: please use Scope instead.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option allow for control of details of the TestMeterProvider created.
func WithTemporalitySelector ¶
func WithTemporalitySelector(ts aggregation.TemporalitySelector) Option
WithTemporalitySelector allows for the use of either cumulative (default) or delta metrics.
Warning: the current SDK does not convert async instruments into delta temporality.
type Scope ¶
Scope is the same as "sdk/instrumentation".Scope but there is a package cycle to use it so it is redeclared here.
Source Files ¶
config.go doc.go exporter.go
- Version
- v0.31.0
- Published
- Jul 8, 2022
- Platform
- darwin/amd64
- Imports
- 13 packages
- Last checked
- 31 minutes ago –
Tools for package owners.