无效的proto解组。

huangapple go评论93阅读模式
英文:

Invalid unmarshalling of proto

问题

我正在尝试解码 proto 数据,但是 proto 没有正确解码。

这是我的做法:

decodedStr, err := base64.StdEncoding.DecodeString(request.Body)
if err != nil {
    panic("malformed input")
}

data := &tracepb.ExportTraceServiceRequest{}
if err := proto.Unmarshal(decodedStr, data); err != nil {
        log.Fatalln("Failed to parse:", err)
}

log.Printf("Response - %v", data)

输出结果如下:

Response - resource_spans:{resource:{attributes:{key:"service.name"  value:{string_value:"node_app"}}  attributes:{key:"telemetry.sdk.language"  value:{string_value:"nodejs"}}  attributes:{key:"telemetry.sdk.name"  value:{string_value:"opentelemetry"}}  attributes:{key:"telemetry.sdk.version"  value:{string_value:"1.8.0"}}  attributes:{key:"process.pid"  value:{int_value:1}}  attributes:{key:"process.executable.name"  value:{string_value:"node"}}  attributes:{key:"process.command"  value:{string_value:"/usr/app/index.js"}}  attributes:{key:"process.command_line"  value:{string_value:"/usr/local/bin/node /usr/app/index.js"}}  attributes:{key:"process.runtime.version"  value:{string_value:"18.13.0"}}  attributes:{key:"process.runtime.name"  value:{string_value:"nodejs"}}  attributes:{key:"process.runtime.description"  value:{string_value:"Node.js"}}}  scope_spans:{scope:{name:"@opentelemetry/instrumentation-express"  version:"0.32.0"}  spans:{trace_id:"\xb5\x81\x91\x8b\x02\x9a/\xf1\x08\x06\xaf~\xea\x9fQ\xc0"  span_id:"T\x06\x89m\x1ex\xf9A"  parent_span_id:"?\xbc\x18`O\xa5\xb8\xe1"  name:"middleware - query"  kind:SPAN_KIND_INTERNAL  start_time_unix_nano:1673434036590614272  end_time_unix_nano:1673434036590671104  attributes:{key:"http.route"  value:{string_value:"/"}}  attributes:{key:"express.name"  value:{string_value:"query"}}  attributes:{key:"express.type"  value:{string_value:"middleware"}}  status:{}}  spans:{trace_id:"\xb5\x81\x91\x8b\x02\x9a/\xf1\x08\x06\xaf~\xea\x9fQ\xc0"  span_id:"\xd5c\xf7>\xf6Cxz"  parent_span_id:"?\xbc\x18`O\xa5\xb8\xe1"  name:"middleware - expressInit"  kind:SPAN_KIND_INTERNAL  start_time_unix_nano:1673434036590760704

不确定为什么 traceId 显示为这样:

spans:{trace_id:"\xb5\x81\x91\x8b\x02\x9a/\xf1\x08\x06\xaf~\xea\x9fQ\xc0"

我对 GoLang 还不熟悉。任何帮助将不胜感激。

英文:

I am trying to decode proto data. But the proto is not properly decoded.

This is how I am doing:

decodedStr, err := base64.StdEncoding.DecodeString(request.Body)
if err != nil {
    panic("malformed input")
}

data := &tracepb.ExportTraceServiceRequest{}
if err := proto.Unmarshal(decodedStr, data); err != nil {
        log.Fatalln("Failed to parse:", err)
}

log.Printf("Response - %v", data)

The output is like this:

Response - resource_spans:{resource:{attributes:{key:"service.name"  value:{string_value:"node_app"}}  attributes:{key:"telemetry.sdk.language"  value:{string_value:"nodejs"}}  attributes:{key:"telemetry.sdk.name"  value:{string_value:"opentelemetry"}}  attributes:{key:"telemetry.sdk.version"  value:{string_value:"1.8.0"}}  attributes:{key:"process.pid"  value:{int_value:1}}  attributes:{key:"process.executable.name"  value:{string_value:"node"}}  attributes:{key:"process.command"  value:{string_value:"/usr/app/index.js"}}  attributes:{key:"process.command_line"  value:{string_value:"/usr/local/bin/node /usr/app/index.js"}}  attributes:{key:"process.runtime.version"  value:{string_value:"18.13.0"}}  attributes:{key:"process.runtime.name"  value:{string_value:"nodejs"}}  attributes:{key:"process.runtime.description"  value:{string_value:"Node.js"}}}  scope_spans:{scope:{name:"@opentelemetry/instrumentation-express"  version:"0.32.0"}  spans:{trace_id:"\xb5\x81\x91\x8b\x02\x9a/\xf1\x08\x06\xaf~\xea\x9fQ\xc0"  span_id:"T\x06\x89m\x1ex\xf9A"  parent_span_id:"?\xbc\x18`O\xa5\xb8\xe1"  name:"middleware - query"  kind:SPAN_KIND_INTERNAL  start_time_unix_nano:1673434036590614272  end_time_unix_nano:1673434036590671104  attributes:{key:"http.route"  value:{string_value:"/"}}  attributes:{key:"express.name"  value:{string_value:"query"}}  attributes:{key:"express.type"  value:{string_value:"middleware"}}  status:{}}  spans:{trace_id:"\xb5\x81\x91\x8b\x02\x9a/\xf1\x08\x06\xaf~\xea\x9fQ\xc0"  span_id:"\xd5c\xf7>\xf6Cxz"  parent_span_id:"?\xbc\x18`O\xa5\xb8\xe1"  name:"middleware - expressInit"  kind:SPAN_KIND_INTERNAL  start_time_unix_nano:1673434036590760704

Not sure why traceId is shown like this:

spans:{trace_id:"\xb5\x81\x91\x8b\x02\x9a/\xf1\x08\x06\xaf~\xea\x9fQ\xc0"

Am new to GoLang. Any help would be greatly appreciated

答案1

得分: 0

trace_id字段似乎包含的是二进制数据而不是十六进制。生成的proto String方法将把二进制数据渲染为字符串。因此,不可打印的字符将显示为ASCII转义序列。

如果你想以其他格式(例如十六进制)显示数据,你需要自己实现一个函数来渲染proto。

英文:

The trace_id field appears to contain the ID as binary data instead of hex. The generated proto String method will render the binary data as a string. Hence non-printable characters as displayed as ASCII escape sequences.

If you want to display the data in other format (eg, Hex) you will need to implement your own function to render the proto.

答案2

得分: 0

使用encoding/hex模块的hex.EncodeToString()函数将bytes转换为hex

英文:

Used encoding/hex module's hex.EncodeToString() function to convert bytes to hex

huangapple
  • 本文由 发表于 2023年1月11日 18:57:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75081956.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定