sdk-javascript icon indicating copy to clipboard operation
sdk-javascript copied to clipboard

Support CloudEvents Traceparent Extension

Open grant opened this issue 3 years ago • 3 comments

Describe the Bug

Some HTTP requests may follow the CE traceparent extension. Currently, the default way of parsing this extension does not work as expected.

Concretely, this extension / attributes (traceparent, tracestate):

https://github.com/cloudevents/spec/blob/v1.0/extensions/distributed-tracing.md

  • There should be a way to access CloudEvent extensions after converting a HTTP request to a CloudEvent.
  • There should be a sample showing how to receive CloudEvents with documented extensions.

Steps to Reproduce

index.js

const app = require("express")();
const { HTTP } = require("cloudevents");

app.post("/", (req, res) => {
  // body and headers come from an incoming HTTP request, e.g. express.js
  const receivedEvent = HTTP.toEvent({ headers: req.headers, body: req.body });
  console.log(receivedEvent);
  res.send('ok');
});

app.listen(8080);

package.json

{
  "main": "index.js",
  "scripts": {
    "start": "node ."
  },
  "dependencies": {
    "cloudevents": "^5.2.0",
    "express": "^4.17.1"
  }
}

Send HTTP request:

curl localhost:8080 -XPOST \
-H "Ce-Id: abc-123" \
-H "Ce-Source: cloudevents.conformance.tool" \
-H "Ce-Specversion: 1.0" \
-H "Ce-Type: foo.bar" \
-H "traceparent: 123"

Observe no traceparent field:

{"id":"abc-123","time":"2021-12-10T17:34:55.358Z","type":"foo.bar","source":"cloudevents.conformance.tool","specversion":"1.0"}

Note: The traceparent header does not need to have a ce- prefix.

Expected Behavior

  • HTTP.toEvent should parse this documented extension attribute and populate the attribute in the CloudEvent object.
  • There is a sample for receiving the traceparent extension

Actual Behavior

  • There is no sample for receiving the traceparent extension
  • HTTP.toEvent doesn't seem to parse this extension

grant avatar Dec 11 '21 01:12 grant

Hey @grant - so to be quite honest, this extension has been a bit confusing to me, and I have had different expectations. To try and clarify, I posted in the #cloudevents slack about it. https://cloud-native.slack.com/archives/C9DB5ABAA/p1639507801025900

My expectation is that all extension attributes are encoded in the same way as other attributes. That is, they appear in the incoming HTTP request headers prefixed with ce-. So in your case, the header should be ce-traceparent. If you try your example with that header, you should see the extension in the CloudEvent object.

Let's see if we get any response from others on that Slack query.

lance avatar Dec 14 '21 18:12 lance

@grant I am pretty sure that the SDK is doing the right thing based on discussion in the Slack link above. There is also an open PR to the Distributed Tracing Extension that we should be aware of https://github.com/cloudevents/spec/pull/912.

lance avatar Dec 15 '21 15:12 lance

This issue is stale because it has been open 30 days with no activity.

github-actions[bot] avatar Jan 15 '22 00:01 github-actions[bot]

Closing as obsolete

lance avatar Oct 11 '22 20:10 lance