Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,12 @@ func initFlags() {
"",
"Prometheus pushgateway auth password",
)
rootCmd.Flags().StringVar(
flags.PushGateway.Format,
"push-gtwy-format",
"",
"Prometheus pushgateway format",
)
}

// ----------------------------------------------------------------------------
Expand Down
8 changes: 8 additions & 0 deletions pkg/config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package config
import (
"errors"
"fmt"
"slices"
"strings"

"k8s.io/cli-runtime/pkg/genericclioptions"
Expand Down Expand Up @@ -86,6 +87,13 @@ func (f *Flags) Validate() error {
return errors.New("you must set --push-gtwy-url when prometheus report is enabled")
}
}
if IsStrSet(f.PushGateway.URL) && IsStrSet(f.PushGateway.Format) {
validFormats := []string{"protocompact", "protodelim", "prototext", "textplain", "openmetrics"}

if !slices.Contains(validFormats, *f.PushGateway.Format) {
return fmt.Errorf("'--push-gtwy-format' must be one of: %s", strings.Join(validFormats, ", "))
}
}

return nil
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/prom.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ func newBasicAuth() BasicAuth {
type PushGateway struct {
URL *string
BasicAuth BasicAuth
Format *string
}

func newPushGateway() *PushGateway {
return &PushGateway{
URL: strPtr(""),
BasicAuth: newBasicAuth(),
Format: strPtr("textplain"),
}
}
31 changes: 29 additions & 2 deletions pkg/popeye.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ import (
"errors"
"fmt"
"io"
"maps"
"net/http"
"os"
"path/filepath"
"slices"
"strings"
"time"

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

validFormats := map[string]expfmt.Format{
"protocompact": expfmt.NewFormat(expfmt.TypeProtoCompact),
"protodelim": expfmt.NewFormat(expfmt.TypeProtoDelim),
"prototext": expfmt.NewFormat(expfmt.TypeProtoText),
"textplain": expfmt.NewFormat(expfmt.TypeTextPlain),
"openmetrics": expfmt.NewFormat(expfmt.TypeOpenMetrics),
}

format := validFormats["textplain"]
if config.IsStrSet(p.flags.PushGateway.Format) {
if f, exists := validFormats[*p.flags.PushGateway.Format]; exists {
format = f
} else {
validFormatsList := slices.Collect(maps.Keys(validFormats))
return fmt.Errorf(
"'--push-gtwy-format' must be one of: %s",
strings.Join(validFormatsList, ", "),
)
}
}

pusher := p.builder.ToPrometheus(
p.flags.PushGateway,
instance,
p.client().ActiveNamespace(),
asset,
p.codes.Glossary,
)
// Enable saving to file

pusher = pusher.Format(format)
// Persist is used when prometheus output format is selected...
// ...custom p.Do func intercepts push output and prints it...
// ...to stdout, while replying to pusher with stub HTTP 200
if persist {
pusher = pusher.Client(p)
pusher = pusher.Format(expfmt.NewFormat(expfmt.TypeTextPlain))
}

return pusher.AddContext(ctx)
Expand Down