ginprometheus

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2024 License: MIT Imports: 16 Imported by: 0

README ΒΆ

πŸ›° gin-prometheus

Prometheus metrics exporter for Gin base on OpenTelemetry.

πŸ”° Installation

$ go get -u github.com/hanshuaikang/gin-prometheus

πŸ“ Usage

It's easy to get started with in-prometheus, You just need to install a middleware for your Gin project

package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	ginprometheus "github.com/hanshuaikang/gin-prometheus"
	semconv "go.opentelemetry.io/otel/semconv/v1.25.0"
	"net/http"
)

const serviceName = "gin-prometheus-demo"

func main() {
	fmt.Println("initializing")
	r := gin.New()
	r.Use(ginprometheus.Middleware())
	r.GET("/ping", func(ctx *gin.Context) {
		ctx.JSON(http.StatusOK, gin.H{
			"message": "pong",
		})
	})
	r.Run()
}

The Prometheus service will start, you can visit http://localhost:2233/metrics at your browser

gin-prometheus supports a lot of custom configuration. the detail please look example/example.go file

πŸŽ‰ Metrics

Details about exposed Prometheus metrics.

Name Type Exposed Information
http_server_active_requests Counter Number of requests inflight.
http_server_duration Histogram Time Taken by request
http_server_request_total Counter Number of Requests.
http_server_request_content_length Histogram HTTP request sizes in bytes.
http_server_response_content_length Histogram HTTP response sizes in bytes.
system_cpu_usage Counter CPU Usage
system_memory_usage Counter Memory Usage

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

View Source
var DefaultAttributes = func(route string, request *http.Request) []attribute.KeyValue {
	attrs := []attribute.KeyValue{
		semconv.HTTPMethodKey.String(request.Method),
	}
	if route != "" {
		attrs = append(attrs, semconv.HTTPRouteKey.String(route))
	}
	return attrs
}

Functions ΒΆ

func Middleware ΒΆ

func Middleware(options ...Option) gin.HandlerFunc

Middleware 请求中间仢

Types ΒΆ

type Option ΒΆ

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

Option applies a configuration to the given config

func WithAttributes ΒΆ

func WithAttributes(attributes func(route string, request *http.Request) []attribute.KeyValue) Option

func WithGlobalAttributes ΒΆ

func WithGlobalAttributes(attributes []attribute.KeyValue) Option

func WithGroupedStatusDisabled ΒΆ

func WithGroupedStatusDisabled() Option

func WithMetricPrefix ΒΆ

func WithMetricPrefix(prefix string) Option

func WithPrometheusPort ΒΆ

func WithPrometheusPort(port int) Option

func WithRecordDurationDisabled ΒΆ

func WithRecordDurationDisabled() Option

func WithRecordInFlightDisabled ΒΆ

func WithRecordInFlightDisabled() Option

func WithRecordSizeDisabled ΒΆ

func WithRecordSizeDisabled() Option

func WithRecorder ΒΆ

func WithRecorder(recorder Recorder) Option

func WithService ΒΆ

func WithService(serviceName string, version string) Option

func WithShouldRecordFunc ΒΆ

func WithShouldRecordFunc(shouldRecord func(route string, request *http.Request) bool) Option

func WithSystemMetricDisabled ΒΆ

func WithSystemMetricDisabled() Option

type Recorder ΒΆ

type Recorder interface {
	// AddRequests increments the number of requests being processed.
	AddRequests(ctx context.Context, quantity int64, attributes []attribute.KeyValue)

	// ObserveHTTPRequestDuration measures the duration of an HTTP request.
	ObserveHTTPRequestDuration(ctx context.Context, duration time.Duration, attributes []attribute.KeyValue)

	// ObserveHTTPRequestSize measures the size of an HTTP request in bytes.
	ObserveHTTPRequestSize(ctx context.Context, sizeBytes int64, attributes []attribute.KeyValue)

	// ObserveHTTPResponseSize measures the size of an HTTP response in bytes.
	ObserveHTTPResponseSize(ctx context.Context, sizeBytes int64, attributes []attribute.KeyValue)

	// AddInflightRequests increments and decrements the number of inflight request being processed.
	AddInflightRequests(ctx context.Context, quantity int64, attributes []attribute.KeyValue)

	// ObserveSystemMetric measures the cpu and memory usage
	ObserveSystemMetric(ctx context.Context, attributes []attribute.KeyValue)
}

func NewHttpMetricsRecorder ΒΆ

func NewHttpMetricsRecorder(serviceName, version, metricsPrefix string) Recorder

Directories ΒΆ

Path Synopsis

Jump to

Keyboard shortcuts

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