middleware

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2024 License: MIT Imports: 15 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

middleware Package

The twistingmercury/middleware package is a Go library that provides middleware for instrumenting and tracing incoming requests in a Gin web application. It uses twistingmercury/telemetry to collect and export telemetry data, including metrics and traces.

Features

  • Middleware for instrumenting incoming requests in a gin-gonic/gin web application
  • Integration with OpenTelemetry for collecting and exporting traces
  • Automatic generation of request duration and count metrics
  • Parsing of user agent and request headers for detailed telemetry data
  • Customizable span naming and attribute generation
  • Correlation of logs with trace and span information

Installation

go get github.com/twistingmercury/middleware

Usage

Please visit middleware-example for a comprehensive example of how to utilize both the middleware as well as the twistingmercury/telemetry package.

  1. Initialize the logging package.
  2. Initialize the metrics package.
  3. Invoke metrics.Publish()
  4. Initialize the tracing package.
  5. Create a gin router and invoke gin.Use(middleware.PrometheusMetrics(..), middleware.OtelTracing(..), middleware.Logging(..)).

After that, you can define your routes and handlers as usual, and the middleware will automatically instrument and trace the incoming requests.

Telemetry Data

The Gin Middleware package generates the following telemetry data for the gin handlers (RESTful endpoints):

  • Request duration metric: Measures the duration of each incoming request in milliseconds.
  • Request count metric: Counts the number of incoming requests.
  • Current request gauge metric: the current number of active requests.
  • Request trace: Creates a trace for each incoming request, including span information.
  • Detailed request information: Parses the user agent and request headers to include additional attributes in the telemetry data.
Telemetry collection
  • Logs: Logs are written to stdout and correlated with traces if tracing middleware is used.
  • Traces: Traces are sent to the configured exporter. In a production environment, you'd create gRPC exporter and send the data to an OTel collector.
  • Metrics: Metrics are exposed by default over http on port 9090, i.e., http://[my-api]:9090/metrics

Contributing

Contributions to the twistingmercury/telemetry package are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request on the GitHub repository.

License

The twistingmercury/middleware package is open-source and released under the MIT License.

Documentation

Index

Constants

View Source
const (
	UserAgentOS             = "http.user_agent.os"
	UserAgentOSVersion      = "http.user_agent.os_version"
	UserAgentDevice         = "http.user_agent.device"
	UserAgentBrowser        = "http.user_agent.browser"
	UserAgentBrowserVersion = "http.user_agent.browser_version"
	BrowserChrome           = "chrome"
	BrowserSafari           = "safari"
	BrowserFirefox          = "firefox"
	BrowserOpera            = "opera"
	BrowserIE               = "ie"
	BrowserEdge             = "edge"
	BrowserTrident          = "Trident"
	DeviceMobile            = "mobile"
	DeviceDesktop           = "desktop"
	DeviceBot               = "bot"
)
View Source
const (
	Http            = "http"
	Https           = "https"
	HttpMethod      = "http.request.method"
	HttpPath        = "http.request.path"
	HttpRemoteAddr  = "http.request.remoteAddr"
	HttpRequestHost = "http.request.host"
	HttpStatus      = "http.response.status"
	HttpLatency     = "http.response.latency"
	TLSVersion      = "http.tls.serviceVersion"
	HttpScheme      = "http.scheme"
)

Variables

This section is empty.

Functions

func Initialize deprecated

func Initialize(registry *prometheus.Registry, namespace, apiname string) error

Initialize preps the middleware.

Deprecated: use GetMetricsMiddleware, GetTracingMiddleware, and GetLoggingMiddleware which do not require separate initialization.

func Logging added in v1.2.0

func Logging(options LoggingOptions) gin.HandlerFunc

Logging returns the logging middleware

func Metrics added in v1.0.0

Metrics provides the prometheus metrics that are to be tracked.

func OtelTracing added in v1.2.0

func OtelTracing(options TracingOptions) gin.HandlerFunc

OtelTracing returns the tracing middleware.

func ParseHeaders

func ParseHeaders(headers map[string][]string) (args map[string]any)

ParseHeaders parses the headers and returns a map of attribs.

func ParseUserAgent

func ParseUserAgent(rawUserAgent string) (args map[string]any)

ParseUserAgent parses the user agent string and returns a map of attribs.

func PrometheusMetrics added in v1.2.0

func PrometheusMetrics(registry *prometheus.Registry, namespace string, apiname string, options MetricsOptions) gin.HandlerFunc

PrometheusMetrics returns the metrics middleware used by the Prometheus software.

func SpanStatus

func SpanStatus(status int) (code otelCodes.Code, desc string)

SpanStatus returns the OpenTelemetry statusLabel code as defined in go.opentelemetry.io/old_elemetry/codes and a brief description for a given HTTP statusLabel code.

func Telemetry deprecated

func Telemetry() gin.HandlerFunc

Telemetry returns middleware that will instrument and trace incoming requests.

Deprecated: use GetMetricsMiddleware, GetTracingMiddleware, and GetLoggingMiddleware to get separated middleware for metrics, tracing, and logging.

Types

type LoggingOption added in v1.2.0

type LoggingOption func(*LoggingOptions)

type LoggingOptions added in v1.2.0

type LoggingOptions struct {
}

type MetricsOption added in v1.2.0

type MetricsOption func(*MetricsOptions)

type MetricsOptions added in v1.2.0

type MetricsOptions struct {
}

type TracingOption added in v1.2.0

type TracingOption func(*TracingOptions)

func WithExcludedPaths added in v1.2.0

func WithExcludedPaths(paths []string) TracingOption

WithExcludedPaths allows to filter out URL paths, so that they are not traced.

type TracingOptions added in v1.2.0

type TracingOptions struct {
	ExcludedPaths map[string]struct{}
}

func NewTracingOptions added in v1.2.0

func NewTracingOptions(opts ...TracingOption) TracingOptions

NewTracingOptions builds TracingOptions based on the provided options.

Jump to

Keyboard shortcuts

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