Skip to content

Commit aba9cb4

Browse files
authored
feat: enable traceparent header for cloudevents (#336)
Fixes: GoogleCloudPlatform#197 `traceparent` is the more modern header that includes information that `X-Cloud-Trace-Context` references.
1 parent 64c48e2 commit aba9cb4

File tree

5 files changed

+29
-0
lines changed

5 files changed

+29
-0
lines changed

src/cloudevents.ts

+5
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,10 @@ export function getBinaryCloudEventContext(
6868
context[attributeName] = req.header(name);
6969
}
7070
}
71+
72+
// Populate the traceparent header.
73+
if (req.header('traceparent')) {
74+
context.traceparent = req.header('traceparent');
75+
}
7176
return context;
7277
}

src/functions.ts

+5
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ export interface CloudEventsContext {
126126
| boolean
127127
| null
128128
| unknown;
129+
/**
130+
* The traceparent string, containing a trace version, trace ID, span ID, and trace options.
131+
* @see https://github.com/cloudevents/spec/blob/master/extensions/distributed-tracing.md
132+
*/
133+
traceparent?: string;
129134
}
130135

131136
export type Context = CloudFunctionsContext | CloudEventsContext;

test/integration/cloudevent.ts

+13
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const TEST_CLOUD_EVENT = {
2525
subject: 'test-subject',
2626
id: 'test-1234-1234',
2727
time: '2020-05-13T01:23:45Z',
28+
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
2829
datacontenttype: 'application/json',
2930
data: {
3031
some: 'payload',
@@ -52,6 +53,17 @@ describe('CloudEvent Function', () => {
5253
body: TEST_CLOUD_EVENT,
5354
expectedCloudEvent: TEST_CLOUD_EVENT,
5455
},
56+
{
57+
name: 'CloudEvents v1.0 structured content request',
58+
headers: {
59+
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
60+
},
61+
body: TEST_CLOUD_EVENT,
62+
expectedCloudEvent: {
63+
...TEST_CLOUD_EVENT,
64+
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
65+
},
66+
},
5567
{
5668
name: 'CloudEvents v1.0 binary content request',
5769
headers: {
@@ -63,6 +75,7 @@ describe('CloudEvent Function', () => {
6375
'ce-id': TEST_CLOUD_EVENT.id,
6476
'ce-time': TEST_CLOUD_EVENT.time,
6577
'ce-datacontenttype': TEST_CLOUD_EVENT.datacontenttype,
78+
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
6679
},
6780
body: TEST_CLOUD_EVENT.data,
6881
expectedCloudEvent: TEST_CLOUD_EVENT,

test/integration/legacy_event.ts

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const TEST_CLOUD_EVENT = {
2424
subject: 'test-subject',
2525
id: 'test-1234-1234',
2626
time: '2020-05-13T01:23:45Z',
27+
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
2728
datacontenttype: 'application/json',
2829
data: {
2930
some: 'payload',
@@ -111,6 +112,7 @@ describe('Event Function', () => {
111112
specversion: '1.0',
112113
subject: 'test-subject',
113114
time: '2020-05-13T01:23:45Z',
115+
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
114116
type: 'com.google.cloud.storage',
115117
},
116118
},
@@ -125,6 +127,7 @@ describe('Event Function', () => {
125127
'ce-id': TEST_CLOUD_EVENT.id,
126128
'ce-time': TEST_CLOUD_EVENT.time,
127129
'ce-datacontenttype': TEST_CLOUD_EVENT.datacontenttype,
130+
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
128131
},
129132
body: TEST_CLOUD_EVENT.data,
130133
expectedData: TEST_CLOUD_EVENT.data,
@@ -136,6 +139,7 @@ describe('Event Function', () => {
136139
specversion: '1.0',
137140
subject: 'test-subject',
138141
time: '2020-05-13T01:23:45Z',
142+
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
139143
type: 'com.google.cloud.storage',
140144
},
141145
},

test/middleware/background_event_to_cloudevent.ts

+2
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ describe('backgroundEventToCloudEventMiddleware', () => {
148148
subject: 'test-subject',
149149
id: 'test-1234-1234',
150150
time: '2020-05-13T01:23:45Z',
151+
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
151152
datacontenttype: 'application/json',
152153
data: {
153154
some: 'payload',
@@ -161,6 +162,7 @@ describe('backgroundEventToCloudEventMiddleware', () => {
161162
subject: 'test-subject',
162163
id: 'test-1234-1234',
163164
time: '2020-05-13T01:23:45Z',
165+
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
164166
datacontenttype: 'application/json',
165167
data: {
166168
some: 'payload',

0 commit comments

Comments
 (0)