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

Support structured logging #202

Merged
merged 1 commit into from
Sep 13, 2023
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ spec:

* `--http-endpoint <endpoint>`: The TCP network address where the HTTP server for diagnostics, including CSI driver health check and metrics. The default is empty string, which means the server is disabled.

* All glog / klog arguments are supported, such as `-v <log level>` or `-alsologtostderr`.
* [Arguments set by the `k8s.io/component-base/logs` package for klog](https://github.com/kubernetes/component-base/blob/v0.28.0-rc.0/logs/api/v1/options.go#L337-L355) are supported, such as `--v <log level>` and `--logging-format <log format>`.

## Community, discussion, contribution, and support

Expand Down
49 changes: 32 additions & 17 deletions cmd/livenessprobe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ import (
"google.golang.org/grpc"
"k8s.io/klog/v2"

"k8s.io/component-base/featuregate"
"k8s.io/component-base/logs"
logsapi "k8s.io/component-base/logs/api/v1"
_ "k8s.io/component-base/logs/json/register"

connlib "github.com/kubernetes-csi/csi-lib-utils/connection"
"github.com/kubernetes-csi/csi-lib-utils/metrics"
"github.com/kubernetes-csi/csi-lib-utils/rpc"
Expand Down Expand Up @@ -61,30 +66,30 @@ func (h *healthProbe) checkProbe(w http.ResponseWriter, req *http.Request) {
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
klog.Errorf("failed to establish connection to CSI driver: %v", err)
klog.ErrorS(err, "Failed to establish connection to CSI driver")
return
}
defer conn.Close()

klog.V(5).Infof("Sending probe request to CSI driver %q", h.driverName)
klog.V(5).InfoS("Sending probe request to CSI driver", "driver", h.driverName)
ready, err := rpc.Probe(ctx, conn)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
klog.Errorf("health check failed: %v", err)
klog.ErrorS(err, "Health check failed")
return
}

if !ready {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("driver responded but is not ready"))
klog.Error("driver responded but is not ready")
klog.ErrorS(nil, "Driver responded but is not ready")
return
}

w.WriteHeader(http.StatusOK)
w.Write([]byte(`ok`))
klog.V(5).Infof("Health check succeeded")
klog.V(5).InfoS("Health check succeeded")
}

// acquireConnection wraps the connlib.Connect but adding support to context
Expand Down Expand Up @@ -119,16 +124,23 @@ func acquireConnection(ctx context.Context, metricsManager metrics.CSIMetricsMan
}

func main() {
klog.InitFlags(nil)
flag.Set("logtostderr", "true")
fg := featuregate.NewFeatureGate()
mauriciopoppe marked this conversation as resolved.
Show resolved Hide resolved
logsapi.AddFeatureGates(fg)
c := logsapi.NewLoggingConfiguration()
logsapi.AddGoFlags(c, flag.CommandLine)
logs.InitLogs()
flag.Parse()
if err := logsapi.ValidateAndApply(c, fg); err != nil {
klog.ErrorS(err, "LoggingConfiguration is invalid")
os.Exit(1)
}

if *healthzPort != defaultHealthzPort && *httpEndpoint != "" {
klog.Error("only one of `--health-port` and `--http-endpoint` can be explicitly set.")
klog.ErrorS(nil, "Only one of `--health-port` and `--http-endpoint` can be explicitly set")
os.Exit(1)
}
if *metricsAddress != "" && *httpEndpoint != "" {
klog.Error("only one of `--metrics-address` and `--http-endpoint` can be explicitly set.")
klog.ErrorS(nil, "Only one of `--metrics-address` and `--http-endpoint` can be explicitly set")
os.Exit(1)
}
var addr string
Expand All @@ -143,16 +155,18 @@ func main() {
if err != nil {
// connlib should retry forever so a returned error should mean
// the grpc client is misconfigured rather than an error on the network
klog.Fatalf("failed to establish connection to CSI driver: %v", err)
klog.ErrorS(err, "Failed to establish connection to CSI driver")
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}

klog.Infof("calling CSI driver to discover driver name")
klog.InfoS("Calling CSI driver to discover driver name")
csiDriverName, err := rpc.GetDriverName(context.Background(), csiConn)
csiConn.Close()
if err != nil {
klog.Fatalf("failed to get CSI driver name: %v", err)
klog.ErrorS(err, "Failed to get CSI driver name")
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
klog.Infof("CSI driver name: %q", csiDriverName)
klog.InfoS("CSI driver name", "driver", csiDriverName)

hp := &healthProbe{
driverName: csiDriverName,
Expand All @@ -171,18 +185,19 @@ func main() {
metricsMux := http.NewServeMux()
metricsManager.RegisterToServer(metricsMux, *metricsPath)
go func() {
klog.Infof("Separate metrics ServeMux listening at %q", *metricsAddress)
klog.InfoS("Separate metrics ServeMux listening", "address", *metricsAddress)
err := http.ListenAndServe(*metricsAddress, metricsMux)
if err != nil {
klog.Fatalf("Failed to start prometheus metrics endpoint on specified address (%q) and path (%q): %s", *metricsAddress, *metricsPath, err)
klog.ErrorS(err, "Failed to start prometheus metrics endpoint on specified address and path", "addr", *metricsAddress, "path", *metricsPath)
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
}()
}

mux.HandleFunc("/healthz", hp.checkProbe)
klog.Infof("ServeMux listening at %q", addr)
klog.InfoS("ServeMux listening", "address", addr)
err = http.ListenAndServe(addr, mux)
if err != nil {
klog.Fatalf("failed to start http server with error: %v", err)
klog.ErrorS(err, "Failed to start http server")
}
}
20 changes: 19 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/kubernetes-csi/csi-lib-utils v0.14.0
github.com/kubernetes-csi/csi-test/v5 v5.0.0
google.golang.org/grpc v1.58.0
k8s.io/component-base v0.28.0
k8s.io/klog/v2 v2.100.1
)

Expand All @@ -16,18 +17,35 @@ require (
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/zapr v1.2.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.10.1 // indirect
github.com/spf13/cobra v1.7.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.19.0 // indirect
golang.org/x/net v0.13.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/apimachinery v0.28.0 // indirect
k8s.io/component-base v0.28.0 // indirect
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
)
Loading