Skip to content

Commit b1e1ab8

Browse files
committed
prometheus: added format flag
Signed-off-by: Yurii Vlasov <[email protected]>
1 parent a2e0cc4 commit b1e1ab8

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

cmd/root.go

+6
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,12 @@ func initFlags() {
336336
"",
337337
"Prometheus pushgateway auth password",
338338
)
339+
rootCmd.Flags().StringVar(
340+
flags.PushGateway.Format,
341+
"push-gtwy-format",
342+
"",
343+
"Prometheus pushgateway format",
344+
)
339345
}
340346

341347
// ----------------------------------------------------------------------------

pkg/config/flags.go

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package config
66
import (
77
"errors"
88
"fmt"
9+
"slices"
910
"strings"
1011

1112
"k8s.io/cli-runtime/pkg/genericclioptions"
@@ -86,6 +87,13 @@ func (f *Flags) Validate() error {
8687
return errors.New("you must set --push-gtwy-url when prometheus report is enabled")
8788
}
8889
}
90+
if IsStrSet(f.PushGateway.URL) && IsStrSet(f.PushGateway.Format) {
91+
validFormats := []string{"protocompact", "protodelim", "prototext", "textplain", "openmetrics"}
92+
93+
if !slices.Contains(validFormats, *f.PushGateway.Format) {
94+
return fmt.Errorf("'--push-gtwy-format' must be one of: %s", strings.Join(validFormats, ", "))
95+
}
96+
}
8997

9098
return nil
9199
}

pkg/config/prom.go

+2
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ func newBasicAuth() BasicAuth {
2020
type PushGateway struct {
2121
URL *string
2222
BasicAuth BasicAuth
23+
Format *string
2324
}
2425

2526
func newPushGateway() *PushGateway {
2627
return &PushGateway{
2728
URL: strPtr(""),
2829
BasicAuth: newBasicAuth(),
30+
Format: strPtr("textplain"),
2931
}
3032
}

pkg/popeye.go

+29-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ import (
1111
"errors"
1212
"fmt"
1313
"io"
14+
"maps"
1415
"net/http"
1516
"os"
1617
"path/filepath"
18+
"slices"
19+
"strings"
1720
"time"
1821

1922
"github.com/derailed/popeye/internal"
@@ -408,17 +411,41 @@ func (p *Popeye) dumpPrometheus(ctx context.Context, asset string, persist bool)
408411
instance += "-" + *p.flags.InClusterName
409412
}
410413

414+
validFormats := map[string]expfmt.Format{
415+
"protocompact": expfmt.NewFormat(expfmt.TypeProtoCompact),
416+
"protodelim": expfmt.NewFormat(expfmt.TypeProtoDelim),
417+
"prototext": expfmt.NewFormat(expfmt.TypeProtoText),
418+
"textplain": expfmt.NewFormat(expfmt.TypeTextPlain),
419+
"openmetrics": expfmt.NewFormat(expfmt.TypeOpenMetrics),
420+
}
421+
422+
format := validFormats["textplain"]
423+
if config.IsStrSet(p.flags.PushGateway.Format) {
424+
if f, exists := validFormats[*p.flags.PushGateway.Format]; exists {
425+
format = f
426+
} else {
427+
validFormatsList := slices.Collect(maps.Keys(validFormats))
428+
return fmt.Errorf(
429+
"'--push-gtwy-format' must be one of: %s",
430+
strings.Join(validFormatsList, ", "),
431+
)
432+
}
433+
}
434+
411435
pusher := p.builder.ToPrometheus(
412436
p.flags.PushGateway,
413437
instance,
414438
p.client().ActiveNamespace(),
415439
asset,
416440
p.codes.Glossary,
417441
)
418-
// Enable saving to file
442+
443+
pusher = pusher.Format(format)
444+
// Persist is used when prometheus output format is selected...
445+
// ...custom p.Do func intercepts push output and prints it...
446+
// ...to stdout, while replying to pusher with stub HTTP 200
419447
if persist {
420448
pusher = pusher.Client(p)
421-
pusher = pusher.Format(expfmt.NewFormat(expfmt.TypeTextPlain))
422449
}
423450

424451
return pusher.AddContext(ctx)

0 commit comments

Comments
 (0)