Skip to content

feat(node): Add basic cloud resource context #8764

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

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 85 additions & 3 deletions packages/node/src/integrations/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable max-lines */
import type {
AppContext,
CloudResourceContext,
Contexts,
CultureContext,
DeviceContext,
Expand Down Expand Up @@ -29,6 +30,7 @@ interface ContextOptions {
os?: boolean;
device?: DeviceContextOptions | boolean;
culture?: boolean;
cloudResource?: boolean;
}

/** Add node modules / packages to the event */
Expand All @@ -48,9 +50,15 @@ export class Context implements Integration {
*/
private _cachedContext: Promise<Contexts> | undefined;

public constructor(private readonly _options: ContextOptions = { app: true, os: true, device: true, culture: true }) {
//
}
public constructor(
private readonly _options: ContextOptions = {
app: true,
os: true,
device: true,
culture: true,
cloudResource: true,
},
) {}

/**
* @inheritDoc
Expand All @@ -73,6 +81,7 @@ export class Context implements Integration {
os: { ...updatedContext.os, ...event.contexts?.os },
device: { ...updatedContext.device, ...event.contexts?.device },
culture: { ...updatedContext.culture, ...event.contexts?.culture },
cloud_resource: { ...updatedContext.cloud_resource, ...event.contexts?.cloud_resource },
};

return event;
Expand Down Expand Up @@ -120,6 +129,10 @@ export class Context implements Integration {
}
}

if (this._options.cloudResource) {
contexts.cloud_resource = getCloudResourceContext();
}

return contexts;
}
}
Expand Down Expand Up @@ -380,3 +393,72 @@ async function getLinuxInfo(): Promise<OsContext> {

return linuxInfo;
}

/**
* Grabs some information about hosting provider based on best effort.
*/
function getCloudResourceContext(): CloudResourceContext | undefined {
if (process.env.VERCEL) {
// https://vercel.com/docs/concepts/projects/environment-variables/system-environment-variables#system-environment-variables
return {
'cloud.provider': 'vercel',
'cloud.region': process.env.VERCEL_REGION,
};
} else if (process.env.AWS_REGION) {
// https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html
return {
'cloud.provider': 'aws',
'cloud.region': process.env.AWS_REGION,
'cloud.platform': process.env.AWS_EXECUTION_ENV,
};
} else if (process.env.GCP_PROJECT) {
// https://cloud.google.com/composer/docs/how-to/managing/environment-variables#reserved_variables
return {
'cloud.provider': 'gcp',
};
} else if (process.env.ALIYUN_REGION_ID) {
// TODO: find where I found these environment variables - at least gc.github.com returns something
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that I haven't figured this out yet. This is not a leftover in this PR but an actual todo.

return {
'cloud.provider': 'alibaba_cloud',
'cloud.region': process.env.ALIYUN_REGION_ID,
};
} else if (process.env.WEBSITE_SITE_NAME && process.env.REGION_NAME) {
// https://learn.microsoft.com/en-us/azure/app-service/reference-app-settings?tabs=kudu%2Cdotnet#app-environment
return {
'cloud.provider': 'azure',
'cloud.region': process.env.REGION_NAME,
};
} else if (process.env.IBM_CLOUD_REGION) {
// TODO: find where I found these environment variables - at least gc.github.com returns something
return {
'cloud.provider': 'ibm_cloud',
'cloud.region': process.env.IBM_CLOUD_REGION,
};
} else if (process.env.TENCENTCLOUD_REGION) {
// https://www.tencentcloud.com/document/product/583/32748
return {
'cloud.provider': 'tencent_cloud',
'cloud.region': process.env.TENCENTCLOUD_REGION,
'cloud.account.id': process.env.TENCENTCLOUD_APPID,
'cloud.availability_zone': process.env.TENCENTCLOUD_ZONE,
};
} else if (process.env.NETLIFY) {
// https://docs.netlify.com/configure-builds/environment-variables/#read-only-variables
return {
'cloud.provider': 'netlify',
};
} else if (process.env.FLY_REGION) {
// https://fly.io/docs/reference/runtime-environment/
return {
'cloud.provider': 'fly.io',
'cloud.region': process.env.FLY_REGION,
};
} else if (process.env.DYNO) {
// https://devcenter.heroku.com/articles/dynos#local-environment-variables
return {
'cloud.provider': 'heroku',
};
} else {
return undefined;
}
}
11 changes: 11 additions & 0 deletions packages/types/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface Contexts extends Record<string, Context | undefined> {
culture?: CultureContext;
response?: ResponseContext;
trace?: TraceContext;
cloud_resource?: CloudResourceContext;
}

export interface AppContext extends Record<string, unknown> {
Expand Down Expand Up @@ -93,3 +94,13 @@ export interface TraceContext extends Record<string, unknown> {
tags?: { [key: string]: Primitive };
trace_id: string;
}

export interface CloudResourceContext extends Record<string, unknown> {
['cloud.provider']?: string;
['cloud.account.id']?: string;
['cloud.region']?: string;
['cloud.availability_zone']?: string;
['cloud.platform']?: string;
['host.id']?: string;
['host.type']?: string;
}
11 changes: 10 additions & 1 deletion packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@ export type {
} from './breadcrumb';
export type { Client } from './client';
export type { ClientReport, Outcome, EventDropReason } from './clientreport';
export type { Context, Contexts, DeviceContext, OsContext, AppContext, CultureContext, TraceContext } from './context';
export type {
Context,
Contexts,
DeviceContext,
OsContext,
AppContext,
CultureContext,
TraceContext,
CloudResourceContext,
} from './context';
export type { DataCategory } from './datacategory';
export type { DsnComponents, DsnLike, DsnProtocol } from './dsn';
export type { DebugImage, DebugMeta } from './debugMeta';
Expand Down