简体   繁体   English

Google Stackdriver 日志条目记录字段 logging.googleapis.com/severity vs severity

[英]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.

相关问题 stackdriver 日志记录如何断言条目的严重性? - How does stackdriver logging assert the severity of an entry? 如何在 python 中设置 stackdriver 日志条目的严重性? - How do I set the severity of a stackdriver log entry in python? Google App Engine 请求日志,包括严重性 - Google App Engine request log including severity 通过 gcloud 或 python 获取“logging.googleapis.com/billing/monthly_bytes_ingested”项目信息 - get "logging.googleapis.com/billing/monthly_bytes_ingested" project info by gcloud or python 从 Cloud Run 作业和作业中使用的 package 以正确的严重性登录到 Cloud Logging - Log to Cloud Logging with correct severity from Cloud Run Job and package used in the job Google Stackdriver Logging 向高级过滤器添加评论 - Google Stackdriver Logging add comments to advanced filter GCP 和 Spring logback。 严重性始终是信息 - GCP and Spring logback. Severity is always info 使用日志文件名作为 Google 日志记录的日志名称 - Use log filenames as Log names on Google logging 如何使用 logback 从 google app engine 以 jsonPayload 的形式登录到 stackdriver? - How to log as jsonPayload to stackdriver from google app engine using logback? 未显示 GKE 的 Stackdriver 日志记录 - Stackdriver logging not showing up for GKE
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM