forked from GoogleCloudPlatform/microservices-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployment_details.go
64 lines (53 loc) · 1.54 KB
/
deployment_details.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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")
}