I have a general log package that wraps logs for Stackdriver- Gets context, severity etc and transform it to LogEntry i.e:
func Log(ctx context.Context, severity Severity, format string, a ...interface{}) {
log.Println(Entry{Severity: severity, Message: fmt.Sprintf(format, a...), Component: "arbitrary-property", Trace: GetTraceId(ctx)})
}
The entry struct lookgs like this
type Entry struct {
Message string `json:"message"`
Severity Severity `json:"severity,omitempty"`
Trace string `json:"logging.googleapis.com/trace,omitempty"`
// Logs Explorer allows filtering and display of this as `jsonPayload.component`.
Component string `json:"component,omitempty"`
}
This package is global and used for multiple services both in Cloud Run and in Compute Engine.
The logs in Compute Engine is ingested with Google Ops Agent
If I want the logs severity to work with ops agent, I need to use this json key logging.googleapis.com/severity
according to the docs: https://cloud.google.com/stackdriver/docs/solutions/agents/ops-agent/configuration#special-fields
Otherwise the severity remains a part of jsonPayload
, and the severity is not applying on the log entry.
But this conflicts with the docs here: https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry
Here it says the json field should be simply severity
without the prefix logging.googleapis.com/
.
If I use this struct
type Entry struct {
...
Severity Severity `json:"severity,omitempty"`
}
It's not working with Ops Agent, if I use this struct
type Entry struct {
...
Severity Severity `json:"logging.googleapis.com/severity,omitempty"`
}
It works with Ops Agent, but not working in Cloud Run (I simply see logging.googleapis.com/severity
as part of jsonPayload
, the severity is not applying).
Is it the expected behaviour? Is there a workaround for this without creating two packages?