From a53bcb3c975010abcd5893c02cd22492286d71a2 Mon Sep 17 00:00:00 2001 From: Michael Lumish Date: Wed, 13 May 2020 12:00:22 -0700 Subject: [PATCH] grpc-js: Add a few basic trace lines to the server --- packages/grpc-js/src/server-call.ts | 15 ++++++++++++++- packages/grpc-js/src/server.ts | 20 ++++++++++++++++---- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/packages/grpc-js/src/server-call.ts b/packages/grpc-js/src/server-call.ts index fc456fda..74b95057 100644 --- a/packages/grpc-js/src/server-call.ts +++ b/packages/grpc-js/src/server-call.ts @@ -20,12 +20,19 @@ import * as http2 from 'http2'; import { Duplex, Readable, Writable } from 'stream'; import { StatusObject } from './call-stream'; -import { Status, DEFAULT_MAX_SEND_MESSAGE_LENGTH, DEFAULT_MAX_RECEIVE_MESSAGE_LENGTH } from './constants'; +import { Status, DEFAULT_MAX_SEND_MESSAGE_LENGTH, DEFAULT_MAX_RECEIVE_MESSAGE_LENGTH, LogVerbosity } from './constants'; import { Deserialize, Serialize } from './make-client'; import { Metadata } from './metadata'; import { StreamDecoder } from './stream-decoder'; import { ObjectReadable, ObjectWritable } from './object-stream'; import { ChannelOptions } from './channel-options'; +import * as logging from './logging'; + +const TRACER_NAME = 'server_call'; + +function trace(text: string): void { + logging.trace(LogVerbosity.DEBUG, TRACER_NAME, text); +} interface DeadlineUnitIndexSignature { [name: string]: number; @@ -294,6 +301,7 @@ export interface UnaryHandler { serialize: Serialize; deserialize: Deserialize; type: HandlerType; + path: string; } export interface ClientStreamingHandler { @@ -301,6 +309,7 @@ export interface ClientStreamingHandler { serialize: Serialize; deserialize: Deserialize; type: HandlerType; + path: string; } export interface ServerStreamingHandler { @@ -308,6 +317,7 @@ export interface ServerStreamingHandler { serialize: Serialize; deserialize: Deserialize; type: HandlerType; + path: string; } export interface BidiStreamingHandler { @@ -315,6 +325,7 @@ export interface BidiStreamingHandler { serialize: Serialize; deserialize: Deserialize; type: HandlerType; + path: string; } export type Handler = @@ -521,6 +532,8 @@ export class Http2ServerCallStream< return; } + trace('Request to method ' + this.handler?.path + ' ended with status code: ' + Status[statusObj.code] + ' details: ' + statusObj.details); + clearTimeout(this.deadline); if (!this.wantTrailers) { diff --git a/packages/grpc-js/src/server.ts b/packages/grpc-js/src/server.ts index c8b8f2b2..f5de7958 100644 --- a/packages/grpc-js/src/server.ts +++ b/packages/grpc-js/src/server.ts @@ -46,14 +46,21 @@ import { import { ServerCredentials } from './server-credentials'; import { ChannelOptions } from './channel-options'; import { createResolver, ResolverListener, mapUriDefaultScheme } from './resolver'; -import { log } from './logging'; +import * as logging from './logging'; import { SubchannelAddress, TcpSubchannelAddress, isTcpSubchannelAddress, + subchannelAddressToString, } from './subchannel'; import { parseUri } from './uri-parser'; +const TRACER_NAME = 'server'; + +function trace(text: string): void { + logging.trace(LogVerbosity.DEBUG, TRACER_NAME, text); +} + interface BindResult { port: number; count: number; @@ -269,6 +276,7 @@ export class Server { } return Promise.all( addressList.map((address) => { + trace('Attempting to bind ' + subchannelAddressToString(address)); let addr: SubchannelAddress; if (isTcpSubchannelAddress(address)) { addr = { @@ -288,6 +296,7 @@ export class Server { http2Server.once('error', onError); http2Server.listen(addr, () => { + trace('Successfully bound ' + subchannelAddressToString(address)); this.http2ServerList.push(http2Server); const boundAddress = http2Server.address()!; if (typeof boundAddress === 'string') { @@ -378,11 +387,11 @@ export class Server { (bindResult) => { if (bindResult.count === 0) { const errorString = `No address added out of total ${addressList.length} resolved`; - log(LogVerbosity.ERROR, errorString); + logging.log(LogVerbosity.ERROR, errorString); callback(new Error(errorString), 0); } else { if (bindResult.count < addressList.length) { - log( + logging.log( LogVerbosity.INFO, `WARNING Only ${bindResult.count} addresses added out of total ${addressList.length} resolved` ); @@ -392,7 +401,7 @@ export class Server { }, (error) => { const errorString = `No address added out of total ${addressList.length} resolved`; - log(LogVerbosity.ERROR, errorString); + logging.log(LogVerbosity.ERROR, errorString); callback(new Error(errorString), 0); } ); @@ -444,6 +453,7 @@ export class Server { serialize, deserialize, type, + path: name, } as UntypedHandler); return true; } @@ -528,9 +538,11 @@ export class Server { try { const path = headers[http2.constants.HTTP2_HEADER_PATH] as string; + trace('Received call to method ' + path + ' at address ' + http2Server.address()?.toString()); const handler = this.handlers.get(path); if (handler === undefined) { + trace('No handler registered for method ' + path + '. Sending UNIMPLEMENTED status.'); throw getUnimplementedStatusResponse(path); }