Skip to content

Commit

Permalink
add flags to enable alert logging level (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
waynepeking348 authored Jul 25, 2023
1 parent c8b2187 commit dfad0ba
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 4 deletions.
4 changes: 4 additions & 0 deletions cmd/base/options/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type GenericOptions struct {

qosOptions *QoSOptions
metricsOptions *MetricsOptions
logsOptions *LogsOptions

componentbaseconfig.ClientConnectionConfiguration
}
Expand All @@ -57,6 +58,7 @@ func NewGenericOptions() *GenericOptions {
GenericEndpoint: ":9316",
qosOptions: NewQoSOptions(),
metricsOptions: NewMetricsOptions(),
logsOptions: NewLogsOptions(),
GenericEndpointHandleChains: []string{process.HTTPChainRateLimiter},
}
}
Expand Down Expand Up @@ -91,6 +93,7 @@ func (o *GenericOptions) AddFlags(fss *cliflag.NamedFlagSets) {

o.qosOptions.AddFlags(fs)
o.metricsOptions.AddFlags(fs)
o.logsOptions.AddFlags(fs)

fs.Float32Var(&o.QPS, "kube-api-qps", o.QPS, "QPS to use while talking with kubernetes apiserver.")
fs.Int32Var(&o.Burst, "kube-api-burst", o.Burst, "Burst to use while talking with kubernetes apiserver.")
Expand All @@ -110,6 +113,7 @@ func (o *GenericOptions) ApplyTo(c *generic.GenericConfiguration) error {
errList := make([]error, 0, 1)
errList = append(errList, o.qosOptions.ApplyTo(c.QoSConfiguration))
errList = append(errList, o.metricsOptions.ApplyTo(c.MetricsConfiguration))
errList = append(errList, o.logsOptions.ApplyTo())

c.ClientConnection.QPS = o.QPS
c.ClientConnection.Burst = o.Burst
Expand Down
43 changes: 43 additions & 0 deletions cmd/base/options/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright 2022 The Katalyst Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package options

import (
"github.com/spf13/pflag"

"github.com/kubewharf/katalyst-core/pkg/util/general"
)

type LogsOptions struct {
LogPackageLevel general.LoggingPKG
}

func NewLogsOptions() *LogsOptions {
return &LogsOptions{
LogPackageLevel: general.LoggingPKGFull,
}
}

// AddFlags adds flags to the specified FlagSet.
func (o *LogsOptions) AddFlags(fs *pflag.FlagSet) {
fs.Var(&o.LogPackageLevel, "logs-package-level", "the default package level for logging")
}

func (o *LogsOptions) ApplyTo() error {
general.SetDefaultLoggingPackage(o.LogPackageLevel)
return nil
}
4 changes: 2 additions & 2 deletions pkg/metrics/metrics-pool/otel_prom_metrics_mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import (
"time"

"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"

"github.com/kubewharf/katalyst-core/pkg/config/generic"
"github.com/kubewharf/katalyst-core/pkg/metrics"
"github.com/kubewharf/katalyst-core/pkg/util/general"
)

type PrometheusMetricOptions struct {
Expand Down Expand Up @@ -91,7 +91,7 @@ func (m *openTelemetryPrometheusMetricsEmitterPool) GetMetricsEmitter(para inter
return nil, err
}
m.emitters[pathName] = e
klog.Infof("add path %s to metric emitter", pathName)
general.Infof("add path %s to metric emitter", pathName)
}
m.started[pathName] = false
return m.emitters[pathName], nil
Expand Down
34 changes: 32 additions & 2 deletions pkg/util/general/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,51 @@ package general
import (
"fmt"
"runtime"
"strconv"
"strings"
"sync"

"k8s.io/klog/v2"
)

type LoggingPKG int

const callDepth = 3
func (l *LoggingPKG) Type() string {
return "LoggingPKG"
}

func (l *LoggingPKG) String() string {
return fmt.Sprintf("logging-level: %v", *l)
}

func (l *LoggingPKG) Set(value string) error {
level, err := strconv.Atoi(value)
if err != nil {
return err
}
*l = LoggingPKG(level)
return nil
}

const (
LoggingPKGNone LoggingPKG = iota
LoggingPKGShort
LoggingPKGFull
)

var defaultLoggingPackage = LoggingPKGFull
var defaultLoggingOnce sync.Once

// SetDefaultLoggingPackage should only be called by flags,
// and should not be alerted dynamically.
func SetDefaultLoggingPackage(l LoggingPKG) {
defaultLoggingOnce.Do(func() {
defaultLoggingPackage = l
})
}

const callDepth = 3

const skippedPackagePrefix = "github.com/kubewharf/"

// loggingWithDepth returns the logging-prefix for caller.
Expand Down Expand Up @@ -67,7 +97,7 @@ func loggingWithDepth(pkg LoggingPKG) string {
}

func logging(message string, params ...interface{}) string {
return "[" + loggingWithDepth(LoggingPKGFull) + "] " + fmt.Sprintf(message, params...)
return "[" + loggingWithDepth(defaultLoggingPackage) + "] " + fmt.Sprintf(message, params...)
}

func loggingPath(pkg LoggingPKG, message string, params ...interface{}) string {
Expand Down

0 comments on commit dfad0ba

Please sign in to comment.