Skip to content

Commit

Permalink
feat: add createMetadataHook to set request tracing info (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored Aug 24, 2023
1 parent 5e78c8a commit 7ca7aa6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/client/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,26 @@ import { Metadata } from '@grpc/grpc-js';
import { KV, RequestWithMeta, Map } from '../types/common';
import { mergeMetadataToMap } from '../utils';

export type CreateMetadataHook = (localStorage?: AsyncLocalStorage<any>) => Record<string, string>;

export interface APIOptions {
logger?: Console;
localStorage?: AsyncLocalStorage<any>;
/**
* Setting more request metadata here, e.g.: tracing headers
*/
createMetadataHook?: CreateMetadataHook;
}

export class API {
protected readonly localStorage?: AsyncLocalStorage<any>;
protected readonly logger: Console;
#createMetadataHook?: CreateMetadataHook;

constructor(options?: APIOptions) {
this.localStorage = options?.localStorage;
this.logger = options?.logger ?? global.console;
this.#createMetadataHook = options?.createMetadataHook;
}

createMetadata(request: RequestWithMeta<{}>, defaultRequestMeta?: Record<string, string>): Metadata {
Expand All @@ -45,6 +53,13 @@ export class API {
metadata.add(key, request.requestMeta[key]);
}
}

if (this.#createMetadataHook) {
const moreMetadata = this.#createMetadataHook(this.localStorage);
for (const key in moreMetadata) {
metadata.add(key, moreMetadata[key]);
}
}
return metadata;
}

Expand Down
5 changes: 5 additions & 0 deletions src/client/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import File from './File';
import Binding from './Binding';
import Oss, { OssOptions } from './Oss';
import Cryption, { CryptionOptions } from './Cryption';
import type { CreateMetadataHook } from './API';

const debug = debuglog('layotto:client:main');

Expand All @@ -39,13 +40,15 @@ export interface ClientOptions {
cryption?: CryptionOptions;
logger?: Console;
localStorage?: AsyncLocalStorage<any>;
createMetadataHook?: CreateMetadataHook;
}

export default class Client {
readonly host: string;
readonly port: string;
protected readonly localStorage?: AsyncLocalStorage<any>;
protected readonly logger: Console;
protected readonly createMetadataHook?: CreateMetadataHook;
protected readonly _runtime: RuntimeClient;
private readonly _address: string;
private readonly _ossClient: ObjectStorageServiceClient;
Expand Down Expand Up @@ -77,6 +80,7 @@ export default class Client {
this._address = address;
this.localStorage = options?.localStorage;
this.logger = options?.logger ?? global.console;
this.createMetadataHook = options?.createMetadataHook;
const clientCredentials = ChannelCredentials.createInsecure();
this._runtime = new RuntimeClient(this._address, clientCredentials);
debug('Start connection to %o', this._address);
Expand All @@ -94,6 +98,7 @@ export default class Client {
return {
localStorage: this.localStorage,
logger: this.logger,
createMetadataHook: this.createMetadataHook,
};
}

Expand Down
9 changes: 9 additions & 0 deletions test/unit/client/Client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
import { strict as assert } from 'node:assert';
import { Client } from '../../../src';
import { CreateMetadataHook } from '../../../src/client/API';
import { CustomClient } from './fixtures/CustomClient';

describe('client/Client.test.ts', () => {
Expand All @@ -34,8 +35,16 @@ describe('client/Client.test.ts', () => {
describe('custom Client', () => {
let customClient: CustomClient;
beforeAll(() => {
const createMetadataHook: CreateMetadataHook = localStorage => {
return {
'x-localStorage': localStorage?.getStore() ?? 'not-exists',
'x-foo': 'bar',
};
};

customClient = new CustomClient({
logger: console,
createMetadataHook,
});
});

Expand Down

0 comments on commit 7ca7aa6

Please sign in to comment.