Skip to content

Commit

Permalink
slog instead of klog in flaky service (#1928)
Browse files Browse the repository at this point in the history
  • Loading branch information
kinjalh authored Feb 19, 2025
1 parent 96b0d7f commit 53e5ecc
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 38 deletions.
7 changes: 3 additions & 4 deletions go/demo/flaky-service/api/api_server.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package api

import (
"log/slog"
"net/http"

"github.com/pluralsh/console/go/demo/flaky-service/args"
"github.com/pluralsh/console/go/demo/flaky-service/internal/log"
"k8s.io/klog/v2"
)

func getHttpHandler(modifier args.BehaviorModifier, timestampModulus int64) func(http.ResponseWriter, *http.Request) {
Expand All @@ -20,10 +19,10 @@ func getHttpHandler(modifier args.BehaviorModifier, timestampModulus int64) func
}

func StartApiServer(address string, apiPath string, modifier args.BehaviorModifier, timestampModulus int64) {
klog.V(log.LogLevelMinimal).InfoS("Starting API server", "address", address, "apiPath", apiPath, "modifier", modifier)
slog.Info("Starting API server", "address", address, "apiPath", apiPath, "modifier", modifier)

http.HandleFunc(apiPath, getHttpHandler(modifier, timestampModulus))
if err := http.ListenAndServe(address, nil); err != nil {
klog.ErrorS(err, "Could not start API server")
slog.Error("Could not start API server", "error", err)
}
}
11 changes: 5 additions & 6 deletions go/demo/flaky-service/api/behavior_modifiers.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package api

import (
"log/slog"
"net/http"
"time"

"github.com/pluralsh/console/go/demo/flaky-service/internal/log"
"github.com/pluralsh/console/go/demo/flaky-service/metrics"
"k8s.io/klog/v2"
)

func HandleRequestDefault() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
klog.V(log.LogLevelMinimal).InfoS("Handling API request normally")
slog.Info("Handling API request normally")

metrics.IncrementRequestCounter(http.StatusOK, r.Method)
w.WriteHeader(http.StatusOK)
Expand All @@ -22,18 +21,18 @@ func HandleRequestDefault() http.HandlerFunc {

func HandleRequestTimestampModulus(timestampModulus int64) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
klog.V(log.LogLevelMinimal).InfoS("Handling API request using timestamp")
slog.Info("Handling API request using timestamp")
time_now := time.Now().UnixNano() / int64(time.Millisecond)

if time_now%timestampModulus == 0 {
klog.V(log.LogLevelMinimal).InfoS("Timestamp is multiple of modulus, returning status.InternalServerError", "time_now", time_now, "modulus", timestampModulus)
slog.Error("Timestamp is multiple of modulus, returning status.InternalServerError", "time_now", time_now, "modulus", timestampModulus)

metrics.IncrementRequestCounter(http.StatusInternalServerError, r.Method)
w.WriteHeader(http.StatusInternalServerError)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"message": "req failed"}`))
} else {
klog.V(log.LogLevelMinimal).InfoS("Timestamp is not multiple of modulus, returning status.OK", "time_now", time_now, "modulus", timestampModulus)
slog.Info("Timestamp is not multiple of modulus, returning status.OK", "time_now", time_now, "modulus", timestampModulus)

metrics.IncrementRequestCounter(http.StatusOK, r.Method)
w.WriteHeader(http.StatusOK)
Expand Down
2 changes: 1 addition & 1 deletion go/demo/flaky-service/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.23.4

require (
github.com/prometheus/client_golang v1.20.5
k8s.io/klog/v2 v2.130.1
golang.org/x/exp v0.0.0-20250218142911-aa4b98e5adaa
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go/demo/flaky-service/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ github.com/prometheus/common v0.55.0 h1:KEi6DK7lXW/m7Ig5i47x0vRzuBsHuvJdi5ee6Y3G
github.com/prometheus/common v0.55.0/go.mod h1:2SECS4xJG1kd8XF9IcM1gMX6510RAEL65zxzNImwdc8=
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
golang.org/x/exp v0.0.0-20250218142911-aa4b98e5adaa h1:t2QcU6V556bFjYgu4L6C+6VrCPyJZ+eyRsABUPs1mz4=
golang.org/x/exp v0.0.0-20250218142911-aa4b98e5adaa/go.mod h1:BHOTPb3L19zxehTsLoJXVaTktb06DFgmdW6Wb9s8jqk=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
Expand Down
15 changes: 0 additions & 15 deletions go/demo/flaky-service/internal/log/log.go

This file was deleted.

16 changes: 10 additions & 6 deletions go/demo/flaky-service/main.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package main

import (
"log/slog"
"os"

"github.com/pluralsh/console/go/demo/flaky-service/api"
"github.com/pluralsh/console/go/demo/flaky-service/args"
"github.com/pluralsh/console/go/demo/flaky-service/internal/log"
"github.com/pluralsh/console/go/demo/flaky-service/metrics"

"k8s.io/klog/v2"
)

func main() {
klog.V(log.LogLevelMinimal).Info("Starting flaky service")

args.Init()
klog.V(log.LogLevelMinimal).InfoS("args", "BehaviorModifer", args.ResponseBehaviorModifier(), "BehaviorModifierTimestampModulus", args.BehaviorModifierTimestampModulus())

handler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelInfo,
})
slog.SetDefault(slog.New(handler))

slog.Info("Starting flaky service", "BehaviorModifer", args.ResponseBehaviorModifier(), "BehaviorModifierTimestampModulus", args.BehaviorModifierTimestampModulus())

go api.StartApiServer(args.ApiAddress(), args.ApiPath(), args.ResponseBehaviorModifier(), args.BehaviorModifierTimestampModulus())
go metrics.StartMetricsServer(args.MetricsAddress(), args.MetricsPath())
Expand Down
10 changes: 4 additions & 6 deletions go/demo/flaky-service/metrics/prometheus_metric_server.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package metrics

import (
"log/slog"
"net/http"
"strconv"

"github.com/pluralsh/console/go/demo/flaky-service/internal/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"

"k8s.io/klog/v2"
)

var requestCounter = prometheus.NewCounterVec(
Expand All @@ -26,16 +24,16 @@ func IncrementRequestCounter(code int, method string) {
}

func handleRequest(w http.ResponseWriter, r *http.Request) {
klog.V(log.LogLevelMinimal).Info("Handling metrics request")
slog.Info("Handling metrics request")
promhttp.HandlerFor(registry, promhttp.HandlerOpts{}).ServeHTTP(w, r)
}

func StartMetricsServer(address string, metricsPath string) {
klog.V(log.LogLevelMinimal).InfoS("Starting metrics server", "address", address, "metricsPath", metricsPath)
slog.Info("Starting metrics server", "address", address, "metricsPath", metricsPath)

registry.MustRegister(requestCounter)
http.HandleFunc(metricsPath, handleRequest)
if err := http.ListenAndServe(address, nil); err != nil {
klog.ErrorS(err, "Could not start metrics server")
slog.Error("Could not start metrics server", "error", err)
}
}

0 comments on commit 53e5ecc

Please sign in to comment.