Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 6adbe93

Browse files
crdgonzalezcadraffensperger
authored andcommitted
Propagate trace context header (#118)
1 parent 1b3a192 commit 6adbe93

13 files changed

Lines changed: 356 additions & 95 deletions

File tree

examples/user_interaction/client/src/App.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ class App extends React.Component {
5858
const data = JSON.parse(xhr.responseText)
5959
const result = this.callCalculatePi();
6060
this.setState({ pi: result, prime_numbers: data });
61-
this.props.history.push('/second_page');
6261
}
6362
};
6463

examples/user_interaction/client/src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ const routing = (
4545
ReactDOM.render(routing, document.getElementById('root'));
4646

4747
window.ocAgent = 'http://localhost:55678';
48+
// For the purpose of this example, send trace header to all hosts.
49+
window.ocTraceHeaderHostRegex = /.*/;
4850
window.ocSampleRate = 1.0; // Sample at 100% for test only. Default is 1/10000.
4951

5052
// Send the root span and child spans for the initial page load to the

examples/user_interaction/server/package-lock.json

Lines changed: 73 additions & 73 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/user_interaction/server/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
"node": ">=8"
2222
},
2323
"dependencies": {
24-
"@opencensus/exporter-zipkin": "^0.0.12",
25-
"@opencensus/nodejs": "^0.0.12",
26-
"@opencensus/propagation-tracecontext": "^0.0.12",
24+
"@opencensus/exporter-zipkin": "^0.0.14",
25+
"@opencensus/nodejs": "^0.0.14",
26+
"@opencensus/propagation-tracecontext": "^0.0.14",
2727
"http": "*",
2828
"sleep": "^6.1.0"
2929
},

examples/user_interaction/server/server.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,34 @@ function calculatePrimeNumbers() {
6363

6464
/** A function which handles requests and send response. */
6565
function handleRequest(request, response) {
66-
const span = tracer.startChildSpan({ name: 'octutorials.handleRequest' });
66+
const span = tracer.startChildSpan({ name: 'ocweb.handleRequest' });
6767

6868
try {
6969
let body = [];
7070
request.on('error', err => console.log(err));
7171
request.on('data', chunk => body.push(chunk));
7272

73-
// Necessary headers because the Node.js and React dev servers run in different ports.
74-
response.setHeader('Access-Control-Allow-Origin', '*');
75-
response.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
76-
response.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
77-
response.setHeader('Access-Control-Allow-Credentials', true);
78-
73+
// Necessary header because the Node.js and React dev servers run in different ports.
74+
response.setHeader('Access-Control-Allow-Origin', '*');
75+
7976
let result = '';
8077
let code = 200;
78+
// Accept all CORS pre-flight requests
79+
if(request.method === 'OPTIONS'){
80+
// Set headers to allow traceparent and subsequent get request.
81+
response.setHeader('Access-Control-Allow-Headers', 'traceparent');
82+
response.setHeader('Access-Control-Allow-Methods', 'GET');
83+
request.on('end', () => {
84+
span.end();
85+
response.statusCode = code;
86+
response.end();
87+
});
88+
return;
89+
}
8190
if (url.parse(request.url).pathname === '/sleep') {
8291
console.log("Sleeping...")
8392
const time = Date.now();
84-
sleep.sleep(5);
93+
sleep.sleep(2);
8594
result = { time: Date.now() - time, value: "" };
8695
console.log("Finished.")
8796
} else if (url.parse(request.url).pathname === '/prime_numbers') {
@@ -109,7 +118,7 @@ function handleRequest(request, response) {
109118
function setupTracerAndExporters() {
110119
const zipkinOptions = {
111120
url: 'http://localhost:9411/api/v2/spans',
112-
serviceName: 'opencensus_tutorial'
121+
serviceName: 'opencensus_web_server'
113122
};
114123

115124
// Creates Zipkin exporter

packages/opencensus-web-instrumentation-zone/package-lock.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/opencensus-web-instrumentation-zone/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@
6868
},
6969
"dependencies": {
7070
"@opencensus/web-core": "^0.0.3",
71-
"@opencensus/web-exporter-ocagent": "^0.0.3"
71+
"@opencensus/web-exporter-ocagent": "^0.0.3",
72+
"@opencensus/web-propagation-tracecontext": "0.0.3"
7273
},
7374
"peerDependencies": {
7475
"zone.js": "~0.9.1"

packages/opencensus-web-instrumentation-zone/src/export-interaction-tracker.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const windowWithOcwGlobals = window as WindowWithOcwGlobals;
2424
const TRACE_ENDPOINT = '/v1/trace';
2525

2626
import { InteractionTracker } from './interaction-tracker';
27+
import { doPatching } from './monkey-patching';
2728

2829
function setupExporter() {
2930
if (!windowWithOcwGlobals.ocAgent) {
@@ -39,6 +40,7 @@ function setupExporter() {
3940
}
4041

4142
export function startInteractionTracker() {
43+
doPatching();
4244
setupExporter();
4345
InteractionTracker.startTracking();
4446
}

0 commit comments

Comments
 (0)