-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(NODE-4696): include FAAS metadata in the mongodb handshake #3616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c623356
1b86b2d
ed44549
da62ec4
a98a423
ef58d3f
51ae18b
6853019
c81552a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| import { calculateObjectSize, Int32 } from 'bson'; | ||
| import * as os from 'os'; | ||
|
|
||
| import type { MongoOptions } from '../../mongo_client'; | ||
| import { applyFaasEnvMetadata } from './faas_provider'; | ||
|
|
||
| /** | ||
| * @public | ||
| * @see https://github.com/mongodb/specifications/blob/master/source/mongodb-handshake/handshake.rst#hello-command | ||
| */ | ||
| export interface ClientMetadata { | ||
| driver: { | ||
| name: string; | ||
| version: string; | ||
| }; | ||
| os: { | ||
| type: string; | ||
| name: NodeJS.Platform; | ||
| architecture: string; | ||
| version: string; | ||
| }; | ||
| platform: string; | ||
| application?: { | ||
| name: string; | ||
| }; | ||
|
|
||
| /** Data containing information about the environment, if the driver is running in a FAAS environment. */ | ||
| env?: { | ||
| name: 'aws.lambda' | 'gcp.func' | 'azure.func' | 'vercel'; | ||
| timeout_sec?: Int32; | ||
| memory_mb?: Int32; | ||
| region?: string; | ||
| url?: string; | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * @internal | ||
| * truncates the client metadata according to the priority outlined here | ||
| * https://github.com/mongodb/specifications/blob/master/source/mongodb-handshake/handshake.rst#limitations | ||
| */ | ||
| export function truncateClientMetadata(metadata: ClientMetadata): ClientMetadata { | ||
| if (calculateObjectSize(metadata) <= 512) { | ||
| return metadata; | ||
| } | ||
| // 1. Truncate ``platform``. | ||
| // no-op - we don't truncate because the `platform` field is essentially a fixed length in Node | ||
| // and there isn't anything we can truncate that without removing useful information. | ||
|
|
||
| // 2. Omit fields from ``env`` except ``env.name``. | ||
| if (metadata.env) { | ||
| metadata.env = { name: metadata.env.name }; | ||
| } | ||
| if (calculateObjectSize(metadata) <= 512) { | ||
| return metadata; | ||
| } | ||
|
|
||
| // 3. Omit the ``env`` document entirely. | ||
| delete metadata.env; | ||
|
|
||
| return metadata; | ||
| } | ||
|
|
||
| /** @public */ | ||
| export interface ClientMetadataOptions { | ||
| driverInfo?: { | ||
| name?: string; | ||
| version?: string; | ||
| platform?: string; | ||
| }; | ||
| appName?: string; | ||
| } | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-var-requires | ||
| const NODE_DRIVER_VERSION = require('../../../package.json').version; | ||
|
|
||
| export function makeClientMetadata( | ||
| options: Pick<MongoOptions, 'appName' | 'driverInfo'> | ||
| ): ClientMetadata { | ||
| const name = options.driverInfo.name ? `nodejs|${options.driverInfo.name}` : 'nodejs'; | ||
| const version = options.driverInfo.version | ||
| ? `${NODE_DRIVER_VERSION}|${options.driverInfo.version}` | ||
| : NODE_DRIVER_VERSION; | ||
| const platform = options.driverInfo.platform | ||
| ? `Node.js ${process.version}, ${os.endianness()}|${options.driverInfo.platform}` | ||
| : `Node.js ${process.version}, ${os.endianness()}`; | ||
|
|
||
| const metadata: ClientMetadata = { | ||
| driver: { | ||
| name, | ||
| version | ||
| }, | ||
| os: { | ||
| type: os.type(), | ||
| name: process.platform, | ||
| architecture: process.arch, | ||
| version: os.release() | ||
| }, | ||
| platform | ||
| }; | ||
|
|
||
| if (options.appName) { | ||
| // MongoDB requires the appName not exceed a byte length of 128 | ||
| const name = | ||
| Buffer.byteLength(options.appName, 'utf8') <= 128 | ||
| ? options.appName | ||
| : Buffer.from(options.appName, 'utf8').subarray(0, 128).toString('utf8'); | ||
| metadata.application = { name }; | ||
| } | ||
|
|
||
| return truncateClientMetadata(applyFaasEnvMetadata(metadata)); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import { Int32 } from 'bson'; | ||
|
|
||
| import { identity } from '../../utils'; | ||
| import type { ClientMetadata } from './client_metadata'; | ||
|
|
||
| export type FAASProvider = 'aws' | 'gcp' | 'azure' | 'vercel' | 'none'; | ||
|
|
||
| function isNonEmptyString(s: string | undefined): s is string { | ||
| return typeof s === 'string' && s.length > 0; | ||
| } | ||
|
|
||
| export function determineFAASProvider(): FAASProvider { | ||
| const awsPresent = | ||
| isNonEmptyString(process.env.AWS_EXECUTION_ENV) || | ||
| isNonEmptyString(process.env.AWS_LAMBDA_RUNTIME_API); | ||
| const azurePresent = isNonEmptyString(process.env.FUNCTIONS_WORKER_RUNTIME); | ||
| const gcpPresent = | ||
| isNonEmptyString(process.env.K_SERVICE) || isNonEmptyString(process.env.FUNCTION_NAME); | ||
| const vercelPresent = isNonEmptyString(process.env.VERCEL); | ||
|
|
||
| const numberOfProvidersPresent = [awsPresent, azurePresent, gcpPresent, vercelPresent].filter( | ||
| identity | ||
| ).length; | ||
|
|
||
| if (numberOfProvidersPresent !== 1) { | ||
| return 'none'; | ||
| } | ||
|
|
||
| if (awsPresent) return 'aws'; | ||
| if (azurePresent) return 'azure'; | ||
| if (gcpPresent) return 'gcp'; | ||
| return 'vercel'; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should vercel come before aws? IIRC vercel runs on AWS lambda is it possible aws env vars are also defined in vercel?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my comment above – they will never both be set. If they are, we explicitly determine none |
||
| } | ||
|
|
||
| function applyAzureMetadata(m: ClientMetadata): ClientMetadata { | ||
| m.env = { name: 'azure.func' }; | ||
| return m; | ||
| } | ||
|
|
||
| function applyGCPMetadata(m: ClientMetadata): ClientMetadata { | ||
| m.env = { name: 'gcp.func' }; | ||
|
|
||
| const memory_mb = Number(process.env.FUNCTION_MEMORY_MB); | ||
| if (Number.isInteger(memory_mb)) { | ||
| m.env.memory_mb = new Int32(memory_mb); | ||
| } | ||
| const timeout_sec = Number(process.env.FUNCTION_TIMEOUT_SEC); | ||
| if (Number.isInteger(timeout_sec)) { | ||
| m.env.timeout_sec = new Int32(timeout_sec); | ||
| } | ||
| if (isNonEmptyString(process.env.FUNCTION_REGION)) { | ||
| m.env.region = process.env.FUNCTION_REGION; | ||
| } | ||
|
|
||
| return m; | ||
| } | ||
|
|
||
| function applyAWSMetadata(m: ClientMetadata): ClientMetadata { | ||
| m.env = { name: 'aws.lambda' }; | ||
| if (isNonEmptyString(process.env.AWS_REGION)) { | ||
| m.env.region = process.env.AWS_REGION; | ||
| } | ||
| const memory_mb = Number(process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE); | ||
| if (Number.isInteger(memory_mb)) { | ||
| m.env.memory_mb = new Int32(memory_mb); | ||
| } | ||
| return m; | ||
| } | ||
|
|
||
| function applyVercelMetadata(m: ClientMetadata): ClientMetadata { | ||
| m.env = { name: 'vercel' }; | ||
| if (isNonEmptyString(process.env.VERCEL_URL)) { | ||
| m.env.url = process.env.VERCEL_URL; | ||
| } | ||
| if (isNonEmptyString(process.env.VERCEL_REGION)) { | ||
| m.env.region = process.env.VERCEL_REGION; | ||
| } | ||
| return m; | ||
| } | ||
|
|
||
| export function applyFaasEnvMetadata(metadata: ClientMetadata): ClientMetadata { | ||
| const handlerMap: Record<FAASProvider, (m: ClientMetadata) => ClientMetadata> = { | ||
| aws: applyAWSMetadata, | ||
| gcp: applyGCPMetadata, | ||
| azure: applyAzureMetadata, | ||
| vercel: applyVercelMetadata, | ||
| none: identity | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of adding a step for none, can't none do nothing to the input? maybe none could instead be represented by a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. an early return based on null should narrow the key to be one of the acceptable strings, this way we can write less handling for the obvious case.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That still requires handling for the obvious case, no? it's just that you've put it in a null check. I like this approach because there's no special casing values (or a "lack" of value here, indicated by |
||
| }; | ||
| const faasProvider = determineFAASProvider(); | ||
|
|
||
| const faasMetadataProvider = handlerMap[faasProvider]; | ||
| return faasMetadataProvider(metadata); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be an ifelse sequence instead?, we have the order encoded below with the early returns, I'd just add an else case to the end of that and return none.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, look closely at the logic. It purposefully returns none when there's more than one faas provider present