[英]Google Stackdriver log entry record field logging.googleapis.com/severity vs severity
I have a general log package that wraps logs for Stackdriver- Gets context, severity etc and transform it to LogEntry ie:我有一个通用日志 package,它包装了 Stackdriver 的日志——获取上下文、严重性等并将其转换为LogEntry ,即:
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.这个 package 是全局的,用于 Cloud Run 和 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. Compute Engine 中的日志是通过Google Ops Agent获取的 如果我希望日志严重性与 ops 代理一起使用,我需要根据文档使用此 json 键logging.googleapis.com/severity
。 com/stackdriver/docs/solutions/agents/ops-agent/configuration#special-fields否则严重性仍然是jsonPayload
的一部分,并且严重性不适用于日志条目。 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/
.但这与此处的文档冲突: https:https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry这里说 json 字段应该是没有前缀logging.googleapis.com/
的简单severity
. 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如果我使用这个结构,它就不能与 Ops Agent 一起工作
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).它适用于 Ops Agent,但不适用于 Cloud Run(我只是将logging.googleapis.com/severity
视为jsonPayload
的一部分,严重性不适用)。 Is it the expected behaviour?这是预期的行为吗? Is there a workaround for this without creating two packages?有没有创建两个包的解决方法?
This is the expected behavior.这是预期的行为。 There's no workaround for this partícular scenario, so you need to build two packages.对于这种特殊情况没有解决方法,因此您需要构建两个包。
In Cloud Run, as you can read here about Special JSON fields in messages, when you provide a structured log as a JSON dictionary, some special fields are stripped from the jsonPayload
and are written to the corresponding field in the generated LogEntry
as described in the documentation for special fields .在 Cloud Run 中,您可以在此处阅读有关消息中特殊 JSON 字段的信息,当您将结构化日志作为 JSON 字典提供时,一些特殊字段会从jsonPayload
中剥离并写入生成的LogEntry
中的相应字段,如特殊领域的文档。
For example, if your JSON includes a severity
property, it is removed from the jsonPayload
and appears instead as the log entry's severity
.例如,如果您的 JSON 包含severity
属性,它将从jsonPayload
中删除并显示为日志条目的severity
。 The message
property is used as the main display text of the log entry if present. message
属性用作日志条目的主要显示文本(如果存在)。 For more on special properties, read the Logging Resource section.有关特殊属性的更多信息,请阅读日志记录资源部分。
I ended up writing two structs and using fmt.Stringer
to write only one log function. This is bad IMO because it requires to handle two structs (ie you add field to one, you need to remember to add to the second one also), but I couldn't find anything better.我最终写了两个结构并使用fmt.Stringer
只写了一个日志 function。这是糟糕的 IMO,因为它需要处理两个结构(即你将字段添加到一个,你需要记住也添加到第二个),但我找不到更好的东西。 It look to me like some kind of mistake in GCP, I guess it written by two different teams, otherwise I don't see any reason why not using the same json field name.在我看来,这像是 GCP 中的某种错误,我猜它是由两个不同的团队编写的,否则我看不出有任何理由不使用相同的 json 字段名称。
var useOpsAgent bool
func SetUseOpsAgent(v bool) {
useOpsAgent = v
}
// for cloud run
type Entry struct {
Message string `json:"message"`
Severity Severity `json:"severity,omitempty"`
Trace string `json:"logging.googleapis.com/trace,omitempty"`
Component string `json:"component,omitempty"`
}
// for compute engine
type EntryOA struct {
Message string `json:"message"`
Severity Severity `json:"logging.googleapis.com/severity,omitempty"`
Trace string `json:"logging.googleapis.com/trace,omitempty"`
Component string `json:"component,omitempty"`
}
func (e Entry) String() string { ...
func (e EntryOA) String() string { ...
func Log(ctx context.Context, severity Severity, format string, a ...interface{}) {
var logEntry fmt.Stringer = Entry{Severity: severity, Message: fmt.Sprintf(format, a...), Component: "arbitrary-property", Trace: GetTraceId(ctx)}
if useOpsAgent {
logEntry = EntryOA{Severity: severity, Message: fmt.Sprintf(format, a...), Component: "arbitrary-property", Trace: GetTraceId(ctx)}
}
log.Println(logEntry)
}
Then when using in the main.go
:然后在main.go
中使用时:
func init() {
loglib.SetUseOpsAgent(true)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.