1
1
/* eslint-disable max-lines */
2
2
import type {
3
3
AppContext ,
4
+ CloudResourceContext ,
4
5
Contexts ,
5
6
CultureContext ,
6
7
DeviceContext ,
@@ -29,6 +30,7 @@ interface ContextOptions {
29
30
os ?: boolean ;
30
31
device ?: DeviceContextOptions | boolean ;
31
32
culture ?: boolean ;
33
+ cloudResource ?: boolean ;
32
34
}
33
35
34
36
/** Add node modules / packages to the event */
@@ -48,9 +50,15 @@ export class Context implements Integration {
48
50
*/
49
51
private _cachedContext : Promise < Contexts > | undefined ;
50
52
51
- public constructor ( private readonly _options : ContextOptions = { app : true , os : true , device : true , culture : true } ) {
52
- //
53
- }
53
+ public constructor (
54
+ private readonly _options : ContextOptions = {
55
+ app : true ,
56
+ os : true ,
57
+ device : true ,
58
+ culture : true ,
59
+ cloudResource : true ,
60
+ } ,
61
+ ) { }
54
62
55
63
/**
56
64
* @inheritDoc
@@ -73,6 +81,7 @@ export class Context implements Integration {
73
81
os : { ...updatedContext . os , ...event . contexts ?. os } ,
74
82
device : { ...updatedContext . device , ...event . contexts ?. device } ,
75
83
culture : { ...updatedContext . culture , ...event . contexts ?. culture } ,
84
+ cloud_resource : { ...updatedContext . cloud_resource , ...event . contexts ?. cloud_resource } ,
76
85
} ;
77
86
78
87
return event ;
@@ -120,6 +129,10 @@ export class Context implements Integration {
120
129
}
121
130
}
122
131
132
+ if ( this . _options . cloudResource ) {
133
+ contexts . cloud_resource = getCloudResourceContext ( ) ;
134
+ }
135
+
123
136
return contexts ;
124
137
}
125
138
}
@@ -380,3 +393,72 @@ async function getLinuxInfo(): Promise<OsContext> {
380
393
381
394
return linuxInfo ;
382
395
}
396
+
397
+ /**
398
+ * Grabs some information about hosting provider based on best effort.
399
+ */
400
+ function getCloudResourceContext ( ) : CloudResourceContext | undefined {
401
+ if ( process . env . VERCEL ) {
402
+ // https://vercel.com/docs/concepts/projects/environment-variables/system-environment-variables#system-environment-variables
403
+ return {
404
+ 'cloud.provider' : 'vercel' ,
405
+ 'cloud.region' : process . env . VERCEL_REGION ,
406
+ } ;
407
+ } else if ( process . env . AWS_REGION ) {
408
+ // https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html
409
+ return {
410
+ 'cloud.provider' : 'aws' ,
411
+ 'cloud.region' : process . env . AWS_REGION ,
412
+ 'cloud.platform' : process . env . AWS_EXECUTION_ENV ,
413
+ } ;
414
+ } else if ( process . env . GCP_PROJECT ) {
415
+ // https://cloud.google.com/composer/docs/how-to/managing/environment-variables#reserved_variables
416
+ return {
417
+ 'cloud.provider' : 'gcp' ,
418
+ } ;
419
+ } else if ( process . env . ALIYUN_REGION_ID ) {
420
+ // TODO: find where I found these environment variables - at least gc.github.com returns something
421
+ return {
422
+ 'cloud.provider' : 'alibaba_cloud' ,
423
+ 'cloud.region' : process . env . ALIYUN_REGION_ID ,
424
+ } ;
425
+ } else if ( process . env . WEBSITE_SITE_NAME && process . env . REGION_NAME ) {
426
+ // https://learn.microsoft.com/en-us/azure/app-service/reference-app-settings?tabs=kudu%2Cdotnet#app-environment
427
+ return {
428
+ 'cloud.provider' : 'azure' ,
429
+ 'cloud.region' : process . env . REGION_NAME ,
430
+ } ;
431
+ } else if ( process . env . IBM_CLOUD_REGION ) {
432
+ // TODO: find where I found these environment variables - at least gc.github.com returns something
433
+ return {
434
+ 'cloud.provider' : 'ibm_cloud' ,
435
+ 'cloud.region' : process . env . IBM_CLOUD_REGION ,
436
+ } ;
437
+ } else if ( process . env . TENCENTCLOUD_REGION ) {
438
+ // https://www.tencentcloud.com/document/product/583/32748
439
+ return {
440
+ 'cloud.provider' : 'tencent_cloud' ,
441
+ 'cloud.region' : process . env . TENCENTCLOUD_REGION ,
442
+ 'cloud.account.id' : process . env . TENCENTCLOUD_APPID ,
443
+ 'cloud.availability_zone' : process . env . TENCENTCLOUD_ZONE ,
444
+ } ;
445
+ } else if ( process . env . NETLIFY ) {
446
+ // https://docs.netlify.com/configure-builds/environment-variables/#read-only-variables
447
+ return {
448
+ 'cloud.provider' : 'netlify' ,
449
+ } ;
450
+ } else if ( process . env . FLY_REGION ) {
451
+ // https://fly.io/docs/reference/runtime-environment/
452
+ return {
453
+ 'cloud.provider' : 'fly.io' ,
454
+ 'cloud.region' : process . env . FLY_REGION ,
455
+ } ;
456
+ } else if ( process . env . DYNO ) {
457
+ // https://devcenter.heroku.com/articles/dynos#local-environment-variables
458
+ return {
459
+ 'cloud.provider' : 'heroku' ,
460
+ } ;
461
+ } else {
462
+ return undefined ;
463
+ }
464
+ }
0 commit comments