Skip to content

Commit

Permalink
test: remove test-trace-header-context (#768)
Browse files Browse the repository at this point in the history
PR-URL: #768
  • Loading branch information
kjin committed Jun 4, 2018
1 parent 5c1dabe commit f9f648a
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 164 deletions.
59 changes: 55 additions & 4 deletions test/plugins/test-trace-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import * as semver from 'semver';
import * as stream from 'stream';
import {URL} from 'url';

import {TraceSpan} from '../../src/trace';
import {Constants} from '../../src/constants';
import {SpanKind, TraceSpan} from '../../src/trace';
import {parseContextFromHeader, TraceContext} from '../../src/util';
import * as testTraceModule from '../trace';
import {ASSERT_SPAN_TIME_TOLERANCE_MS, assertSpanDuration, DEFAULT_SPAN_DURATION} from '../utils';
import {Express4} from '../web-frameworks/express';
Expand All @@ -47,7 +49,7 @@ class WaitForResponse {
// A Promise that is resolved when the request function to which
// this.handleResponse is passed has received its full response, or
// this.handleDone has been called.
done: Promise<string>;
readonly done: Promise<string>;
// A callback to be passed to http.request or http.get, so that when response
// data has been fully consumed, this.done will be resolved.
handleResponse = (res: httpModule.IncomingMessage) => {
Expand Down Expand Up @@ -286,7 +288,53 @@ for (const nodule of Object.keys(servers) as Array<keyof typeof servers>) {
}
});

it('should propagate context', async () => {
it('should propagate trace context across network boundaries', async () => {
const server = new ServerFramework();
// On the server side, capture the incoming trace context.
// The values in the captured context should be consistent with those
// observed on the client side.
let serverCapturedTraceContext: TraceContext|null = null;
server.addHandler({
path: '/',
hasResponse: true,
fn: async (headers) => {
const traceContext = headers[Constants.TRACE_CONTEXT_HEADER_NAME];
assert.ok(traceContext && typeof traceContext === 'string');
serverCapturedTraceContext =
parseContextFromHeader(traceContext as string);
return {statusCode: 200, message: 'hi'};
}
});
const port = server.listen(0);
try {
await testTraceModule.get().runInRootSpan(
{name: 'root'}, async (rootSpan) => {
assert.ok(testTraceModule.get().isRealSpan(rootSpan));
const waitForResponse = new WaitForResponse();
http.get(
{port, rejectUnauthorized: false},
waitForResponse.handleResponse);
await waitForResponse.done;
rootSpan.endSpan();
});
} finally {
server.shutdown();
}
// There should be a single trace with two spans:
// [0] outer root span
// [1] http request on behalf of outer
const clientTrace = testTraceModule.getOneTrace();
assert.strictEqual(clientTrace.spans.length, 2);
assert.strictEqual(clientTrace.spans[1].kind, SpanKind.RPC_CLIENT);
const httpSpan = clientTrace.spans[1];
// Check that trace context is as expected.
assert.ok(serverCapturedTraceContext);
assert.strictEqual(
serverCapturedTraceContext!.traceId, clientTrace.traceId);
assert.strictEqual(serverCapturedTraceContext!.spanId, httpSpan.spanId);
});

it('should preserve trace context across async boundaries', async () => {
const server = new ServerFramework();
server.addHandler({
path: '/',
Expand All @@ -305,6 +353,9 @@ for (const nodule of Object.keys(servers) as Array<keyof typeof servers>) {
{port, rejectUnauthorized: false},
waitForResponse.handleResponse);
await waitForResponse.done;
// This code executes after the HTTP request is done; i.e. in a
// descendant continuation from the request 'end' event handler.
// We make sure that trace context is still accessible here.
const afterHttpSpan =
testTraceModule.get().createChildSpan({name: 'after-http'});
assert.ok(testTraceModule.get().isRealSpan(afterHttpSpan));
Expand Down Expand Up @@ -454,7 +505,7 @@ for (const nodule of Object.keys(servers) as Array<keyof typeof servers>) {
`${nodule}://localhost:${port}/?foo=bar`);
});

it('should should include error information if there was one', () => {
it('should include error information if there was one', () => {
assert.strictEqual(errorSpan.labels[ERROR_DETAILS_NAME], 'Error');
assert.strictEqual(
errorSpan.labels[ERROR_DETAILS_MESSAGE], 'socket hang up');
Expand Down
28 changes: 28 additions & 0 deletions test/test-trace-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,34 @@ describe('Trace Interface', function() {
assert.strictEqual(rootSpan.span.parentSpanId, '667');
});
});

describe('getting response trace context', () => {
it('should behave as expected', () => {
const fakeTraceId = 'ffeeddccbbaa99887766554433221100';
const traceApi = createTraceAgent();
const tracedContext = fakeTraceId + '/0;o=1';
const untracedContext = fakeTraceId + '/0;o=0';
const unspecifiedContext = fakeTraceId + '/0';
assert.strictEqual(
traceApi.getResponseTraceContext(tracedContext, true),
tracedContext);
assert.strictEqual(
traceApi.getResponseTraceContext(tracedContext, false),
untracedContext);
assert.strictEqual(
traceApi.getResponseTraceContext(untracedContext, true),
untracedContext);
assert.strictEqual(
traceApi.getResponseTraceContext(untracedContext, false),
untracedContext);
assert.strictEqual(
traceApi.getResponseTraceContext(unspecifiedContext, true),
untracedContext);
assert.strictEqual(
traceApi.getResponseTraceContext(unspecifiedContext, false),
untracedContext);
});
});
});
});

Expand Down
146 changes: 0 additions & 146 deletions test/test-trace-header-context.ts

This file was deleted.

13 changes: 11 additions & 2 deletions test/web-frameworks/base.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {IncomingHttpHeaders} from 'http';

/**
* Copyright 2018 Google LLC
*
Expand Down Expand Up @@ -30,12 +32,19 @@ export type WebFrameworkAddHandlerOptions = {
path: string;
}&({
hasResponse: false;
fn: () => Promise<void>;
fn: (incomingHeaders: IncomingHttpHeaders) => Promise<void>;
}|{
hasResponse: true;
fn: () => Promise<WebFrameworkResponse>;
fn: (incomingHeaders: IncomingHttpHeaders) => Promise<WebFrameworkResponse>;
});

/**
* A type that describes a ramework-agnostic request handler function.
*/
export type WebFrameworkHandlerFunction =
(incomingHeaders: IncomingHttpHeaders) =>
Promise<void|WebFrameworkResponse>;

/**
* Abstraction over a web framework.
*/
Expand Down
2 changes: 1 addition & 1 deletion test/web-frameworks/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class Connect3 implements WebFramework {
next: Function) => {
let response: WebFrameworkResponse|void;
try {
response = await options.fn();
response = await options.fn(req.headers);
} catch (e) {
// Unlike in Express, there doesn't seem to be an easily documented
// way to silence errors
Expand Down
2 changes: 1 addition & 1 deletion test/web-frameworks/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class Express4 implements WebFramework {
this.app.get(options.path, async (req, res, next) => {
let response: WebFrameworkResponse|void;
try {
response = await options.fn();
response = await options.fn(req.headers);
} catch (e) {
next(e);
return;
Expand Down
7 changes: 3 additions & 4 deletions test/web-frameworks/hapi17.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import {hapi_17} from '../../src/plugins/types';

import {WebFramework, WebFrameworkAddHandlerOptions, WebFrameworkResponse} from './base';
import {WebFramework, WebFrameworkAddHandlerOptions, WebFrameworkHandlerFunction, WebFrameworkResponse} from './base';

export class Hapi17 implements WebFramework {
static commonName = `hapi@17`;
Expand All @@ -28,8 +28,7 @@ export class Hapi17 implements WebFramework {
// So instead of registering a new Hapi plugin per path,
// register only the first time -- passing a function that will iterate
// through a list of routes keyed under the path.
private routes =
new Map<string, Array<() => Promise<WebFrameworkResponse|void>>>();
private routes = new Map<string, WebFrameworkHandlerFunction[]>();
private registering = Promise.resolve();

constructor() {
Expand Down Expand Up @@ -58,7 +57,7 @@ export class Hapi17 implements WebFramework {
handler: async (request, h) => {
let result;
for (const handler of this.routes.get(options.path)!) {
result = await handler();
result = await handler(request.raw.req.headers);
if (result) {
return result;
}
Expand Down
4 changes: 2 additions & 2 deletions test/web-frameworks/hapi8_16.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class Hapi implements WebFramework {
handler: async (request, reply) => {
let response: WebFrameworkResponse;
try {
response = await options.fn();
response = await options.fn(request.raw.req.headers);
} catch (e) {
reply(e);
return;
Expand All @@ -53,7 +53,7 @@ export class Hapi implements WebFramework {
} else {
this.server.ext('onPreHandler', async (request, reply) => {
try {
await options.fn();
await options.fn(request.raw.req.headers);
} catch (e) {
reply(e);
return;
Expand Down
2 changes: 1 addition & 1 deletion test/web-frameworks/koa1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class Koa1 implements WebFramework {
yield testTraceModule.get().wrap(async (cb: Function) => {
let response: WebFrameworkResponse|void;
try {
response = await options.fn();
response = await options.fn(this.request.req.headers);
} catch (err) {
cb(err);
return;
Expand Down
2 changes: 1 addition & 1 deletion test/web-frameworks/koa2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class Koa2 implements WebFramework {
addHandler(options: WebFrameworkAddHandlerOptions): void {
this.app.use(async (ctx, next) => {
if (ctx.request.path === options.path) {
const response = await options.fn();
const response = await options.fn(ctx.req.headers);
if (response) {
ctx.response.status = response.statusCode;
ctx.response.body = response.message;
Expand Down

0 comments on commit f9f648a

Please sign in to comment.