This repository has been archived by the owner on Jul 10, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
separate lambda related application info logic from index
- Loading branch information
1 parent
8bff7b4
commit 0b9dff6
Showing
11 changed files
with
190 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import ConfigProvider from '../config/ConfigProvider'; | ||
import ConfigNames from '../config/ConfigNames'; | ||
import ThundraLogger from '../ThundraLogger'; | ||
|
||
export interface ApplicationInfo { | ||
applicationId: string; | ||
applicationInstanceId: string; | ||
applicationRegion: string; | ||
applicationVersion: string; | ||
applicationTags: any; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import {ApplicationInfo} from './ApplicationInfo'; | ||
|
||
export interface ApplicationInfoProvider { | ||
getApplicationInfo: () => ApplicationInfo; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import {ApplicationInfoProvider} from './ApplicationInfoProvider'; | ||
import {LambdaApplicationInfoProvider} from './LambdaApplicationInfoProvider'; | ||
import {ApplicationInfo} from './ApplicationInfo'; | ||
|
||
export class ApplicationManager { | ||
|
||
static applicationInfoProvider: ApplicationInfoProvider; | ||
|
||
static setApplicationInfoProvider(applicationInfoProvider: ApplicationInfoProvider) { | ||
ApplicationManager.applicationInfoProvider = applicationInfoProvider; | ||
} | ||
|
||
static getApplicationInfoProvider(): ApplicationInfoProvider { | ||
return ApplicationManager.applicationInfoProvider; | ||
} | ||
|
||
static getApplicationInfo(): ApplicationInfo { | ||
return ApplicationManager.applicationInfoProvider.getApplicationInfo(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import Utils from '../plugins/utils/Utils'; | ||
import {EnvVariableKeys} from '../Constants'; | ||
import {ApplicationInfoProvider} from './ApplicationInfoProvider'; | ||
import {ApplicationInfo} from './ApplicationInfo'; | ||
import {LambdaContextProvider} from './LambdaContextProvider'; | ||
import {LambdaUtils} from '../plugins/utils/LambdaUtils'; | ||
|
||
export class LambdaApplicationInfoProvider implements ApplicationInfoProvider { | ||
|
||
private applicationInfo: ApplicationInfo; | ||
|
||
constructor() { | ||
const logStreamName = Utils.getEnvVar(EnvVariableKeys.AWS_LAMBDA_LOG_STREAM_NAME); | ||
const region = Utils.getEnvVar(EnvVariableKeys.AWS_REGION); | ||
const functionVersion = Utils.getEnvVar(EnvVariableKeys.AWS_LAMBDA_FUNCTION_VERSION); | ||
this.applicationInfo = { | ||
applicationId: undefined, | ||
applicationInstanceId: logStreamName ? logStreamName.split(']').pop() : Utils.generateId(), | ||
applicationRegion: region ? region : '', | ||
applicationVersion: functionVersion ? functionVersion : '', | ||
applicationTags: Utils.getApplicationTags(), | ||
}; | ||
} | ||
|
||
getApplicationInfo(): ApplicationInfo { | ||
const lambdaContext = LambdaContextProvider.getContext(); | ||
if (!this.applicationInfo.applicationId && lambdaContext) { | ||
this.applicationInfo.applicationId = LambdaUtils.getApplicationId(lambdaContext); | ||
} | ||
return this.applicationInfo; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export class LambdaContextProvider { | ||
static context: any; | ||
|
||
static setContext(context: any) { | ||
LambdaContextProvider.context = context; | ||
} | ||
|
||
static getContext() { | ||
return LambdaContextProvider.context; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import {EnvVariableKeys} from '../../Constants'; | ||
import ConfigProvider from '../../config/ConfigProvider'; | ||
import ConfigNames from '../../config/ConfigNames'; | ||
import Utils from './Utils'; | ||
|
||
export class LambdaUtils { | ||
|
||
static getApplicationId(originalContext: any) { | ||
const arn = originalContext.invokedFunctionArn; | ||
const region = Utils.getEnvVar(EnvVariableKeys.AWS_REGION) | ||
|| 'local'; | ||
const accountNo = LambdaUtils.getAccountNo(arn, ConfigProvider.get<string>(ConfigNames.THUNDRA_APIKEY)); | ||
const functionName = LambdaUtils.getApplicationName(originalContext); | ||
|
||
return `aws:lambda:${region}:${accountNo}:${functionName}`; | ||
} | ||
|
||
static getApplicationName(originalContext: any) { | ||
return ConfigProvider.get<string>(ConfigNames.THUNDRA_APPLICATION_NAME, | ||
originalContext.functionName | ||
|| Utils.getEnvVar(EnvVariableKeys.AWS_LAMBDA_FUNCTION_NAME) | ||
|| 'lambda-app'); | ||
} | ||
|
||
static getAccountNo(arn: string, apiKey: string) { | ||
if (LambdaUtils.getIfSAMLocalDebugging()) { | ||
return 'sam_local'; | ||
} else if (LambdaUtils.getIfSLSLocalDebugging()) { | ||
return 'sls_local'; | ||
} else { | ||
return (LambdaUtils.getAWSAccountNo(arn) | ||
|| apiKey | ||
|| 'guest'); | ||
} | ||
} | ||
|
||
static getAWSAccountNo(arn: string) { | ||
return LambdaUtils.getARNPart(arn, 4); | ||
} | ||
|
||
static getAWSRegion(arn: string) { | ||
return LambdaUtils.getARNPart(arn, 3); | ||
} | ||
|
||
static getARNPart(arn: string, index: number) { | ||
try { | ||
return arn.split(':')[index]; | ||
} catch (error) { | ||
return ''; | ||
} | ||
} | ||
|
||
static getIfSAMLocalDebugging() { | ||
return Utils.getEnvVar(EnvVariableKeys.AWS_SAM_LOCAL) === 'true'; | ||
} | ||
|
||
static getIfSLSLocalDebugging() { | ||
return Utils.getEnvVar(EnvVariableKeys.SLS_LOCAL) === 'true'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters