-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
AWSKinesisFirehoseProvider.ts
105 lines (87 loc) · 2.65 KB
/
AWSKinesisFirehoseProvider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { AnalyticsAction, ConsoleLogger as Logger } from '@aws-amplify/core';
import { AWSKinesisProvider } from './AWSKinesisProvider';
import {
PutRecordBatchCommand,
FirehoseClient,
} from '@aws-sdk/client-firehose';
import { fromUtf8 } from '@aws-sdk/util-utf8-browser';
import { getAnalyticsUserAgent } from '../utils/UserAgent';
const logger = new Logger('AWSKineisFirehoseProvider');
export class AWSKinesisFirehoseProvider extends AWSKinesisProvider {
private _kinesisFirehose: FirehoseClient;
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 => {
// split by streamName
const evt = params.event;
const { streamName, data } = evt;
if (records[streamName] === undefined) {
records[streamName] = [];
}
const bufferData =
data && typeof data !== 'string' ? JSON.stringify(data) : data;
const Data = fromUtf8(bufferData);
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
.send(
new PutRecordBatchCommand({
Records: records[streamName],
DeliveryStreamName: streamName,
})
)
.then(res => logger.debug('Upload records to stream', streamName))
.catch(err => logger.debug('Failed to upload records to Kinesis', err));
});
}
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 FirehoseClient({
apiVersion: '2015-08-04',
region,
credentials,
customUserAgent: getAnalyticsUserAgent(AnalyticsAction.Record),
});
return true;
}
}