Skip to content
This repository has been archived by the owner on Jul 10, 2023. It is now read-only.

Change application id format #111

Merged
merged 2 commits into from Oct 8, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/index.ts
Expand Up @@ -85,12 +85,12 @@ module.exports = (options: any) => {
ApplicationSupport.parseApplicationTags();

const logStreamName = Utils.getConfiguration(envVariableKeys.AWS_LAMBDA_LOG_STREAM_NAME);
const region = Utils.getConfiguration(envVariableKeys.AWS_REGION);
const region = Utils.getConfiguration(envVariableKeys.AWS_REGION);
const functionVersion = Utils.getConfiguration(envVariableKeys.AWS_LAMBDA_FUNCTION_VERSION);
const applicationId = logStreamName ? logStreamName.split(']').pop() : Utils.generateId();
const applicationInstanceId = logStreamName ? logStreamName.split(']').pop() : Utils.generateId();

const pluginContext: PluginContext = new PluginContext({
applicationId,
applicationInstanceId,
applicationRegion: region ? region : '',
applicationVersion: functionVersion ? functionVersion : '',
requestCount: 0,
Expand All @@ -100,10 +100,6 @@ module.exports = (options: any) => {
config,
});

config.plugins.forEach((plugin: any) => {
plugin.setPluginContext(pluginContext);
});

const increaseRequestCount = () => pluginContext.requestCount += 1;

return (originalFunc: any) => {
Expand All @@ -113,6 +109,14 @@ module.exports = (options: any) => {
}

const thundraWrappedHandler: any = (originalEvent: any, originalContext: any, originalCallback: any) => {
// Creating applicationId here, since we need the information in context
const applicationId = Utils.getApplicationId(originalContext);
pluginContext.applicationId = applicationId;

config.plugins.forEach((plugin: any) => {
plugin.setPluginContext(pluginContext);
});

const originalThis = this;
const thundraWrapper = new ThundraWrapper(
originalThis,
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/PluginContext.ts
Expand Up @@ -2,6 +2,7 @@ import ThundraConfig from './config/ThundraConfig';

class PluginContext {
applicationId: string;
applicationInstanceId: string;
applicationRegion: string;
applicationVersion: string;
requestCount: number;
Expand All @@ -20,6 +21,7 @@ class PluginContext {
constructor(opt: any) {
opt = opt ? opt : {};
this.applicationId = opt.applicationId;
this.applicationInstanceId = opt.applicationInstanceId;
this.applicationRegion = opt.applicationRegion;
this.applicationVersion = opt.applicationVersion;
this.requestCount = opt.requestCount;
Expand Down
22 changes: 21 additions & 1 deletion src/plugins/utils/Utils.ts
Expand Up @@ -394,8 +394,28 @@ class Utils {
}

static getAWSAccountNo(arn: string) {
return Utils.getARNPart(arn, 4);
}

static getAWSRegion(arn: string) {
return Utils.getARNPart(arn, 3);
}

static getAWSFunctionName(arn: string) {
return Utils.getARNPart(arn, 6);
}

static getApplicationId(arn: string) {
const region = Utils.getAWSRegion(arn);
const accountNo = Utils.getAWSAccountNo(arn);
const functionName = Utils.getAWSFunctionName(arn);

return `aws:lambda:${region}:${accountNo}:${functionName}`;
}

static getARNPart(arn: string, index: number) {
try {
return arn.split(':')[4];
return arn.split(':')[index];
} catch (error) {
return '';
}
Expand Down
2 changes: 2 additions & 0 deletions test/plugins/console.shim.filter.test.js
Expand Up @@ -3,6 +3,8 @@ import { createMockPluginContext, createMockBeforeInvocationData } from '../mock

describe('Console integration should filter logs with levels', () => {
const logPlugin = new LogPlugin();
logPlugin.enable();

const pluginContext = createMockPluginContext();
const beforeInvocationData = createMockBeforeInvocationData();

Expand Down
2 changes: 2 additions & 0 deletions test/plugins/console.shim.test.js
Expand Up @@ -3,6 +3,8 @@ import { createMockPluginContext, createMockBeforeInvocationData } from '../mock

describe('Log plugin shim console', () => {
const logPlugin = new LogPlugin();
logPlugin.enable();

const pluginContext = createMockPluginContext();
const beforeInvocationData = createMockBeforeInvocationData();
logPlugin.setPluginContext(pluginContext);
Expand Down
1 change: 1 addition & 0 deletions test/plugins/invocation.trace.support.test.js
Expand Up @@ -147,6 +147,7 @@ describe('Invocation Trace Support', () => {

test('Should set resourceAvgDuration while merging resources', () => {
const tracer = new ThundraTracer();
InvocationTraceSupport.tracer = tracer;
tracer.recorder.destroy();

const spanOptions = {
Expand Down
6 changes: 6 additions & 0 deletions test/plugins/log.test.js
Expand Up @@ -5,6 +5,8 @@ describe('LogPlugin', () => {
describe('constructor', () => {
const options = { op2: 1, opt2: 2 };
const logPlugin = new LogPlugin(options);
logPlugin.enable();

it('should set variables', () => {
expect(logPlugin.hooks).toEqual({ 'before-invocation': logPlugin.beforeInvocation, 'after-invocation': logPlugin.afterInvocation });
expect(logPlugin.options).toEqual(options);
Expand All @@ -14,6 +16,7 @@ describe('LogPlugin', () => {
describe('report', () => {
describe('when reporter instance is set', () => {
const logPlugin = new LogPlugin();
logPlugin.enable();
logPlugin.reporter = createMockReporter();
logPlugin.report('logReport');
it('should report', () => {
Expand All @@ -28,6 +31,7 @@ describe('LogPlugin', () => {

describe('setPluginContext', () => {
const logPlugin = new LogPlugin();
logPlugin.enable();
const pluginContext = createMockPluginContext();
logPlugin.setPluginContext(pluginContext);
it('should set pluginContext and apiKey', () => {
Expand All @@ -38,6 +42,7 @@ describe('LogPlugin', () => {

describe('beforeInvocation', () => {
const logPlugin = new LogPlugin();
logPlugin.enable();
const beforeInvocationData = createMockBeforeInvocationData();
const pluginContext = createMockPluginContext();
logPlugin.setPluginContext(pluginContext);
Expand All @@ -49,6 +54,7 @@ describe('LogPlugin', () => {

describe('reportLog', () => {
const logPlugin = new LogPlugin();
logPlugin.enable();
const pluginContext = createMockPluginContext();
const beforeInvocationData = createMockBeforeInvocationData();
const logData = {
Expand Down