Last active
August 3, 2021 09:34
-
-
Save salrashid123/310c03da383f0ac6560509045d5850fe to your computer and use it in GitHub Desktop.
Google cloud logging consolidated logging in golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
//https://github.com/GoogleCloudPlatform/google-cloud-go/issues/720#issuecomment-320798620 | |
import ( | |
"fmt" | |
"log" | |
"net/http" | |
"time" | |
"cloud.google.com/go/logging" | |
"golang.org/x/net/context" | |
mrpb "google.golang.org/genproto/googleapis/api/monitoredres" | |
) | |
var ( | |
client *logging.Client | |
) | |
const ( | |
httpport = ":8080" | |
) | |
func fronthandler(w http.ResponseWriter, r *http.Request) { | |
log.Println("Main Handler") | |
s := r.Header.Get("X-Cloud-Trace-Context") | |
log.Println("X-Cloud-Trace-Context: %s",s) | |
parentLogger := client.Logger( | |
"appengine.googleapis.com/request_log", | |
logging.CommonLabels(map[string]string{ | |
"appengine.googleapis.com/trace_id": s, | |
}), | |
logging.CommonResource(&mrpb.MonitoredResource{ | |
Labels: map[string]string{ | |
"module_id": "default", | |
"project_id": "your_project", | |
"version_id": "1", | |
}, | |
Type: "gae_app", | |
}), | |
) | |
childLogger := client.Logger( | |
"my-log", | |
logging.CommonLabels(map[string]string{ | |
"appengine.googleapis.com/trace_id": s, | |
}), | |
logging.CommonResource(&mrpb.MonitoredResource{ | |
Labels: map[string]string{ | |
"module_id": "default", | |
"project_id": "your_project", | |
"version_id": "1", | |
}, | |
Type: "gae_app", | |
}), | |
) | |
ent2 := logging.Entry{ | |
Timestamp: time.Now(), | |
Severity: logging.Debug, | |
Trace: r.Header.Get("X-Cloud-Trace-Context"), | |
Payload: ">>>>>>>>>>>>>>>>> Child Log message #1", | |
} | |
childLogger.Log(ent2) | |
ent3 := logging.Entry{ | |
Timestamp: time.Now(), | |
Severity: logging.Debug, | |
Trace: r.Header.Get("X-Cloud-Trace-Context"), | |
Payload: ">>>>>>>>>>>>>>>>> Child Log message #2", | |
} | |
childLogger.Log(ent3) | |
ent := logging.Entry{ | |
HTTPRequest: &logging.HTTPRequest{ | |
Request: r, | |
}, | |
Timestamp: time.Now(), | |
Severity: logging.Debug, | |
Trace: r.Header.Get("X-Cloud-Trace-Context"), | |
} | |
parentLogger.Log(ent) | |
fmt.Fprint(w, "hello world") | |
} | |
func healthhandler(w http.ResponseWriter, r *http.Request) { | |
//log.Println("heathcheck...") | |
fmt.Fprint(w, "ok") | |
} | |
func main() { | |
ctx := context.Background() | |
client, _ = logging.NewClient(ctx, "your_project") | |
http.HandleFunc("/", fronthandler) | |
http.HandleFunc("/_ah/health", healthhandler) | |
log.Printf("Starting server on %v", httpport) | |
srv := &http.Server{ | |
Addr: httpport, | |
} | |
srv.ListenAndServe() | |
} | |
// For GCF: | |
/* | |
logName: "projects/your_project/logs/cloudfunctions.googleapis.com/cloud-functions" | |
labels: { | |
execution_id: "4xsh8lstbb2r" | |
} | |
resource: { | |
labels: { | |
function_name: "your_function" | |
project_id: "your_project" | |
region: "us-central1" | |
} | |
type: "cloud_function" | |
} | |
*/ |
Author
salrashid123
commented
Aug 19, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment