metrics

package
v0.0.0-...-546b1e8 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultMeterName represents the default name of the meter used to create metrics. It is set to "go.opentelemetry.io/otel/metric/echo-metrics".
	DefaultMeterName string = "go.opentelemetry.io/otel/metric/echo-metrics"
	// DefaultInstrumentNamePrefix represents the default prefix added to the names of the created metrics. It is set to "echo".
	DefaultInstrumentNamePrefix string = "echo"
)
View Source
const MeterName string = "github.com/SaimonWoidig/cc-microsvcs/common/otel/metrics"

Variables

This section is empty.

Functions

func New

func New() (echo.MiddlewareFunc, error)

New returns a new instance of the middleware function with default configuration.

The returned middleware function measures the latency, request count, request size, and response size of HTTP requests handled by an Echo server. It uses OpenTelemetry to create metrics and records the measurements for each request.

The default configuration uses the following settings:

  • MeterProvider: The default OpenTelemetry meter provider obtained from otel.GetMeterProvider().
  • Skipper: The default Echo middleware skipper obtained from middleware.DefaultSkipper.
  • MeterName: The default meter name "echo_http_metrics".
  • InstrumentNamePrefix: The default instrument name prefix "echo".

Returns:

  • echo.MiddlewareFunc: The middleware function that can be used with Echo's Use() method.
  • error: An error if there was a problem creating the metrics or configuring the middleware.

Example usage:

e := echo.New()
e.Use(New())

Note: This function is a convenience wrapper around NewWithConfig() and provides default configuration values. If you need to customize the configuration, use NewWithConfig() instead.

See Also:

  • NewWithConfig: Creates a new instance of the middleware function with custom configuration.
  • Config: The configuration struct used to customize the middleware behavior.

func NewMeter

func NewMeter(mp *sdkmetric.MeterProvider) metric.Meter

func NewMeterProvider

func NewMeterProvider(res *sdkresource.Resource, exporter sdkmetric.Exporter, exportInterval time.Duration, memStatsInterval time.Duration) (*sdkmetric.MeterProvider, error)

func NewOTLPMetricExporter

func NewOTLPMetricExporter(ctx context.Context, otlpEndpoint string, exportTimeout time.Duration) (*otlpmetrichttp.Exporter, error)

func NewStdoutMetricExporter

func NewStdoutMetricExporter() (sdkmetric.Exporter, error)

func NewWithConfig

func NewWithConfig(config Config) (echo.MiddlewareFunc, error)

NewWithConfig is a function that creates and returns an Echo middleware function with the given configuration.

Parameters:

  • config: A Config struct that contains the configuration options for the middleware.

Returns:

  • echo.MiddlewareFunc: The Echo middleware function.
  • error: An error if any error occurs during the creation of the middleware.

The NewWithConfig function initializes the required metrics for monitoring HTTP requests and responses. It creates histograms for request duration, request count, request size, and response size. The function then returns a middleware function that records the metrics for each incoming request and response.

The middleware function measures the duration of each request, the count of requests, the size of the request, and the size of the response. It records these metrics using the configured meter provider and attribute set. The function also handles any errors that occur during the execution of the next handler and sets the appropriate HTTP status code.

Example usage:

config := Config{
	Skipper: middleware.DefaultSkipper,
	MeterProvider: otel.GetMeterProvider(),
	InstrumentNamePrefix: "myapp",
}

middlewareFunc, err := NewWithConfig(config)

if err != nil {
	log.Fatal(err)
}

e := echo.New()
e.Use(middlewareFunc)

Note: The NewWithConfig function assumes that the required dependencies, such as the Echo framework and OpenTelemetry meter provider, are already imported and configured.

Types

type Config

type Config struct {
	// MeterProvider is metric.MeterProvider used to create metrics.
	MeterProvider metric.MeterProvider
	// Skipper is the middleware.Skipper function used to determine if the middleware should be skipped for a request.
	Skipper middleware.Skipper
	// MeterName is the name of the meter used to create metrics.
	MeterName string
	// InstrumentNamePrefix is the prefix added to the names of the created metrics.
	InstrumentNamePrefix string
}

Config is a struct that represents the configuration options for a metrics middleware.

Jump to

Keyboard shortcuts

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