metrictest

package
v0.0.0-...-dba27d4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

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

func (e *Exporter) Collect(ctx context.Context) error

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.

Example
package main

import (
	"context"
	"fmt"

	"github.com/hitzhangjie/codemaster/opentelemetry/otel/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())
}
Output:

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.

Example
package main

import (
	"context"
	"fmt"

	"go.opentelemetry.io/otel/attribute"

	"github.com/hitzhangjie/codemaster/opentelemetry/otel/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())
}
Output:

4

func (*Exporter) GetRecords

func (e *Exporter) GetRecords() []ExportRecord

GetRecords returns all Records found by the SDK.

type Library

type Library struct {
	InstrumentationName    string
	InstrumentationVersion string
	SchemaURL              string
}

Library is the same as "sdk/instrumentation".Library but there is a package cycle to use it so it is redeclared here.

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option allow for control of details of the TestMeterProvider created.

func WithExplicitBoundaries

func WithExplicitBoundaries(boundaries []float64) Option

WithExplicitBoundaries allows for the explicit specified boundaries for histogram.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL