summaryrefslogtreecommitdiff
path: root/internal/ingester/pushgateway.go
blob: c5c80c3be891e986f0c27096e9e2d95ffd945e3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package ingester

import (
	"context"
	"fmt"

	"epimetheus/internal/metrics"

	"github.com/prometheus/client_golang/prometheus/push"
)

// PushgatewayIngester handles realtime metric ingestion via Pushgateway.
// Note: Pushgateway does not preserve custom timestamps - all metrics are
// timestamped with the current time when pushed.
type PushgatewayIngester struct{}

// NewPushgatewayIngester creates a new Pushgateway ingester.
func NewPushgatewayIngester() PushgatewayIngester {
	return PushgatewayIngester{}
}

// Ingest pushes metrics to Pushgateway.
// The samples parameter is currently ignored because Pushgateway doesn't support
// custom metric values from samples - it uses registered Prometheus collectors.
// This ingests generated metrics using the provided collectors.
func (i PushgatewayIngester) Ingest(ctx context.Context, collectors metrics.Collectors, url, jobName string) error {
	select {
	case <-ctx.Done():
		return ctx.Err()
	default:
	}

	// Generate random metric values
	collectors.Simulate()

	// Create pusher with all collectors
	pusher := push.New(url, jobName).
		Collector(collectors.RequestsTotal).
		Collector(collectors.ActiveConnections).
		Collector(collectors.TemperatureCelsius).
		Collector(collectors.RequestDuration).
		Collector(collectors.JobsProcessed).
		Grouping("instance", "example-app")

	// Push metrics to Pushgateway
	if err := pusher.Push(); err != nil {
		return fmt.Errorf("failed to push to pushgateway: %w", err)
	}

	return nil
}