aws-lambda#KinesisStreamRecord TypeScript Examples

The following examples show how to use aws-lambda#KinesisStreamRecord. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: kinesis-stream-wrapper.service.ts    From nr-apm-stack with Apache License 2.0 6 votes vote down vote up
/**
   * Wraps data into a KinesisStreamRecord
   * @param data The data to wrap
   * @returns The mocked KinesisStreamRecord
   */
  private wrapDataIntoRecord(data: OsDocumentData): KinesisStreamRecord {
    return {
      awsRegion: 'ca-central-1',
      eventID: 'string',
      eventName: 'string',
      eventSource: 'string',
      eventSourceARN: 'string',
      eventVersion: 'string',
      invokeIdentityArn: 'string',
      kinesis: {
        approximateArrivalTimestamp: 0,
        data: Buffer.from(JSON.stringify(data), 'utf8').toString('base64'),
        kinesisSchemaVersion: 'string',
        partitionKey: 'string',
        sequenceNumber: 'string',
      },
    };
  }
Example #2
Source File: kinesis-stream-record-mapper.service.ts    From nr-apm-stack with Apache License 2.0 6 votes vote down vote up
/**
   * Converts a KinesisStreamRecord to an OsDocument
   * @param record The record to convert
   * @returns
   */
  public toOpensearchDocument(record: KinesisStreamRecord): OsDocument {
    const data = JSON.parse(Buffer.from(record.kinesis.data, 'base64').toString('utf8'));
    return {
      fingerprint: FINGERPRINT_UNKNOWN,
      id: null,
      index: null,
      type: '_doc',
      data,
      record,
      error: null,
    };
  }
Example #3
Source File: ecs-transform.service.spec.ts    From nr-apm-stack with Apache License 2.0 4 votes vote down vote up
describe('EcsTransformService', () => {
  it('transforms data', () => {
    const mockParsers = [
      {
        matches: jest.fn().mockReturnValue(true),
        apply: jest.fn().mockReturnValue({}),
      },
    ] as unknown as Parser[];
    const ksrMapper = {
      toOpensearchDocument: jest.fn().mockReturnValue({}),
      toFingerprintedDocument: jest.fn(),
    } as unknown as KinesisStreamRecordMapperService;
    const logger = {
      log: jest.fn(),
      debug: jest.fn(),
    } as LoggerService;

    const service = new EcsTransformService(
      mockParsers, mockParsers, mockParsers,
      mockParsers, mockParsers, mockParsers,
      ksrMapper, logger);

    service.transform({Records: [{} as KinesisStreamRecord]});

    expect(ksrMapper.toOpensearchDocument).toBeCalledTimes(1);
    expect(ksrMapper.toFingerprintedDocument).toBeCalledTimes(1);
    expect(mockParsers[0].matches).toBeCalledTimes(6);
    expect(mockParsers[0].apply).toBeCalledTimes(6);
    expect(logger.log).toBeCalledWith('Received 1 records');
  });

  it('rejects and continues processing data', () => {
    const mockParsers = [
      {
        matches: jest.fn().mockReturnValue(true),
        apply: jest.fn()
          .mockImplementationOnce(() => {
            throw new ParserError('hi', 'bob');
          })
          .mockReturnValue({}),
      },
    ] as unknown as Parser[];
    const ksrMapper = {
      toOpensearchDocument: jest.fn().mockReturnValue({
        fingerprint: {
          name: 'fingerprint',
        },
        data: {
          host: {
            hostname: 'host',
          },
          service: {
            name: 'service',
          },
          organization: {
            id: 'org',
          },
          event: {
            sequence: 20,
          },
          log: {
            file: {
              path: '/path',
            },
          },
        },
      } as unknown as OsDocument),
      toFingerprintedDocument: jest.fn(),
    } as unknown as KinesisStreamRecordMapperService;
    const logger = {
      log: jest.fn(),
      debug: jest.fn(),
    } as LoggerService;

    const service = new EcsTransformService(
      mockParsers, mockParsers, mockParsers,
      mockParsers, mockParsers, mockParsers,
      ksrMapper, logger);

    const rVal = service.transform({Records: [{awsRegion: 'fail'} as KinesisStreamRecord, {} as KinesisStreamRecord]});

    expect(ksrMapper.toOpensearchDocument).toBeCalledTimes(2);
    expect(ksrMapper.toFingerprintedDocument).toBeCalledTimes(1);
    expect(mockParsers[0].matches).toBeCalledTimes(7);
    expect(mockParsers[0].apply).toBeCalledTimes(7);
    expect(logger.log).toBeCalledWith('Received 2 records');
    expect(logger.log).toBeCalledWith('PARSE_ERROR:bob org host service /path:20 fingerprint : hi');
    expect(logger.log).toBeCalledWith('Rejected 1 records');
    expect(rVal.length).toBe(1);
    expect(rVal[0].data.host.hostname).toBe('host');
  });
});