Skip to content

Commit 5d8a321

Browse files
committed
feat: new promslog/flag package to compliment promslog
Prereq for prometheus/prometheus#14355 This adds a new `promslog/flag` package to be used with the new promslog package. It uses the same flag structure for a transition that is hopefully easy for end users. Signed-off-by: TJ Hoplock <[email protected]>
1 parent 0c6d887 commit 5d8a321

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

promlog/flag/flag.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14+
// Package flag defines standardised flag interactions for use with promlog
15+
// across Prometheus components.
16+
// It should typically only ever be imported by main packages.
17+
18+
// Deprecated: This package has been replaced with github.com/prometheus/common/promslog/flag.
1419
package flag
1520

1621
import (

promslog/flag/flag.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2024 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
// Package flag defines standardised flag interactions for use with promslog
15+
// across Prometheus components.
16+
// It should typically only ever be imported by main packages.
17+
18+
package flag
19+
20+
import (
21+
"strings"
22+
23+
kingpin "github.com/alecthomas/kingpin/v2"
24+
25+
"github.com/prometheus/common/promslog"
26+
)
27+
28+
// LevelFlagName is the canonical flag name to configure the allowed log level
29+
// within Prometheus projects.
30+
const LevelFlagName = "log.level"
31+
32+
// LevelFlagHelp is the help description for the log.level flag.
33+
var LevelFlagHelp = "Only log messages with the given severity or above. One of: [" + strings.Join(promslog.LevelFlagOptions, ", ") + "]"
34+
35+
// FormatFlagName is the canonical flag name to configure the log format
36+
// within Prometheus projects.
37+
const FormatFlagName = "log.format"
38+
39+
// FormatFlagHelp is the help description for the log.format flag.
40+
var FormatFlagHelp = "Output format of log messages. One of: [" + strings.Join(promslog.FormatFlagOptions, ", ") + "]"
41+
42+
// AddFlags adds the flags used by this package to the Kingpin application.
43+
// To use the default Kingpin application, call AddFlags(kingpin.CommandLine)
44+
func AddFlags(a *kingpin.Application, config *promslog.Config) {
45+
config.Level = &promslog.AllowedLevel{}
46+
a.Flag(LevelFlagName, LevelFlagHelp).
47+
Default("info").HintOptions(promslog.LevelFlagOptions...).
48+
SetValue(config.Level)
49+
50+
config.Format = &promslog.AllowedFormat{}
51+
a.Flag(FormatFlagName, FormatFlagHelp).
52+
Default("logfmt").HintOptions(promslog.FormatFlagOptions...).
53+
SetValue(config.Format)
54+
}

0 commit comments

Comments
 (0)