|
| 1 | +import { promises as fs } from 'fs'; |
1 | 2 | import * as os from 'os';
|
2 | 3 | import * as process from 'process';
|
3 | 4 |
|
4 |
| -import { BSON, Int32 } from '../../bson'; |
| 5 | +import { BSON, type Document, Int32 } from '../../bson'; |
5 | 6 | import { MongoInvalidArgumentError } from '../../error';
|
6 | 7 | import type { MongoOptions } from '../../mongo_client';
|
7 | 8 |
|
@@ -71,13 +72,13 @@ export class LimitedSizeDocument {
|
71 | 72 | return true;
|
72 | 73 | }
|
73 | 74 |
|
74 |
| - toObject(): ClientMetadata { |
| 75 | + toObject(): Document { |
75 | 76 | return BSON.deserialize(BSON.serialize(this.document), {
|
76 | 77 | promoteLongs: false,
|
77 | 78 | promoteBuffers: false,
|
78 | 79 | promoteValues: false,
|
79 | 80 | useBigInt64: false
|
80 |
| - }) as ClientMetadata; |
| 81 | + }); |
81 | 82 | }
|
82 | 83 | }
|
83 | 84 |
|
@@ -152,8 +153,57 @@ export function makeClientMetadata(options: MakeClientMetadataOptions): ClientMe
|
152 | 153 | }
|
153 | 154 | }
|
154 | 155 | }
|
| 156 | + return metadataDocument.toObject() as ClientMetadata; |
| 157 | +} |
| 158 | + |
| 159 | +let dockerPromise: Promise<boolean>; |
| 160 | +/** @internal */ |
| 161 | +async function getContainerMetadata() { |
| 162 | + const containerMetadata: Record<string, any> = {}; |
| 163 | + dockerPromise ??= fs.access('/.dockerenv').then( |
| 164 | + () => true, |
| 165 | + () => false |
| 166 | + ); |
| 167 | + const isDocker = await dockerPromise; |
| 168 | + |
| 169 | + const { KUBERNETES_SERVICE_HOST = '' } = process.env; |
| 170 | + const isKubernetes = KUBERNETES_SERVICE_HOST.length > 0 ? true : false; |
| 171 | + |
| 172 | + if (isDocker) containerMetadata.runtime = 'docker'; |
| 173 | + if (isKubernetes) containerMetadata.orchestrator = 'kubernetes'; |
| 174 | + |
| 175 | + return containerMetadata; |
| 176 | +} |
| 177 | + |
| 178 | +/** |
| 179 | + * @internal |
| 180 | + * Re-add each metadata value. |
| 181 | + * Attempt to add new env container metadata, but keep old data if it does not fit. |
| 182 | + */ |
| 183 | +export async function addContainerMetadata(originalMetadata: ClientMetadata) { |
| 184 | + const containerMetadata = await getContainerMetadata(); |
| 185 | + if (Object.keys(containerMetadata).length === 0) return originalMetadata; |
| 186 | + |
| 187 | + const extendedMetadata = new LimitedSizeDocument(512); |
| 188 | + |
| 189 | + const extendedEnvMetadata = { ...originalMetadata?.env, container: containerMetadata }; |
| 190 | + |
| 191 | + for (const [key, val] of Object.entries(originalMetadata)) { |
| 192 | + if (key !== 'env') { |
| 193 | + extendedMetadata.ifItFitsItSits(key, val); |
| 194 | + } else { |
| 195 | + if (!extendedMetadata.ifItFitsItSits('env', extendedEnvMetadata)) { |
| 196 | + // add in old data if newer / extended metadata does not fit |
| 197 | + extendedMetadata.ifItFitsItSits('env', val); |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + if (!('env' in originalMetadata)) { |
| 203 | + extendedMetadata.ifItFitsItSits('env', extendedEnvMetadata); |
| 204 | + } |
155 | 205 |
|
156 |
| - return metadataDocument.toObject(); |
| 206 | + return extendedMetadata.toObject(); |
157 | 207 | }
|
158 | 208 |
|
159 | 209 | /**
|
|
0 commit comments