-
Notifications
You must be signed in to change notification settings - Fork 18
fix(backend): add instance-scoped Kafka consumer group and client IDs #273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+160
−5
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
61 changes: 61 additions & 0 deletions
61
backend/src/agent-trace/__tests__/agent-trace-ingest.service.spec.ts
This file contains hidden or 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,61 @@ | ||
| import { afterEach, beforeEach, describe, expect, test } from 'bun:test'; | ||
|
|
||
| import { AgentTraceIngestService } from '../agent-trace-ingest.service'; | ||
| import type { AgentTraceRepository } from '../agent-trace.repository'; | ||
|
|
||
| const ORIGINAL_ENV = { ...process.env }; | ||
|
|
||
| function restoreEnv(): void { | ||
| process.env = { ...ORIGINAL_ENV }; | ||
| } | ||
|
|
||
| describe('AgentTraceIngestService', () => { | ||
| beforeEach(() => { | ||
| restoreEnv(); | ||
| process.env.LOG_KAFKA_BROKERS = 'localhost:19092'; | ||
| delete process.env.SHIPSEC_INSTANCE; | ||
| delete process.env.AGENT_TRACE_KAFKA_GROUP_ID; | ||
| delete process.env.AGENT_TRACE_KAFKA_CLIENT_ID; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| restoreEnv(); | ||
| }); | ||
|
|
||
| test('uses legacy defaults when SHIPSEC_INSTANCE is unset', () => { | ||
| const repository = { append: async () => undefined } as unknown as AgentTraceRepository; | ||
| const service = new AgentTraceIngestService(repository) as unknown as { | ||
| kafkaGroupId: string; | ||
| kafkaClientId: string; | ||
| }; | ||
|
|
||
| expect(service.kafkaGroupId).toBe('shipsec-agent-trace-ingestor'); | ||
| expect(service.kafkaClientId).toBe('shipsec-backend-agent-trace'); | ||
| }); | ||
|
|
||
| test('uses instance-scoped defaults when SHIPSEC_INSTANCE is set', () => { | ||
| process.env.SHIPSEC_INSTANCE = '7'; | ||
| const repository = { append: async () => undefined } as unknown as AgentTraceRepository; | ||
| const service = new AgentTraceIngestService(repository) as unknown as { | ||
| kafkaGroupId: string; | ||
| kafkaClientId: string; | ||
| }; | ||
|
|
||
| expect(service.kafkaGroupId).toBe('shipsec-agent-trace-ingestor-7'); | ||
| expect(service.kafkaClientId).toBe('shipsec-backend-agent-trace-7'); | ||
| }); | ||
|
|
||
| test('prefers explicit env vars over defaults', () => { | ||
| process.env.SHIPSEC_INSTANCE = '3'; | ||
| process.env.AGENT_TRACE_KAFKA_GROUP_ID = 'custom-agent-trace-group'; | ||
| process.env.AGENT_TRACE_KAFKA_CLIENT_ID = 'custom-agent-trace-client'; | ||
| const repository = { append: async () => undefined } as unknown as AgentTraceRepository; | ||
| const service = new AgentTraceIngestService(repository) as unknown as { | ||
| kafkaGroupId: string; | ||
| kafkaClientId: string; | ||
| }; | ||
|
|
||
| expect(service.kafkaGroupId).toBe('custom-agent-trace-group'); | ||
| expect(service.kafkaClientId).toBe('custom-agent-trace-client'); | ||
| }); | ||
| }); |
This file contains hidden or 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
70 changes: 70 additions & 0 deletions
70
backend/src/node-io/__tests__/node-io-ingest.service.spec.ts
This file contains hidden or 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,70 @@ | ||
| import { afterEach, beforeEach, describe, expect, test } from 'bun:test'; | ||
|
|
||
| import { NodeIOIngestService } from '../node-io-ingest.service'; | ||
| import type { NodeIORepository } from '../node-io.repository'; | ||
|
|
||
| const ORIGINAL_ENV = { ...process.env }; | ||
|
|
||
| function restoreEnv(): void { | ||
| process.env = { ...ORIGINAL_ENV }; | ||
| } | ||
|
|
||
| describe('NodeIOIngestService', () => { | ||
| beforeEach(() => { | ||
| restoreEnv(); | ||
| process.env.LOG_KAFKA_BROKERS = 'localhost:19092'; | ||
| delete process.env.SHIPSEC_INSTANCE; | ||
| delete process.env.NODE_IO_KAFKA_GROUP_ID; | ||
| delete process.env.NODE_IO_KAFKA_CLIENT_ID; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| restoreEnv(); | ||
| }); | ||
|
|
||
| test('uses legacy defaults when SHIPSEC_INSTANCE is unset', () => { | ||
| const repository = { | ||
| recordStart: async () => undefined, | ||
| recordCompletion: async () => undefined, | ||
| } as unknown as NodeIORepository; | ||
| const service = new NodeIOIngestService(repository) as unknown as { | ||
| kafkaGroupId: string; | ||
| kafkaClientId: string; | ||
| }; | ||
|
|
||
| expect(service.kafkaGroupId).toBe('shipsec-node-io-ingestor'); | ||
| expect(service.kafkaClientId).toBe('shipsec-backend-node-io'); | ||
| }); | ||
|
|
||
| test('uses instance-scoped defaults when SHIPSEC_INSTANCE is set', () => { | ||
| process.env.SHIPSEC_INSTANCE = '4'; | ||
| const repository = { | ||
| recordStart: async () => undefined, | ||
| recordCompletion: async () => undefined, | ||
| } as unknown as NodeIORepository; | ||
| const service = new NodeIOIngestService(repository) as unknown as { | ||
| kafkaGroupId: string; | ||
| kafkaClientId: string; | ||
| }; | ||
|
|
||
| expect(service.kafkaGroupId).toBe('shipsec-node-io-ingestor-4'); | ||
| expect(service.kafkaClientId).toBe('shipsec-backend-node-io-4'); | ||
| }); | ||
|
|
||
| test('prefers explicit env vars over defaults', () => { | ||
| process.env.SHIPSEC_INSTANCE = '9'; | ||
| process.env.NODE_IO_KAFKA_GROUP_ID = 'custom-node-io-group'; | ||
| process.env.NODE_IO_KAFKA_CLIENT_ID = 'custom-node-io-client'; | ||
| const repository = { | ||
| recordStart: async () => undefined, | ||
| recordCompletion: async () => undefined, | ||
| } as unknown as NodeIORepository; | ||
| const service = new NodeIOIngestService(repository) as unknown as { | ||
| kafkaGroupId: string; | ||
| kafkaClientId: string; | ||
| }; | ||
|
|
||
| expect(service.kafkaGroupId).toBe('custom-node-io-group'); | ||
| expect(service.kafkaClientId).toBe('custom-node-io-client'); | ||
| }); | ||
| }); |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the backend PM2 env block, this default always appends
-${instanceNum}even whenSHIPSEC_INSTANCEis not provided, becauseinstanceNumfalls back to'0'earlier in the file. That means PM2-launched backends withoutSHIPSEC_INSTANCEnow get new group IDs (same pattern also exists for agent trace below), so the service-level “legacy fallback when unset” is bypassed and consumers can replay from the beginning (fromBeginning: true), duplicating ingest data for existing single-instance PM2 deployments.Useful? React with 👍 / 👎.