Skip to content

Commit

Permalink
richer logging
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Winther <jippignu@gmail.com>
  • Loading branch information
jippi committed Mar 14, 2018
1 parent 97c996f commit 7076d25
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions internal/http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ func StarServer() {
// handles: /{api_version}/meta-data/iam/info
// handles: /{api_version}/meta-data/iam/info/{junk}
func iamInfoHandler(w http.ResponseWriter, r *http.Request) {
log.Infof("Handling %s from %s", r.URL.String(), r.RemoteAddr)

// setup basic telemetry
vars := mux.Vars(r)
labels := []metrics.Label{
Expand All @@ -99,6 +97,8 @@ func iamInfoHandler(w http.ResponseWriter, r *http.Request) {
metrics.Label{Name: "request_path", Value: "/meta-data/iam/info"},
}

logWithLabels(labels).Infof("Handling %s from %s", r.URL.String(), remoteIP(r.RemoteAddr))

// ensure we got compatible api version
if !isCompatibleAPIVersion(r) {
logWithLabels(labels).Info("Request is using too old version of meta-data API, passing through directly")
Expand Down Expand Up @@ -147,17 +147,17 @@ func iamInfoHandler(w http.ResponseWriter, r *http.Request) {

// handles: /{api_version}/meta-data/iam/security-credentials/
func iamSecurityCredentialsName(w http.ResponseWriter, r *http.Request) {
log.Infof("Handling %s from %s", r.URL.String(), r.RemoteAddr)

// setup basic telemetry
vars := mux.Vars(r)
labels := []metrics.Label{
metrics.Label{Name: "api_version", Value: vars["api_version"]},
metrics.Label{Name: "handler_name", Value: "iam-security-credentials-name"},
metrics.Label{Name: "remote_addr", Value: r.RemoteAddr},
metrics.Label{Name: "remote_addr", Value: remoteIP(r.RemoteAddr)},
metrics.Label{Name: "request_path", Value: "/meta-data/iam/security-credentials/"},
}

logWithLabels(labels).Infof("Handling %s from %s", r.URL.String(), remoteIP(r.RemoteAddr))

// ensure we got compatible api version
if !isCompatibleAPIVersion(r) {
logWithLabels(labels).Info("Request is using too old version of meta-data API, passing through directly")
Expand Down Expand Up @@ -187,17 +187,16 @@ func iamSecurityCredentialsName(w http.ResponseWriter, r *http.Request) {

// handles: /{api_version}/meta-data/iam/security-credentials/{requested_role}
func iamSecurityCredentialsForRole(w http.ResponseWriter, r *http.Request) {
log.Infof("Handling %s from %s", r.URL.String(), r.RemoteAddr)

// setup basic telemetry
vars := mux.Vars(r)
labels := []metrics.Label{
metrics.Label{Name: "api_version", Value: vars["api_version"]},
metrics.Label{Name: "handler_name", Value: "iam-security-crentials-for-role"},
metrics.Label{Name: "remote_addr", Value: r.RemoteAddr},
metrics.Label{Name: "remote_addr", Value: remoteIP(r.RemoteAddr)},
metrics.Label{Name: "request_path", Value: "/meta-data/iam/security-credentials/{requested_role}"},
metrics.Label{Name: "requested_role", Value: vars["requested_role"]},
}
logWithLabels(labels).Infof("Handling %s from %s", r.URL.String(), remoteIP(r.RemoteAddr))

// ensure we got compatible api version
if !isCompatibleAPIVersion(r) {
Expand Down Expand Up @@ -259,16 +258,15 @@ func iamSecurityCredentialsForRole(w http.ResponseWriter, r *http.Request) {

// handles: /*
func passthroughHandler(w http.ResponseWriter, r *http.Request) {
log.Infof("Handling %s from %s", r.URL.String(), r.RemoteAddr)

// setup basic telemetry
vars := mux.Vars(r)
labels := []metrics.Label{
metrics.Label{Name: "api_version", Value: vars["api_version"]},
metrics.Label{Name: "handler_name", Value: "passthrough"},
metrics.Label{Name: "remote_addr", Value: r.RemoteAddr},
metrics.Label{Name: "remote_addr", Value: remoteIP(r.RemoteAddr)},
metrics.Label{Name: "request_path", Value: r.URL.String()},
}
logWithLabels(labels).Infof("Handling %s from %s", r.URL.String(), remoteIP(r.RemoteAddr))

// try to enrich the telemetry with additional labels
// if this fail, we will still proxy the request as-is
Expand Down Expand Up @@ -316,7 +314,7 @@ func passthroughHandler(w http.ResponseWriter, r *http.Request) {
func metricsHandler(w http.ResponseWriter, r *http.Request) {
metrics.IncrCounterWithLabels([]string{telemetryPrefix, "http_request"}, 1, []metrics.Label{
metrics.Label{Name: "handler_name", Value: "metrics"},
metrics.Label{Name: "remote_addr", Value: r.RemoteAddr},
metrics.Label{Name: "remote_addr", Value: remoteIP(r.RemoteAddr)},
metrics.Label{Name: "request_path", Value: "/metrics"},
})

Expand Down

0 comments on commit 7076d25

Please sign in to comment.