-
Notifications
You must be signed in to change notification settings - Fork 64
/
integrations_gke.go
221 lines (184 loc) · 6.11 KB
/
integrations_gke.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"github.com/getsentry/sentry-go"
"github.com/rs/zerolog"
)
type IntegrationGKE struct {
clusterLocation string
clusterName string
projectName string
clusterURL string
_initialized bool
}
func GetIntegrationGKE() *IntegrationGKE {
return &instanceIntegrationGKE
}
var instanceIntegrationGKE = IntegrationGKE{_initialized: false}
const instanceMetadataURL = "http://metadata.google.internal/computeMetadata/v1/instance/attributes/?recursive=true"
const projectMetadataURL = "http://metadata.google.internal/computeMetadata/v1/project/?recursive=true"
type InstanceMetadata struct {
// Allow both types of casing for compatibility
ClusterName1 string `json:"cluster-name"`
ClusterName2 string `json:"clusterName"`
ClusterLocation1 string `json:"cluster-location"`
ClusterLocation2 string `json:"clusterLocation"`
}
type ProjectMetadata struct {
ProjectID1 string `json:"project-id"`
ProjectID2 string `json:"projectId"`
NumericProjectID1 int `json:"numeric-project-id"`
NumericProjectID2 int `json:"numericProjectId"`
}
func getGkeLogger() *zerolog.Logger {
_, logger := getLoggerWithTag(context.Background(), "integration", "gke")
return logger
}
func (im *InstanceMetadata) ClusterName() string {
if im.ClusterName1 != "" {
return im.ClusterName1
}
return im.ClusterName2
}
func (im *InstanceMetadata) ClusterLocation() string {
if im.ClusterLocation1 != "" {
return im.ClusterLocation1
}
return im.ClusterLocation2
}
func (pm *ProjectMetadata) ProjectID() string {
if pm.ProjectID1 != "" {
return pm.ProjectID1
}
return pm.ProjectID2
}
func (pm *ProjectMetadata) NumericProjectID() int {
if pm.NumericProjectID1 != 0 {
return pm.NumericProjectID1
}
return pm.NumericProjectID2
}
func getClusterURL(clusterLocation string, clusterName string, projectID string) string {
if clusterLocation == "" || clusterName == "" || projectID == "" {
return ""
}
return fmt.Sprintf(
"https://console.cloud.google.com/kubernetes/clusters/details/%s/%s/details?project=%s",
clusterLocation,
clusterName,
projectID,
)
}
func readGoogleMetadata(url string, output interface{}) error {
client := http.Client{}
req, _ := http.NewRequest("GET", url, nil)
req.Header = http.Header{
"Metadata-Flavor": {"Google"},
}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("cannot fetch metadata: %v", err)
}
defer resp.Body.Close()
if err = json.NewDecoder(resp.Body).Decode(output); err != nil {
return fmt.Errorf("cannot decode metadata: %v", err)
}
return nil
}
func (igke *IntegrationGKE) IsEnabled() bool {
return isTruthy(os.Getenv("SENTRY_K8S_INTEGRATION_GKE_ENABLED"))
}
func (igke *IntegrationGKE) IsInitialized() bool {
return igke._initialized
}
func (igke *IntegrationGKE) Init() error {
logger := getGkeLogger()
logger.Info().Msg("Initializing GKE integration")
// Instance metadata
var instanceMeta InstanceMetadata
err := readGoogleMetadata(instanceMetadataURL, &instanceMeta)
if err != nil {
return fmt.Errorf("error initializing GKE integration: %v", err)
}
igke.clusterName = instanceMeta.ClusterName()
igke.clusterLocation = instanceMeta.ClusterLocation()
// Project metadata
var projectMeta ProjectMetadata
err = readGoogleMetadata(projectMetadataURL, &projectMeta)
if err != nil {
return fmt.Errorf("error initializing GKE integration: %v", err)
}
igke.projectName = projectMeta.ProjectID()
igke.clusterURL = getClusterURL(
igke.clusterLocation, igke.clusterName, igke.projectName,
)
igke._initialized = true
return nil
}
func (igke *IntegrationGKE) GetContext() (string, sentry.Context, error) {
if !igke._initialized {
return "", sentry.Context{}, fmt.Errorf("running GetContext on a non-initialized integration")
}
gkeContext := sentry.Context{
"Cluster name": igke.clusterName,
"Cluster location": igke.clusterLocation,
"GCP project": igke.projectName,
}
if igke.clusterURL != "" {
gkeContext["Cluster URL"] = igke.clusterURL
}
return "Google Kubernetes Engine", gkeContext, nil
}
func (igke *IntegrationGKE) GetTags() (map[string]string, error) {
if !igke._initialized {
return nil, fmt.Errorf("running GetTags on a non-initialized integration")
}
res := map[string]string{
"gke_cluster_name": igke.clusterName,
"gke_cluster_location": igke.clusterLocation,
"gke_project_name": igke.projectName,
}
return res, nil
}
func (igke *IntegrationGKE) getLinkToPodLogs(podName string, namespace string) (string, error) {
if !igke._initialized {
return "", fmt.Errorf("the integration is not initialized")
}
projectName := igke.projectName
clusterName := igke.clusterName
clusterLocation := igke.clusterLocation
if podName == "" || namespace == "" || projectName == "" || clusterName == "" || clusterLocation == "" {
return "", nil
}
link := ("https://console.cloud.google.com/logs/query;query=" +
fmt.Sprintf("resource.type%%3D%%22k8s_container%%22%%0A") +
fmt.Sprintf("resource.labels.project_id%%3D%%22%s%%22%%0A", projectName) +
fmt.Sprintf("resource.labels.location%%3D%%22%s%%22%%0A", clusterLocation) +
fmt.Sprintf("resource.labels.cluster_name%%3D%%22%s%%22%%0A", clusterName) +
fmt.Sprintf("resource.labels.namespace_name%%3D%%22%s%%22%%0A", namespace) +
fmt.Sprintf("resource.labels.pod_name%%3D%%22%s%%22%%0A", podName) +
fmt.Sprintf(";duration=PT1H?project=%s", projectName))
return link, nil
}
func addPodLogLinkToGKEContext(ctx context.Context, scope *sentry.Scope, podName string, namespace string) {
logger := zerolog.Ctx(ctx)
gkeIntegration := GetIntegrationGKE()
if !gkeIntegration.IsEnabled() || !gkeIntegration.IsInitialized() {
logger.Debug().Msgf("The GKE integration is not enabled or initialized, so not adding/modifying the context")
return
}
logLink, err := gkeIntegration.getLinkToPodLogs(podName, namespace)
if logLink != "" && err == nil {
contextName, gkeContext, err := gkeIntegration.GetContext()
if err != nil {
logger.Debug().Msgf("Cannot get the context for GKE integration: %v", err)
return
}
gkeContext["Pod Logs"] = logLink
scope.SetContext(contextName, gkeContext)
}
}