-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(analytics): Add analytics provider for AWS Firehose (#2823)
* Add Firehose provider for analytics * Add KinesisFirehose provider unit test
- Loading branch information
Showing
5 changed files
with
228 additions
and
18 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
packages/analytics/__tests__/Providers/AWSKinesisFirehoseProvider-unit-test.ts
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,100 @@ | ||
jest.mock('aws-sdk/clients/firehose', () => { | ||
const Firehose = () => { | ||
const firehose = null; | ||
return firehose; | ||
}; | ||
|
||
Firehose.prototype.putRecordBatch = (params, callback) => { | ||
callback(null, 'data'); | ||
}; | ||
|
||
return Firehose; | ||
}); | ||
|
||
import * as Firehose from 'aws-sdk/clients/firehose'; | ||
import { Credentials } from '@aws-amplify/core'; | ||
import KinesisFirehoseProvider from '../../src/Providers/AWSKinesisFirehoseProvider'; | ||
|
||
const credentials = { | ||
accessKeyId: 'accessKeyId', | ||
sessionToken: 'sessionToken', | ||
secretAccessKey: 'secretAccessKey', | ||
identityId: 'identityId', | ||
authenticated: true, | ||
}; | ||
|
||
jest.useFakeTimers(); | ||
|
||
describe('kinesis firehose provider test', () => { | ||
describe('getCategory test', () => { | ||
test('happy case', () => { | ||
const analytics = new KinesisFirehoseProvider(); | ||
|
||
expect(analytics.getCategory()).toBe('Analytics'); | ||
}); | ||
}); | ||
|
||
describe('getProviderName test', () => { | ||
test('happy case', () => { | ||
const analytics = new KinesisFirehoseProvider(); | ||
|
||
expect(analytics.getProviderName()).toBe('AWSKinesisFirehose'); | ||
}); | ||
}); | ||
|
||
describe('configure test', () => { | ||
test('happy case', () => { | ||
const analytics = new KinesisFirehoseProvider(); | ||
|
||
expect(analytics.configure({ region: 'region1' })).toEqual({ | ||
bufferSize: 1000, | ||
flushInterval: 5000, | ||
flushSize: 100, | ||
region: 'region1', | ||
resendLimit: 5, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('record test', () => { | ||
test('record without credentials', async () => { | ||
const analytics = new KinesisFirehoseProvider(); | ||
|
||
const spyon = jest | ||
.spyOn(Credentials, 'get') | ||
.mockImplementationOnce(() => { | ||
return Promise.reject('err'); | ||
}); | ||
|
||
expect(await analytics.record('params')).toBe(false); | ||
spyon.mockRestore(); | ||
}); | ||
|
||
test('record happy case', async () => { | ||
const analytics = new KinesisFirehoseProvider(); | ||
analytics.configure({ region: 'region1' }); | ||
|
||
const spyon = jest.spyOn(Firehose.prototype, 'putRecordBatch'); | ||
|
||
jest.spyOn(Credentials, 'get').mockImplementationOnce(() => { | ||
return Promise.resolve(credentials); | ||
}); | ||
|
||
await analytics.record({ | ||
event: { | ||
data: { | ||
data: 'data', | ||
}, | ||
streamName: 'stream', | ||
}, | ||
config: {}, | ||
}); | ||
|
||
jest.advanceTimersByTime(6000); | ||
|
||
expect(spyon).toBeCalled(); | ||
|
||
spyon.mockRestore(); | ||
}); | ||
}); | ||
}); |
111 changes: 111 additions & 0 deletions
111
packages/analytics/src/Providers/AWSKinesisFirehoseProvider.ts
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,111 @@ | ||
/* | ||
* Copyright 2017-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with | ||
* the License. A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0/ | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | ||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions | ||
* and limitations under the License. | ||
*/ | ||
|
||
import { ConsoleLogger as Logger } from '@aws-amplify/core'; | ||
import { AWSKinesisProvider } from './index'; | ||
import * as Firehose from 'aws-sdk/clients/firehose'; | ||
|
||
const logger = new Logger('AWSKineisFirehoseProvider'); | ||
|
||
export default class AWSKinesisFirehoseProvider extends AWSKinesisProvider { | ||
private _kinesisFirehose; | ||
|
||
constructor(config?) { | ||
super(config); | ||
} | ||
|
||
/** | ||
* get provider name of the plugin | ||
*/ | ||
public getProviderName(): string { | ||
return 'AWSKinesisFirehose'; | ||
} | ||
|
||
protected _sendEvents(group) { | ||
if (group.length === 0) { | ||
return; | ||
} | ||
|
||
const { config, credentials } = group[0]; | ||
|
||
const initClients = this._init(config, credentials); | ||
if (!initClients) return false; | ||
|
||
const records = {}; | ||
|
||
group.map(params => { | ||
// spit by streamName | ||
const evt = params.event; | ||
const { streamName } = evt; | ||
if (records[streamName] === undefined) { | ||
records[streamName] = []; | ||
} | ||
|
||
const PartitionKey = | ||
evt.partitionKey || `partition-${credentials.identityId}`; | ||
|
||
Object.assign(evt.data, { PartitionKey }); | ||
const Data = JSON.stringify(evt.data); | ||
const record = { Data }; | ||
records[streamName].push(record); | ||
}); | ||
|
||
Object.keys(records).map(streamName => { | ||
logger.debug( | ||
'putting records to kinesis', | ||
streamName, | ||
'with records', | ||
records[streamName] | ||
); | ||
this._kinesisFirehose.putRecordBatch( | ||
{ | ||
Records: records[streamName], | ||
DeliveryStreamName: streamName, | ||
}, | ||
err => { | ||
if (err) logger.debug('Failed to upload records to Kinesis', err); | ||
else logger.debug('Upload records to stream', streamName); | ||
} | ||
); | ||
}); | ||
} | ||
|
||
protected _init(config, credentials) { | ||
logger.debug('init clients'); | ||
|
||
if ( | ||
this._kinesisFirehose && | ||
this._config.credentials && | ||
this._config.credentials.sessionToken === credentials.sessionToken && | ||
this._config.credentials.identityId === credentials.identityId | ||
) { | ||
logger.debug('no change for analytics config, directly return from init'); | ||
return true; | ||
} | ||
|
||
this._config.credentials = credentials; | ||
const { region } = config; | ||
|
||
return this._initFirehose(region, credentials); | ||
} | ||
|
||
private _initFirehose(region, credentials) { | ||
logger.debug('initialize kinesis firehose with credentials', credentials); | ||
this._kinesisFirehose = new Firehose({ | ||
apiVersion: '2015-08-04', | ||
region, | ||
credentials, | ||
}); | ||
return 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
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import AWSPinpointProvider from './AWSPinpointProvider'; | ||
import AWSKinesisProvider from './AWSKinesisProvider'; | ||
import AWSKinesisFirehoseProvider from './AWSKinesisFirehoseProvider'; | ||
import AmazonPersonalizeProvider from './AmazonPersonalizeProvider'; | ||
|
||
export { AWSPinpointProvider, AWSKinesisProvider, AmazonPersonalizeProvider }; | ||
export { AWSPinpointProvider, AWSKinesisProvider, AWSKinesisFirehoseProvider, AmazonPersonalizeProvider }; |
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