-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Added kafkajs instrumentation and versioned tests skeleton (#2224
- Loading branch information
1 parent
89df06a
commit fc13916
Showing
10 changed files
with
236 additions
and
6 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 @@ | ||
/* | ||
* Copyright 2024 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
module.exports = function initialize(_agent, kafkajs, _moduleName, shim) { | ||
// Put instrumentation code here for kafkajs.Kafka.producer and kafkajs.Kafka.consumer | ||
} |
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 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 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,66 @@ | ||
/* | ||
* Copyright 2024 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
const tap = require('tap') | ||
const helper = require('../../lib/agent_helper') | ||
const params = require('../../lib/params') | ||
const { removeModules } = require('../../lib/cache-buster') | ||
const utils = require('./utils') | ||
|
||
const broker = `${params.kafka_host}:${params.kafka_port}` | ||
|
||
tap.beforeEach(async (t) => { | ||
t.context.agent = helper.instrumentMockedAgent() | ||
|
||
const { Kafka, logLevel } = require('kafkajs') | ||
t.context.Kafka = Kafka | ||
const topic = utils.randomTopic() | ||
t.context.topic = topic | ||
|
||
const kafka = new Kafka({ | ||
clientId: 'kafka-test', | ||
brokers: [broker], | ||
logLevel: logLevel.NOTHING | ||
}) | ||
await utils.createTopic({ topic, kafka }) | ||
|
||
const producer = kafka.producer() | ||
await producer.connect() | ||
t.context.producer = producer | ||
const consumer = kafka.consumer({ groupId: 'kafka' }) | ||
await consumer.connect() | ||
t.context.consumer = consumer | ||
}) | ||
|
||
tap.afterEach(async (t) => { | ||
helper.unloadAgent(t.context.agent) | ||
removeModules(['kafkajs']) | ||
await t.context.consumer.disconnect() | ||
await t.context.producer.disconnect() | ||
}) | ||
|
||
tap.test('stub', async (t) => { | ||
const { consumer, producer, topic } = t.context | ||
const message = 'test message' | ||
|
||
await consumer.subscribe({ topics: [topic], fromBeginning: true }) | ||
const testPromise = new Promise((resolve) => { | ||
consumer.run({ | ||
eachMessage: async ({ message: actualMessage }) => { | ||
t.equal(actualMessage.value.toString(), message) | ||
resolve() | ||
} | ||
}) | ||
}) | ||
await utils.waitForConsumersToJoinGroup({ consumer }) | ||
await producer.send({ | ||
acks: 1, | ||
topic, | ||
messages: [{ key: 'key', value: message }] | ||
}) | ||
await testPromise | ||
}) |
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,28 @@ | ||
/* | ||
* Copyright 2021 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
exports.config = { | ||
app_name: ['My Application'], | ||
license_key: 'license key here', | ||
logging: { | ||
level: 'trace', | ||
filepath: '../../newrelic_agent.log' | ||
}, | ||
utilization: { | ||
detect_aws: false, | ||
detect_pcf: false, | ||
detect_azure: false, | ||
detect_gcp: false, | ||
detect_docker: false | ||
}, | ||
distributed_tracing: { | ||
enabled: true | ||
}, | ||
transaction_tracer: { | ||
enabled: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "kafka-tests", | ||
"targets": [{"name":"kafkajs","minAgentVersion":"11.19.0"}], | ||
"version": "0.0.0", | ||
"private": true, | ||
"tests": [ | ||
{ | ||
"engines": { | ||
"node": ">=16" | ||
}, | ||
"dependencies": { | ||
"kafkajs": ">=2.0.0" | ||
}, | ||
"files": [ | ||
"kafka.tap.js" | ||
] | ||
} | ||
] | ||
} |
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,64 @@ | ||
/* | ||
* Copyright 2024 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
const { makeId } = require('../../../lib/util/hashes') | ||
const utils = module.exports | ||
|
||
/** | ||
* Creates a random topic to be used for testing | ||
* @param {string} [prefix=test-topic] topic prefix | ||
* @returns {string} topic name with random id appended | ||
*/ | ||
utils.randomTopic = (prefix = 'test-topic') => { | ||
return `${prefix}-${makeId()}` | ||
} | ||
|
||
/** | ||
* Creates a topic with the admin class | ||
* @param {object} params to function | ||
* @param {object} params.kafka instance of kafka.Kafka | ||
* @param {string} params.topic topic name | ||
*/ | ||
utils.createTopic = async ({ kafka, topic }) => { | ||
const admin = kafka.admin() | ||
try { | ||
await admin.connect() | ||
await admin.createTopics({ | ||
waitForLeaders: true, | ||
topics: [{ topic, numPartitions: 1, replicationFactor: 1, configEntries: [] }] | ||
}) | ||
} finally { | ||
await admin.disconnect() | ||
} | ||
} | ||
|
||
/** | ||
* Waits for consumer to join the group | ||
* | ||
* @param {object} params to function | ||
* @param {object} params.consumer instance of kafkajs.Kafka.consumer | ||
* @param {number} [params.maxWait=10000] how long to wait for consumer to join group | ||
* @returns {Promise} | ||
* | ||
*/ | ||
utils.waitForConsumersToJoinGroup = ({ consumer, maxWait = 10000 }) => | ||
new Promise((resolve, reject) => { | ||
const timeoutId = setTimeout(() => { | ||
consumer.disconnect().then(() => { | ||
reject() | ||
}) | ||
}, maxWait) | ||
consumer.on(consumer.events.GROUP_JOIN, (event) => { | ||
clearTimeout(timeoutId) | ||
resolve(event) | ||
}) | ||
consumer.on(consumer.events.CRASH, (event) => { | ||
clearTimeout(timeoutId) | ||
consumer.disconnect().then(() => { | ||
reject(event.payload.error) | ||
}) | ||
}) | ||
}) |