Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(logging): migration to structure logging for main.go, pkg/ & internal/ folders #1807

Merged
merged 5 commits into from
Aug 16, 2022
Merged
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
8 changes: 4 additions & 4 deletions internal/store/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (b *Builder) WithCustomResourceStoreFactories(fs ...customresource.Registry
for i := range fs {
f := fs[i]
if _, ok := availableStores[f.Name()]; ok {
klog.Warningf("The internal resource store named %s already exists and is overridden by a custom resource store with the same name, please make sure it meets your expectation", f.Name())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this because we don't have a way to log warn messages with structure logging?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that does not exist there. See also: kubernetes/klog#184

klog.InfoS("The internal resource store already exists and is overridden by a custom resource store with the same name, please make sure it meets your expectation", "registryName", f.Name())
}
availableStores[f.Name()] = func(b *Builder) []cache.Store {
return b.buildCustomResourceStoresFunc(
Expand Down Expand Up @@ -230,7 +230,7 @@ func (b *Builder) Build() []metricsstore.MetricsWriter {
}
}

klog.Infof("Active resources: %s", strings.Join(activeStoreNames, ","))
klog.InfoS("Active resources", "activeStoreNames", strings.Join(activeStoreNames, ","))

return metricsWriters
}
Expand All @@ -255,7 +255,7 @@ func (b *Builder) BuildStores() [][]cache.Store {
}
}

klog.Infof("Active resources: %s", strings.Join(activeStoreNames, ","))
klog.InfoS("Active resources", "activeStoreNames", strings.Join(activeStoreNames, ","))

return allStores
}
Expand Down Expand Up @@ -483,7 +483,7 @@ func (b *Builder) buildCustomResourceStores(resourceName string,

customResourceClient, ok := b.customResourceClients[resourceName]
if !ok {
klog.Warningf("Custom resource client %s does not exist", resourceName)
klog.InfoS("Custom resource client does not exist", "resourceName", resourceName)
return []cache.Store{}
}

Expand Down
12 changes: 8 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ func main() {
opts.AddFlags()

if err := opts.Parse(); err != nil {
klog.Fatalf("Parsing flag definitions error: %v", err)
klog.ErrorS(err, "Parsing flag definitions error")
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}

if opts.Version {
Expand All @@ -55,14 +56,16 @@ func main() {
if config, set := resolveCustomResourceConfig(opts); set {
crf, err := customresourcestate.FromConfig(config)
if err != nil {
klog.Fatal(err)
klog.ErrorS(err, "Parsing from Custom Resource State Metrics file failed")
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
factories = append(factories, crf...)
}

ctx := context.Background()
if err := app.RunKubeStateMetrics(ctx, opts, factories...); err != nil {
klog.Fatalf("Failed to run kube-state-metrics: %v", err)
klog.ErrorS(err, "Failed to run kube-state-metrics")
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
}

Expand All @@ -73,7 +76,8 @@ func resolveCustomResourceConfig(opts *options.Options) (customresourcestate.Con
if file := opts.CustomResourceConfigFile; file != "" {
f, err := os.Open(file)
if err != nil {
klog.Fatal(err)
klog.ErrorS(err, "Custom Resource State Metrics file could not be opened")
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
return yaml.NewDecoder(f), true
}
Expand Down
21 changes: 10 additions & 11 deletions pkg/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options, factories .

var resources []string
if len(opts.Resources) == 0 {
klog.Info("Using default resources")
klog.InfoS("Used default resources")
resources = options.DefaultResources.AsSlice()
// enable custom resource
for _, factory := range factories {
resources = append(resources, factory.Name())
}
} else {
klog.Infof("Using resources %s", opts.Resources.String())
klog.InfoS("Used resources", "resources", opts.Resources.String())
resources = opts.Resources.AsSlice()
}

Expand All @@ -119,15 +119,15 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options, factories .
return fmt.Errorf("error initializing the allowdeny list: %v", err)
}

klog.Infof("Metric allow-denylisting: %v", allowDenyList.Status())
klog.InfoS("Metric allow-denylisting", "allowDenyStatus", allowDenyList.Status())

optInMetricFamilyFilter, err := optin.NewMetricFamilyFilter(opts.MetricOptInList)
if err != nil {
return fmt.Errorf("error initializing the opt-in metric list: %v", err)
}

if optInMetricFamilyFilter.Count() > 0 {
klog.Infof("Metrics which were opted into: %v", optInMetricFamilyFilter.Status())
klog.InfoS("Metrics which were opted into", "optInMetricsFamilyStatus", optInMetricFamilyFilter.Status())
}

storeBuilder.WithFamilyGeneratorFilter(generator.NewCompositeFamilyGeneratorFilter(
Expand Down Expand Up @@ -188,7 +188,7 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options, factories .
// Run Telemetry server
{
g.Add(func() error {
klog.Infof("Starting kube-state-metrics self metrics server: %s", telemetryListenAddress)
klog.InfoS("Started kube-state-metrics self metrics server", "telemetryAddress", telemetryListenAddress)
return web.ListenAndServe(&telemetryServer, tlsConfig, promLogger)
}, func(error) {
ctxShutDown, cancel := context.WithTimeout(ctx, 3*time.Second)
Expand All @@ -199,7 +199,7 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options, factories .
// Run Metrics server
{
g.Add(func() error {
klog.Infof("Starting metrics server: %s", metricsServerListenAddress)
klog.InfoS("Started metrics server", "metricsServerAddress", metricsServerListenAddress)
return web.ListenAndServe(&metricsServer, tlsConfig, promLogger)
}, func(error) {
ctxShutDown, cancel := context.WithTimeout(ctx, 3*time.Second)
Expand All @@ -211,7 +211,7 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options, factories .
if err := g.Run(); err != nil {
return fmt.Errorf("run server group error: %v", err)
}
klog.Info("Exiting")
klog.InfoS("Exited")
return nil
}

Expand Down Expand Up @@ -247,14 +247,13 @@ func createKubeClient(apiserver string, kubeconfig string, factories ...customre
// Informers don't seem to do a good job logging error messages when it
// can't reach the server, making debugging hard. This makes it easier to
// figure out if apiserver is configured incorrectly.
klog.Infof("Testing communication with server")
klog.InfoS("Tested communication with server")
v, err := kubeClient.Discovery().ServerVersion()
if err != nil {
return nil, nil, nil, errors.Wrap(err, "error while trying to communicate with apiserver")
}
klog.Infof("Running with Kubernetes cluster version: v%s.%s. git version: %s. git tree state: %s. commit: %s. platform: %s",
v.Major, v.Minor, v.GitVersion, v.GitTreeState, v.GitCommit, v.Platform)
klog.Infof("Communication with server successful")
klog.InfoS("Run with Kubernetes cluster version", "major", v.Major, "minor", v.Minor, "gitVersion", v.GitVersion, "gitTreeState", v.GitTreeState, "gitCommit", v.GitCommit, "platform", v.Platform)
klog.InfoS("Communication with server successful")

return kubeClient, vpaClient, customResourceClients, nil
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/customresourcestate/registry_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ func compilePath(path []string) (out valuePath, _ error) {
}

func (s fieldMetrics) MetricFamilyGenerators(_, _ []string) (result []generator.FamilyGenerator) {
klog.Infof("custom resource state adding metrics: %v", s.names())
klog.InfoS("Custom resource state added metrics", "familyNames", s.names())
for _, f := range s.Families {
result = append(result, famGen(f))
}
Expand All @@ -461,7 +461,7 @@ func famGen(f compiledFamily) generator.FamilyGenerator {
}

func generate(u *unstructured.Unstructured, f compiledFamily, errLog klog.Verbose) *metric.Family {
klog.V(10).Infof("%s: checking %s", f.Name, u.GetName())
klog.V(10).InfoS("Checked", "compiledFamilyName", f.Name, "unstructuredName", u.GetName())
var metrics []*metric.Metric
baseLabels := f.BaseLabels(u.Object)
values, errors := f.Each.Values(u.Object)
Expand All @@ -474,7 +474,7 @@ func generate(u *unstructured.Unstructured, f compiledFamily, errLog klog.Verbos
v.DefaultLabels(baseLabels)
metrics = append(metrics, v.ToMetric())
}
klog.V(10).Infof("%s: produced %d metrics for %s", f.Name, len(metrics), u.GetName())
klog.V(10).InfoS("Produced metrics for", "compiledFamilyName", f.Name, "metricsLength", len(metrics), "unstructuredName", u.GetName())

return &metric.Family{
Metrics: metrics,
Expand Down
12 changes: 6 additions & 6 deletions pkg/metricshandler/metrics_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (m *MetricsHandler) ConfigureSharding(ctx context.Context, shard int32, tot
m.cancel()
}
if totalShards != 1 {
klog.Infof("configuring sharding of this instance to be shard index %d (zero-indexed) out of %d total shards", shard, totalShards)
klog.InfoS("Configuring sharding of this instance to be shard index (zero-indexed) out of total shards", "shard", shard, "totalShards", totalShards)
}
ctx, m.cancel = context.WithCancel(ctx)
m.storeBuilder.WithSharding(shard, totalShards)
Expand All @@ -93,14 +93,14 @@ func (m *MetricsHandler) Run(ctx context.Context) error {
autoSharding := len(m.opts.Pod) > 0 && len(m.opts.Namespace) > 0

if !autoSharding {
klog.Info("Autosharding disabled")
klog.InfoS("Autosharding disabled")
m.ConfigureSharding(ctx, m.opts.Shard, m.opts.TotalShards)
<-ctx.Done()
return ctx.Err()
}

klog.Infof("Autosharding enabled with pod=%v pod_namespace=%v", m.opts.Pod, m.opts.Namespace)
klog.Infof("Auto detecting sharding settings.")
klog.InfoS("Autosharding enabled with pod", "pod", klog.KRef(m.opts.Namespace, m.opts.Pod))
klog.InfoS("Auto detecting sharding settings")
ss, err := detectStatefulSet(m.kubeClient, m.opts.Pod, m.opts.Namespace)
if err != nil {
return errors.Wrap(err, "detect StatefulSet")
Expand All @@ -124,7 +124,7 @@ func (m *MetricsHandler) Run(ctx context.Context) error {

shard, totalShards, err := shardingSettingsFromStatefulSet(ss, m.opts.Pod)
if err != nil {
klog.Errorf("detect sharding settings from StatefulSet: %v", err)
klog.ErrorS(err, "Detected sharding settings from StatefulSet")
return
}

Expand All @@ -151,7 +151,7 @@ func (m *MetricsHandler) Run(ctx context.Context) error {

shard, totalShards, err := shardingSettingsFromStatefulSet(cur, m.opts.Pod)
if err != nil {
klog.Errorf("detect sharding settings from StatefulSet: %v", err)
klog.ErrorS(err, "Detected sharding settings from StatefulSet")
return
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/options/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,13 @@ func (n *NamespaceList) Set(value string) error {
func (n *NamespaceList) GetNamespaces() NamespaceList {
ns := *n
if len(*n) == 0 {
klog.Info("Using all namespace")
klog.InfoS("Using all namespaces")
ns = DefaultNamespaces
} else {
if n.IsAllNamespaces() {
klog.Info("Using all namespace")
klog.InfoS("Using all namespaces")
} else {
klog.Infof("Using %s namespaces", ns)
klog.InfoS("Using namespaces", "nameSpaces", ns)
}
}
return ns
Expand Down
6 changes: 3 additions & 3 deletions pkg/util/proc/reaper.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@ import (
// that has pid 1.
func StartReaper() {
if os.Getpid() == 1 {
klog.V(4).Infof("Launching reaper")
klog.V(4).InfoS("Launching reaper")
go func() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGCHLD)
for {
// Wait for a child to terminate
sig := <-sigs
klog.V(4).Infof("Signal received: %v", sig)
klog.V(4).InfoS("Signal received", "signal", sig)
for {
// Reap processes
cpid, _ := syscall.Wait4(-1, nil, syscall.WNOHANG, nil)
if cpid < 1 {
break
}

klog.V(4).Infof("Reaped process with pid %d", cpid)
klog.V(4).InfoS("Reaped process with pid", "cpid", cpid)
}
}
}()
Expand Down