GKE Structured log with in go with glog and logsrus

JSON logging on GKE with various golang logging libraries.

This is just a simple http application which emits JSON strings to stdout/stderr from go using

For each log type, there is an endpoint to invoke them where the various handler will emit a JSON string:

{
"str": "foo",
"num": 100,
"bool": false,
"null": null,
"array": [
"foo",
"bar",
"baz"
],
"obj": {
"a": 1,
"b": 2
}
}

References

Setup

gcloud container  clusters create cluster-1 --machine-type "n1-standard-2" \
--zone us-central1-a \
--num-nodes 2 --enable-ip-alias \
--cluster-version "1.19"
$ kubectl apply -f my-deployment.yaml -f my-srv.yaml$ kubectl get po,svc
NAME READY STATUS RESTARTS AGE
pod/myapp-deployment-6fcc7f9df9-ddls6 1/1 Running 0 81s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.10.32.1 <none> 443/TCP 6d11h
service/myapp-srv-lb LoadBalancer 10.10.43.107 34.122.242.147 8080:30970/TCP 81s

Once a network loadbalancer is setup, configure a firewall rule to allow you to access te servoce

gcloud compute  firewall-rules create allow-lb-requests \
--allow=tcp:8080 --network=default --source-ranges=0.0.0.0/0

Then invoke each endpoint

export L4=`kubectl get service/myapp-srv-lb -o jsonpath='{.status.loadBalancer.ingress[0].ip}'`
echo $L4
curl -s http://$L4:8080/fmt
curl -s http://$L4:8080/log
curl -s http://$L4:8080/glog
curl -s http://$L4:8080/logsrus

Note the output inside the jsonPayload struct (since w’ere outputting raw JSON to stdout/stderr)

fmt

Creates JSON and parses it as structured:

log

Creates JSON and parses it as structured:

glog

Creates JSON and but does NOT parse the inner json:

logsrus

Creates JSON and but does NOT parse the inner json:

— -

http_server.go

Dockerfile

Deployment.yaml