Skip to content

Commit

Permalink
feat: enable traceparent header for cloudevents (#336)
Browse files Browse the repository at this point in the history
Fixes: #197

`traceparent` is the more modern header that includes information that `X-Cloud-Trace-Context` references.
  • Loading branch information
grant committed Oct 12, 2021
1 parent 64c48e2 commit aba9cb4
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/cloudevents.ts
Expand Up @@ -68,5 +68,10 @@ export function getBinaryCloudEventContext(
context[attributeName] = req.header(name);
}
}

// Populate the traceparent header.
if (req.header('traceparent')) {
context.traceparent = req.header('traceparent');
}
return context;
}
5 changes: 5 additions & 0 deletions src/functions.ts
Expand Up @@ -126,6 +126,11 @@ export interface CloudEventsContext {
| boolean
| null
| unknown;
/**
* The traceparent string, containing a trace version, trace ID, span ID, and trace options.
* @see https://github.com/cloudevents/spec/blob/master/extensions/distributed-tracing.md
*/
traceparent?: string;
}

export type Context = CloudFunctionsContext | CloudEventsContext;
13 changes: 13 additions & 0 deletions test/integration/cloudevent.ts
Expand Up @@ -25,6 +25,7 @@ const TEST_CLOUD_EVENT = {
subject: 'test-subject',
id: 'test-1234-1234',
time: '2020-05-13T01:23:45Z',
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
datacontenttype: 'application/json',
data: {
some: 'payload',
Expand Down Expand Up @@ -52,6 +53,17 @@ describe('CloudEvent Function', () => {
body: TEST_CLOUD_EVENT,
expectedCloudEvent: TEST_CLOUD_EVENT,
},
{
name: 'CloudEvents v1.0 structured content request',
headers: {
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
},
body: TEST_CLOUD_EVENT,
expectedCloudEvent: {
...TEST_CLOUD_EVENT,
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
},
},
{
name: 'CloudEvents v1.0 binary content request',
headers: {
Expand All @@ -63,6 +75,7 @@ describe('CloudEvent Function', () => {
'ce-id': TEST_CLOUD_EVENT.id,
'ce-time': TEST_CLOUD_EVENT.time,
'ce-datacontenttype': TEST_CLOUD_EVENT.datacontenttype,
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
},
body: TEST_CLOUD_EVENT.data,
expectedCloudEvent: TEST_CLOUD_EVENT,
Expand Down
4 changes: 4 additions & 0 deletions test/integration/legacy_event.ts
Expand Up @@ -24,6 +24,7 @@ const TEST_CLOUD_EVENT = {
subject: 'test-subject',
id: 'test-1234-1234',
time: '2020-05-13T01:23:45Z',
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
datacontenttype: 'application/json',
data: {
some: 'payload',
Expand Down Expand Up @@ -111,6 +112,7 @@ describe('Event Function', () => {
specversion: '1.0',
subject: 'test-subject',
time: '2020-05-13T01:23:45Z',
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
type: 'com.google.cloud.storage',
},
},
Expand All @@ -125,6 +127,7 @@ describe('Event Function', () => {
'ce-id': TEST_CLOUD_EVENT.id,
'ce-time': TEST_CLOUD_EVENT.time,
'ce-datacontenttype': TEST_CLOUD_EVENT.datacontenttype,
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
},
body: TEST_CLOUD_EVENT.data,
expectedData: TEST_CLOUD_EVENT.data,
Expand All @@ -136,6 +139,7 @@ describe('Event Function', () => {
specversion: '1.0',
subject: 'test-subject',
time: '2020-05-13T01:23:45Z',
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
type: 'com.google.cloud.storage',
},
},
Expand Down
2 changes: 2 additions & 0 deletions test/middleware/background_event_to_cloudevent.ts
Expand Up @@ -148,6 +148,7 @@ describe('backgroundEventToCloudEventMiddleware', () => {
subject: 'test-subject',
id: 'test-1234-1234',
time: '2020-05-13T01:23:45Z',
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
datacontenttype: 'application/json',
data: {
some: 'payload',
Expand All @@ -161,6 +162,7 @@ describe('backgroundEventToCloudEventMiddleware', () => {
subject: 'test-subject',
id: 'test-1234-1234',
time: '2020-05-13T01:23:45Z',
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
datacontenttype: 'application/json',
data: {
some: 'payload',
Expand Down

0 comments on commit aba9cb4

Please sign in to comment.