prometheus

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package prometheus provides a Prometheus adapter for github.com/abczzz13/clientip.

The package exposes clientip options that install a Prometheus-backed Metrics implementation on an extractor, using either the default registerer or a caller-provided registerer.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PrometheusMetrics

type PrometheusMetrics struct {
	// contains filtered or unexported fields
}

PrometheusMetrics is a Prometheus-backed implementation of clientip.Metrics.

func New

func New() (*PrometheusMetrics, error)

New creates Prometheus-backed metrics and registers its collectors on prom.DefaultRegisterer.

Example
package main

import (
	"fmt"
	"net/http"

	"github.com/abczzz13/clientip"
	clientipprom "github.com/abczzz13/clientip/prometheus"
)

func main() {
	metrics, err := clientipprom.New()
	if err != nil {
		panic(err)
	}

	extractor, err := clientip.New(clientip.Config{Metrics: metrics})
	if err != nil {
		panic(err)
	}

	result, err := extractor.Extract(&http.Request{
		RemoteAddr: "1.1.1.1:12345",
		Header:     make(http.Header),
	})
	if err != nil {
		panic(err)
	}

	fmt.Println(result.IP, result.Source)
}
Output:
1.1.1.1 remote_addr
Example (ConfigMetrics)
package main

import (
	"fmt"
	"net/http"

	"github.com/abczzz13/clientip"
	clientipprom "github.com/abczzz13/clientip/prometheus"
)

func main() {
	metrics, err := clientipprom.New()
	if err != nil {
		panic(err)
	}

	extractor, err := clientip.New(clientip.Config{Metrics: metrics})
	if err != nil {
		panic(err)
	}

	result, err := extractor.Extract(&http.Request{
		RemoteAddr: "1.1.1.1:12345",
		Header:     make(http.Header),
	})
	if err != nil {
		panic(err)
	}

	fmt.Println(result.IP, result.Source)
}
Output:
1.1.1.1 remote_addr

func NewWithRegisterer

func NewWithRegisterer(registerer prom.Registerer) (*PrometheusMetrics, error)

NewWithRegisterer creates Prometheus-backed metrics and registers its collectors on the given registerer.

If registerer is nil, prom.DefaultRegisterer is used. If the metrics are already registered, existing compatible collectors are reused.

Example
registry := prom.NewRegistry()

metrics, err := clientipprom.NewWithRegisterer(registry)
if err != nil {
	panic(err)
}

extractor, err := clientip.New(clientip.Config{Metrics: metrics})
if err != nil {
	panic(err)
}

_, err = extractor.Extract(&http.Request{
	RemoteAddr: "1.1.1.1:12345",
	Header:     make(http.Header),
})
if err != nil {
	panic(err)
}

fmt.Printf("%.0f\n", counterValue(registry, "ip_extraction_total", map[string]string{
	"source": clientip.SourceRemoteAddr.String(),
	"result": "success",
}))
Output:
1
Example (ConfigMetrics)
registry := prom.NewRegistry()

metrics, err := clientipprom.NewWithRegisterer(registry)
if err != nil {
	panic(err)
}

extractor, err := clientip.New(clientip.Config{Metrics: metrics})
if err != nil {
	panic(err)
}

_, err = extractor.Extract(&http.Request{
	RemoteAddr: "1.1.1.1:12345",
	Header:     make(http.Header),
})
if err != nil {
	panic(err)
}

fmt.Printf("%.0f\n", counterValue(registry, "ip_extraction_total", map[string]string{
	"source": clientip.SourceRemoteAddr.String(),
	"result": "success",
}))
Output:
1

func (*PrometheusMetrics) RecordExtractionFailure

func (m *PrometheusMetrics) RecordExtractionFailure(source string)

RecordExtractionFailure increments ip_extraction_total with result="invalid" for the provided source.

func (*PrometheusMetrics) RecordExtractionSuccess

func (m *PrometheusMetrics) RecordExtractionSuccess(source string)

RecordExtractionSuccess increments ip_extraction_total with result="success" for the provided source.

func (*PrometheusMetrics) RecordSecurityEvent

func (m *PrometheusMetrics) RecordSecurityEvent(event string)

RecordSecurityEvent increments ip_extraction_security_events_total for the provided event label.

Jump to

Keyboard shortcuts

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