Skip to content

Commit

Permalink
Fix Deployment Details Bug (#699)
Browse files Browse the repository at this point in the history
* Improve loading of deployment details

* Hide missing deployment details labels

* Load deployment details once via init()

* Add comment about go loadDeploymentDetails()

* Simplify deployment_details.go

* Remove getDeploymentDetailsIfLoaded() function

Co-authored-by: Shabir Mohamed Abdul Samadh <[email protected]>
  • Loading branch information
NimJay and Shabirmean authored Feb 2, 2022
1 parent 1c558c7 commit 6b25a1a
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 42 deletions.
64 changes: 64 additions & 0 deletions src/frontend/deployment_details.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"net/http"
"os"
"time"

"cloud.google.com/go/compute/metadata"
"github.com/sirupsen/logrus"
)

var deploymentDetailsMap map[string]string
var log *logrus.Logger

func init() {
initializeLogger()
// Use a goroutine to ensure loadDeploymentDetails()'s GCP API
// calls don't block non-GCP deployments. See issue #685.
go loadDeploymentDetails()
}

func initializeLogger() {
log = logrus.New()
log.Level = logrus.DebugLevel
log.Formatter = &logrus.JSONFormatter{
FieldMap: logrus.FieldMap{
logrus.FieldKeyTime: "timestamp",
logrus.FieldKeyLevel: "severity",
logrus.FieldKeyMsg: "message",
},
TimestampFormat: time.RFC3339Nano,
}
log.Out = os.Stdout
}

func loadDeploymentDetails() {
deploymentDetailsMap = make(map[string]string)
var metaServerClient = metadata.NewClient(&http.Client{})

podHostname, err := os.Hostname()
if err != nil {
log.Error("Failed to fetch the hostname for the Pod", err)
}

podCluster, err := metaServerClient.InstanceAttributeValue("cluster-name")
if err != nil {
log.Error("Failed to fetch the name of the cluster in which the pod is running", err)
}

podZone, err := metaServerClient.Zone()
if err != nil {
log.Error("Failed to fetch the Zone of the node where the pod is scheduled", err)
}

deploymentDetailsMap["HOSTNAME"] = podHostname
deploymentDetailsMap["CLUSTERNAME"] = podCluster
deploymentDetailsMap["ZONE"] = podZone

log.WithFields(logrus.Fields{
"cluster": podCluster,
"zone": podZone,
"hostname": podHostname,
}).Debug("Loaded deployment details")
}
44 changes: 5 additions & 39 deletions src/frontend/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"strings"
"time"

"cloud.google.com/go/compute/metadata"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -116,7 +115,7 @@ func (fe *frontendServer) homeHandler(w http.ResponseWriter, r *http.Request) {
"platform_css": plat.css,
"platform_name": plat.provider,
"is_cymbal_brand": isCymbalBrand,
"deploymentDetails": getDeploymentDetails(r),
"deploymentDetails": deploymentDetailsMap,
}); err != nil {
log.Error(err)
}
Expand Down Expand Up @@ -201,7 +200,7 @@ func (fe *frontendServer) productHandler(w http.ResponseWriter, r *http.Request)
"platform_css": plat.css,
"platform_name": plat.provider,
"is_cymbal_brand": isCymbalBrand,
"deploymentDetails": getDeploymentDetails(r),
"deploymentDetails": deploymentDetailsMap,
}); err != nil {
log.Println(err)
}
Expand Down Expand Up @@ -313,7 +312,7 @@ func (fe *frontendServer) viewCartHandler(w http.ResponseWriter, r *http.Request
"platform_css": plat.css,
"platform_name": plat.provider,
"is_cymbal_brand": isCymbalBrand,
"deploymentDetails": getDeploymentDetails(r),
"deploymentDetails": deploymentDetailsMap,
}); err != nil {
log.Println(err)
}
Expand Down Expand Up @@ -386,7 +385,7 @@ func (fe *frontendServer) placeOrderHandler(w http.ResponseWriter, r *http.Reque
"platform_css": plat.css,
"platform_name": plat.provider,
"is_cymbal_brand": isCymbalBrand,
"deploymentDetails": getDeploymentDetails(r),
"deploymentDetails": deploymentDetailsMap,
}); err != nil {
log.Println(err)
}
Expand Down Expand Up @@ -448,7 +447,7 @@ func renderHTTPError(log logrus.FieldLogger, r *http.Request, w http.ResponseWri
"error": errMsg,
"status_code": code,
"status": http.StatusText(code),
"deploymentDetails": getDeploymentDetails(r),
"deploymentDetails": deploymentDetailsMap,
}); templateErr != nil {
log.Println(templateErr)
}
Expand Down Expand Up @@ -517,36 +516,3 @@ func stringinSlice(slice []string, val string) bool {
}
return false
}

func getDeploymentDetails(httpRequest *http.Request) map[string]string {
var deploymentDetailsMap = make(map[string]string)
var metaServerClient = metadata.NewClient(&http.Client{})
var log = httpRequest.Context().Value(ctxKeyLog{}).(logrus.FieldLogger)

podHostname, err := os.Hostname()
if err != nil {
log.Error("Failed to fetch the hostname for the Pod", err)
}

podCluster, err := metaServerClient.InstanceAttributeValue("cluster-name")
if err != nil {
log.Error("Failed to fetch the name of the cluster in which the pod is running", err)
}

podZone, err := metaServerClient.Zone()
if err != nil {
log.Error("Failed to fetch the Zone of the node where the pod is scheduled", err)
}

deploymentDetailsMap["HOSTNAME"] = podHostname
deploymentDetailsMap["CLUSTERNAME"] = podCluster
deploymentDetailsMap["ZONE"] = podZone

log.WithFields(logrus.Fields{
"cluster": podCluster,
"zone": podZone,
"hostname": podHostname,
}).Debug("Fetched pod details")

return deploymentDetailsMap
}
15 changes: 12 additions & 3 deletions src/frontend/templates/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,18 @@
<br/>
<small>
{{ if $.deploymentDetails }}
<b>Cluster: </b>{{index .deploymentDetails "CLUSTERNAME" }}<br/>
<b>Zone: </b>{{index .deploymentDetails "ZONE" }}<br/>
<b>Pod: </b>{{index .deploymentDetails "HOSTNAME" }}
{{ if index .deploymentDetails "CLUSTERNAME" }}
<b>Cluster: </b>{{ index .deploymentDetails "CLUSTERNAME" }}<br/>
{{ end }}
{{ if index .deploymentDetails "ZONE" }}
<b>Zone: </b>{{ index .deploymentDetails "ZONE" }}<br/>
{{ end }}
{{ if index .deploymentDetails "HOSTNAME" }}
<b>Pod: </b>{{ index .deploymentDetails "HOSTNAME" }}
{{ end }}
{{ else }}
Deployment details are still loading.
Try refreshing this page.
{{ end }}
</small>
</p>
Expand Down

0 comments on commit 6b25a1a

Please sign in to comment.