Skip to content

Commit

Permalink
Add delete all pushgateway metric endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mcorbin committed Jul 4, 2024
1 parent 7c45066 commit 6a46a73
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 2 deletions.
12 changes: 12 additions & 0 deletions internal/database/pushgateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,15 @@ func (c *Database) CleanPushgatewayMetrics(ctx context.Context) (int64, error) {
}
return affected, nil
}

func (c *Database) DeleteAllPushgatewayMetrics(ctx context.Context) error {
result, err := c.db.ExecContext(ctx, "TRUNCATE pushgateway_metric")
if err != nil {
return fmt.Errorf("fail to clean all pushgateway metrics: %w", err)
}
_, err = result.RowsAffected()
if err != nil {
return fmt.Errorf("fail to check affected row: %w", err)
}
return nil
}
10 changes: 10 additions & 0 deletions internal/database/pushgateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,14 @@ func TestPushgatewayCRUD(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, int64(5), deleteCount)

metric, err = TestComponent.GetMetrics(context.Background())
assert.NoError(t, err)
assert.Len(t, metric, 5)

err = TestComponent.DeleteAllPushgatewayMetrics(context.Background())
assert.NoError(t, err)

metric, err = TestComponent.GetMetrics(context.Background())
assert.NoError(t, err)
assert.Len(t, metric, 0)
}
1 change: 1 addition & 0 deletions internal/http/handlers/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type PushgatewayService interface {
DeleteMetricsByName(ctx context.Context, name string) error
DeleteMetricByID(ctx context.Context, id string) error
PrometheusMetrics(ctx context.Context) (string, error)
DeleteAllPushgatewayMetrics(ctx context.Context) error
}

type Builder struct {
Expand Down
9 changes: 9 additions & 0 deletions internal/http/handlers/pushgateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ func (b *Builder) DeleteMetric(ec echo.Context) error {
return ec.JSON(http.StatusOK, NewResponse("metrics deleted"))
}

func (b *Builder) DeleteAllPushgatewayMetrics(ec echo.Context) error {

err := b.pushgateway.DeleteAllPushgatewayMetrics(ec.Request().Context())
if err != nil {
return err
}
return ec.JSON(http.StatusOK, NewResponse("metrics deleted"))
}

func (b *Builder) ListPushgatewayMetrics(ec echo.Context) error {
metrics, err := b.pushgateway.GetMetrics(ec.Request().Context())
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/http/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -871,8 +871,8 @@ func TestIntegration(t *testing.T) {
},
{
url: "/api/v1/pushgateway",
expectedStatus: 404,
body: "Not Found",
expectedStatus: 200,
body: "metrics deleted",
method: "DELETE",
headers: map[string]string{
"Authorization": basicAuth(testUser, testPassword),
Expand Down
1 change: 1 addition & 0 deletions internal/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func NewServer(logger *slog.Logger, config Configuration, registry *prometheus.R
apiGroup.GET("/healthcheck", builder.ListHealthchecks)
apiGroup.GET("/cabourotte/discovery", builder.CabourotteDiscovery)
apiGroup.POST("/pushgateway", builder.CreateOrUpdatePushgatewayMetric)
apiGroup.DELETE("/pushgateway", builder.DeleteAllPushgatewayMetrics)
apiGroup.DELETE("/pushgateway/:identifier", builder.DeleteMetric)
apiGroup.GET("/pushgateway", builder.ListPushgatewayMetrics)
apiGroup.GET("/pushgateway/prometheus/metrics", builder.PushgatewayMetrics)
Expand Down
5 changes: 5 additions & 0 deletions pkg/pushgateway/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ func (s *Service) DeleteMetricByID(ctx context.Context, id string) error {
return s.store.DeleteMetricByID(ctx, id)
}

func (s *Service) DeleteAllPushgatewayMetrics(ctx context.Context) error {
s.logger.Debug("deleting all push gateway metrics")
return s.store.DeleteAllPushgatewayMetrics(ctx)
}

func (s *Service) CleanPushgatewayMetrics(ctx context.Context) {
numberMetricsDeleted, err := s.store.CleanPushgatewayMetrics(ctx)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/pushgateway/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Store interface {
DeleteMetricsByName(ctx context.Context, name string) error
DeleteMetricByID(ctx context.Context, id string) error
CleanPushgatewayMetrics(ctx context.Context) (int64, error)
DeleteAllPushgatewayMetrics(ctx context.Context) error
}

type Service struct {
Expand Down

0 comments on commit 6a46a73

Please sign in to comment.