prometheus

package
v2.0.5 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Provider

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

Provider is a struct that implements collector and provider methods. It gives users a simple way to use metrics for go-vshard-router.

func NewPrometheusProvider

func NewPrometheusProvider() *Provider

NewPrometheusProvider - is an experimental function. Prometheus Provider is one of the ready-to-use providers implemented for go-vshard-router. It can be used to easily integrate metrics into your service without worrying about buckets, metric names, or other metric options.

The provider implements both the interface required by vshard-router and the Prometheus collector interface.

To register it in your Prometheus instance, use: registry.MustRegister(provider)

Then, pass it to go-vshard-router so that it can manage the metrics:

vshard_router.NewRouter(ctx, vshard_router.Config{
    Metrics: provider,
})

This approach simplifies the process of collecting and handling metrics, freeing you from manually managing metric-specific configurations.

Example
// Let's create new prometheus provider.
provider := NewPrometheusProvider()

// Create new prometheus registry.
registry := prometheus.NewRegistry()
// Register prometheus provider.
registry.MustRegister(provider)

// Create example http server.
server := httptest.NewServer(promhttp.HandlerFor(registry, promhttp.HandlerOpts{}))
defer server.Close()

// Then we can register our provider in go vshard router.
// It will use our provider to write prometheus metrics.
/*
	vshard_router.NewRouter(ctx, vshard_router.Config{
		Metrics: provider,
	})
*/

provider.CronDiscoveryEvent(true, 150*time.Millisecond, "success")
provider.RetryOnCall("timeout")
provider.RequestDuration(200*time.Millisecond, "test", true, false)

resp, err := http.Get(server.URL + "/metrics")
if err != nil {
	panic(err)
}

defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
	panic(err)
}

metricsOutput := string(body)

if strings.Contains(metricsOutput, "vshard_request_duration_bucket") {
	fmt.Println("Metrics output contains vshard_request_duration_bucket")
}
if strings.Contains(metricsOutput, "vshard_cron_discovery_event_bucket") {
	fmt.Println("Metrics output contains vshard_cron_discovery_event_bucket")
}

if strings.Contains(metricsOutput, "vshard_retry_on_call") {
	fmt.Println("Metrics output contains vshard_retry_on_call")
}
Output:

Metrics output contains vshard_request_duration_bucket
Metrics output contains vshard_cron_discovery_event_bucket
Metrics output contains vshard_retry_on_call

func (*Provider) Collect

func (pp *Provider) Collect(ch chan<- prometheus.Metric)

Collect gathers the metrics and sends them to the provided channel.

func (*Provider) CronDiscoveryEvent

func (pp *Provider) CronDiscoveryEvent(ok bool, duration time.Duration, reason string)

CronDiscoveryEvent records the duration of a cron discovery event with labels.

func (*Provider) Describe

func (pp *Provider) Describe(ch chan<- *prometheus.Desc)

Describe sends the descriptors of each metric to the provided channel.

func (*Provider) RequestDuration

func (pp *Provider) RequestDuration(duration time.Duration, procedure string, ok, mapReduce bool)

RequestDuration records the duration of a request with labels for success and map-reduce usage.

func (*Provider) RetryOnCall

func (pp *Provider) RetryOnCall(reason string)

RetryOnCall increments the retry counter for a specific reason.

Jump to

Keyboard shortcuts

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