This repository was archived by the owner on Mar 11, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
Introduce middleware directory #248
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /*! | ||
| * Copyright 2018 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| export interface StackdriverHttpRequest { | ||
| requestMethod?: string; | ||
| requestUrl?: string; | ||
| requestSize?: number; | ||
| status?: number; | ||
| responseSize?: number; | ||
| userAgent?: string; | ||
| remoteIp?: string; | ||
| serverIp?: string; | ||
| referer?: string; | ||
| latency?: {seconds: number; nanos: number;}; | ||
| cacheLookup?: boolean; | ||
| cacheHit?: boolean; | ||
| cacheValidatedWithOriginServer?: boolean; | ||
| cacheFillBytes?: number; | ||
| protocol?: string; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /*! | ||
| * Copyright 2018 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import * as context from '@opencensus/propagation-stackdriver'; | ||
| import * as http from 'http'; | ||
|
|
||
| export type HeaderWrapper = context.HeaderGetter&context.HeaderSetter; | ||
|
|
||
| export function makeHeaderWrapper(req: http.IncomingMessage): HeaderWrapper { | ||
| const wrapper = { | ||
| setHeader(name: string, value: string) { | ||
| req.headers[name] = value; | ||
| }, | ||
| getHeader(name: string) { | ||
| return req.headers[name]; | ||
| } | ||
| }; | ||
| return wrapper; | ||
| } | ||
|
|
||
| export function getOrInjectContext(headerWrapper: HeaderWrapper): | ||
| context.SpanContext { | ||
| let spanContext = context.extract(headerWrapper); | ||
| if (spanContext) { | ||
| return spanContext; | ||
| } | ||
|
|
||
| // We were the first actor to detect lack of context. Establish context. | ||
| spanContext = context.generate(); | ||
| context.inject(headerWrapper, spanContext); | ||
| return spanContext; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /*! | ||
| * Copyright 2018 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| // Types-only import. | ||
| import {Request, Response} from 'express'; | ||
|
|
||
| import {StackdriverHttpRequest} from '../../http-request'; | ||
|
|
||
| export function makeHttpRequestData( | ||
| req: Request, res: Response, | ||
| latencyMilliseconds: number): StackdriverHttpRequest { | ||
| return { | ||
| status: res.statusCode, | ||
| requestUrl: req.url, | ||
| requestMethod: req.method, | ||
| userAgent: req.headers['user-agent'], | ||
| responseSize: | ||
| (res.getHeader && Number(res.getHeader('Content-Length'))) || 0, | ||
| latency: { | ||
| seconds: Math.floor(latencyMilliseconds / 1e3), | ||
| nanos: Math.floor((latencyMilliseconds % 1e3) * 1e6) | ||
| } | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /*! | ||
| * Copyright 2018 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import onFinished = require('on-finished'); | ||
| import {getOrInjectContext, makeHeaderWrapper} from '../context'; | ||
| // Types-only import. | ||
| import {Request, Response, NextFunction} from 'express'; | ||
| import {makeHttpRequestData} from './make-http-request'; | ||
|
|
||
| const CHILD_LOG_NAME_SUFFIX = 'applog'; | ||
|
|
||
| export interface AnnotatedRequestType<LoggerType> extends Request { | ||
| log: LoggerType; | ||
| } | ||
|
|
||
| export function makeMiddleware<LoggerType>( | ||
| projectId: string, | ||
| emitRequestLog: (httpRequest: HttpRequest, trace: string) => void, | ||
| makeChildLogger: (childSuffix: string, trace: string) => LoggerType) { | ||
| return (req: Request, res: Response, next: NextFunction) => { | ||
| // TODO(ofrobots): use high-resolution timer. | ||
| const requestStartMs = Date.now(); | ||
|
|
||
| const wrapper = makeHeaderWrapper(req); | ||
|
|
||
| const spanContext = getOrInjectContext(wrapper); | ||
| const trace = `projects/${projectId}/traces/${spanContext.traceId}`; | ||
|
|
||
| // Install a child logger on the request object. | ||
| (req as AnnotatedRequestType<LoggerType>).log = | ||
| makeChildLogger(CHILD_LOG_NAME_SUFFIX, trace); | ||
|
|
||
| // Emit a 'Request Log' on the parent logger. | ||
| onFinished(res, () => { | ||
| const latencyMs = Date.now() - requestStartMs; | ||
| const httpRequest = makeHttpRequestData(req, res, latencyMs); | ||
| emitRequestLog(httpRequest, trace); | ||
| }); | ||
|
|
||
| next(); | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /*! | ||
| * Copyright 2018 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import * as assert from 'assert'; | ||
| // Types-only import. | ||
| import {Request, Response} from 'express'; | ||
| import {makeHttpRequestData} from '../../../src/middleware/express/make-http-request'; | ||
|
|
||
| describe('middleware/express/make-http-request', () => { | ||
| it('should convert latency to proto Duration', () => { | ||
| const fakeRequest = {headers: {}}; | ||
| const fakeResponse = {}; | ||
|
|
||
| const h1 = makeHttpRequestData( | ||
| fakeRequest as Request, fakeResponse as Response, 1003); | ||
| assert.deepStrictEqual(h1.latency, {seconds: 1, nanos: 3e6}); | ||
|
|
||
| const h2 = makeHttpRequestData( | ||
| fakeRequest as Request, fakeResponse as Response, 9003.1); | ||
| assert.deepStrictEqual(h2.latency, {seconds: 9, nanos: 3.1e6}); | ||
|
|
||
| // Make sure we nanos is uint32. | ||
| const h3 = makeHttpRequestData( | ||
| fakeRequest as Request, fakeResponse as Response, 1.0000000001); | ||
| assert.deepStrictEqual(h3.latency, {seconds: 0, nanos: 1e6}); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| /*! | ||
| * Copyright 2018 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import * as assert from 'assert'; | ||
| import * as EventEmitter from 'events'; | ||
| import * as extend from 'extend'; | ||
| import * as proxyquire from 'proxyquire'; | ||
|
|
||
| const FAKE_PROJECT_ID = 'project-🦄'; | ||
|
|
||
| function makeFakeRequest() { | ||
| return {headers: {'content-type': 'application/🍰'}}; | ||
| } | ||
|
|
||
| function makeFakeResponse() { | ||
| const ee = new EventEmitter(); | ||
| // tslint:disable-next-line:no-any | ||
| (ee as any).getHeader = (name: string) => {}; | ||
| return ee; | ||
| } | ||
|
|
||
| let getOrInjectContextValue; | ||
| const FAKE_CONTEXT = { | ||
| getOrInjectContext: () => { | ||
| return getOrInjectContextValue; | ||
| } | ||
| }; | ||
|
|
||
|
|
||
| describe('middleware/express/make-middleware', () => { | ||
| describe('makeMiddleware', () => { | ||
| const {makeMiddleware} = proxyquire( | ||
| '../../../src/middleware/express/make-middleware', | ||
| {'../context': FAKE_CONTEXT}); | ||
|
|
||
| it('should return a function accepting 3 arguments', () => { | ||
| const middleware = makeMiddleware(FAKE_PROJECT_ID, () => {}, () => {}); | ||
| assert.ok(typeof middleware === 'function'); | ||
| assert.ok(middleware.length === 3); | ||
| }); | ||
|
|
||
| describe('middleware', () => { | ||
| const FAKE_SPAN_CONTEXT = {traceId: 'traceId-🥑'}; | ||
|
|
||
| beforeEach(() => { | ||
| getOrInjectContextValue = undefined; | ||
| }); | ||
|
|
||
| it('should call the next middleware synchronously', () => { | ||
| getOrInjectContextValue = FAKE_SPAN_CONTEXT; | ||
| const fakeRequest = makeFakeRequest(); | ||
| const fakeResponse = makeFakeResponse(); | ||
| let called = false; | ||
|
|
||
| const middleware = makeMiddleware(FAKE_PROJECT_ID, () => {}, () => {}); | ||
|
|
||
| middleware(fakeRequest, fakeResponse, () => { | ||
| called = true; | ||
| }); | ||
| assert.ok(called); | ||
| }); | ||
|
|
||
| it('should call makeChildLogger with correct trace context', () => { | ||
| const FAKE_CHILD_LOGGER = {log: '🍌'}; | ||
| getOrInjectContextValue = FAKE_SPAN_CONTEXT; | ||
| const fakeRequest = makeFakeRequest(); | ||
| const fakeResponse = makeFakeResponse(); | ||
|
|
||
| function makeChild(childSuffix, trace) { | ||
| assert.strictEqual( | ||
| trace, | ||
| `projects/${FAKE_PROJECT_ID}/traces/${ | ||
| FAKE_SPAN_CONTEXT.traceId}`); | ||
| assert.strictEqual(typeof childSuffix, 'string'); | ||
| return FAKE_CHILD_LOGGER; | ||
| } | ||
|
|
||
| const middleware = makeMiddleware(FAKE_PROJECT_ID, () => {}, makeChild); | ||
| middleware(fakeRequest, fakeResponse, () => {}); | ||
|
|
||
| // Should annotate the request with the child logger. | ||
| // tslint:disable-next-line:no-any | ||
| assert.strictEqual((fakeRequest as any).log, FAKE_CHILD_LOGGER); | ||
| }); | ||
|
|
||
| it('should emit a request log when response is finished', (done) => { | ||
| getOrInjectContextValue = FAKE_SPAN_CONTEXT; | ||
| const fakeRequest = makeFakeRequest(); | ||
| const fakeResponse = makeFakeResponse(); | ||
| let emitRequestLogCalled = false; | ||
|
|
||
| function emitRequestLog(httpRequest, trace) { | ||
| assert.strictEqual( | ||
| trace, | ||
| `projects/${FAKE_PROJECT_ID}/traces/${ | ||
| FAKE_SPAN_CONTEXT.traceId}`); | ||
| // TODO: check httpRequest properties. | ||
| emitRequestLogCalled = true; | ||
| } | ||
|
|
||
| const middleware = | ||
| makeMiddleware(FAKE_PROJECT_ID, emitRequestLog, () => {}); | ||
| middleware(fakeRequest, fakeResponse, () => {}); | ||
|
|
||
| setTimeout(() => { | ||
| fakeResponse.emit('finished'); | ||
| assert.strictEqual(emitRequestLogCalled, true); | ||
| done(); | ||
| }, 10); | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.