golang / protobuf

Go support for Google's protocol buffers
BSD 3-Clause "New" or "Revised" License
9.74k stars 1.58k forks source link

Invalid Unmarshaling #1512

Closed vinay2897 closed 1 year ago

vinay2897 commented 1 year ago

I am trying to unmarshal opentelemetry data like so:

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)

But the output is not completely decoded. Some of the output looks 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 looks like this: spans:{trace_id:"\xb5\x81\x91\x8b\x02\x9a/\xf1\x08\x06\xaf~\xea\x9fQ\xc0"

Any help would be appreciated

cybrcodr commented 1 year ago

It looks like the trace_id field is of bytes type. Is that correct? bytes field are in hex string format in textproto.

puellanivis commented 1 year ago

It is indeed a bytes field: https://github.com/open-telemetry/opentelemetry-proto/blob/main/opentelemetry/proto/trace/v1/trace.proto#L81-L89

vinay2897 commented 1 year ago

Thanks @cybrcodr @puellanivis for pointing me, I had to use the hex.EncodeToString() to get the hex id from bytes