-
-
Notifications
You must be signed in to change notification settings - Fork 736
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
feat: unique connection counting #9074
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import type { IUniqueConnectionStore } from '../../types'; | ||
import type { | ||
TimedUniqueConnections, | ||
UniqueConnections, | ||
} from './unique-connection-store-type'; | ||
|
||
export class FakeUniqueConnectionStore implements IUniqueConnectionStore { | ||
private uniqueConnectionsRecord: Record<string, TimedUniqueConnections> = | ||
{}; | ||
|
||
async insert(uniqueConnections: UniqueConnections): Promise<void> { | ||
this.uniqueConnectionsRecord[uniqueConnections.id] = { | ||
...uniqueConnections, | ||
updatedAt: new Date(), | ||
}; | ||
} | ||
|
||
async get( | ||
id: 'current' | 'previous', | ||
): Promise<(UniqueConnections & { updatedAt: Date }) | null> { | ||
return this.uniqueConnectionsRecord[id] || null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import type { IUnleashConfig } from '../../types/option'; | ||
import type { IFlagResolver, IUnleashStores } from '../../types'; | ||
import type { Logger } from '../../logger'; | ||
import type { IUniqueConnectionStore } from './unique-connection-store-type'; | ||
import HyperLogLog from 'hyperloglog-lite'; | ||
import type EventEmitter from 'events'; | ||
import { SDK_CONNECTION_ID_RECEIVED } from '../../metric-events'; | ||
|
||
export class UniqueConnectionService { | ||
private logger: Logger; | ||
|
||
private uniqueConnectionStore: IUniqueConnectionStore; | ||
|
||
private flagResolver: IFlagResolver; | ||
|
||
private eventBus: EventEmitter; | ||
|
||
private activeHour: number; | ||
|
||
private hll = HyperLogLog(12); | ||
|
||
constructor( | ||
{ | ||
uniqueConnectionStore, | ||
}: Pick<IUnleashStores, 'uniqueConnectionStore'>, | ||
config: IUnleashConfig, | ||
) { | ||
this.uniqueConnectionStore = uniqueConnectionStore; | ||
this.logger = config.getLogger('services/unique-connection-service.ts'); | ||
this.flagResolver = config.flagResolver; | ||
this.eventBus = config.eventBus; | ||
this.activeHour = new Date().getHours(); | ||
} | ||
|
||
listen() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this service is interested in new connection ids received |
||
this.eventBus.on(SDK_CONNECTION_ID_RECEIVED, this.count.bind(this)); | ||
} | ||
|
||
async count(connectionId: string) { | ||
if (!this.flagResolver.isEnabled('uniqueSdkTracking')) return; | ||
this.hll.add(HyperLogLog.hash(connectionId)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this data structure tracks unique connections |
||
} | ||
|
||
async sync(): Promise<void> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this logic will get detailed coverage. We track current and previous HyperLogLog in 2 DB rows. If we found the hour has changed we start a new current bucket and copy current to previous. We have to accommodate for multiple pods where any pod can be the first one to start a current bucket. |
||
if (!this.flagResolver.isEnabled('uniqueSdkTracking')) return; | ||
const currentHour = new Date().getHours(); | ||
const currentBucket = await this.uniqueConnectionStore.get('current'); | ||
if (this.activeHour !== currentHour && currentBucket) { | ||
if (currentBucket.updatedAt.getHours() < currentHour) { | ||
this.hll.merge({ n: 12, buckets: currentBucket.hll }); | ||
await this.uniqueConnectionStore.insert({ | ||
hll: this.hll.output().buckets, | ||
id: 'previous', | ||
}); | ||
} else { | ||
const previousBucket = | ||
await this.uniqueConnectionStore.get('previous'); | ||
this.hll.merge({ n: 12, buckets: previousBucket }); | ||
await this.uniqueConnectionStore.insert({ | ||
hll: this.hll.output().buckets, | ||
id: 'previous', | ||
}); | ||
} | ||
this.activeHour = currentHour; | ||
|
||
this.hll = HyperLogLog(12); | ||
} else { | ||
if (currentBucket) { | ||
this.hll.merge({ n: 12, buckets: currentBucket }); | ||
} | ||
await this.uniqueConnectionStore.insert({ | ||
hll: this.hll.output().buckets, | ||
id: 'current', | ||
}); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export type UniqueConnections = { | ||
hll: Buffer; | ||
id: 'current' | 'previous'; | ||
}; | ||
|
||
export type TimedUniqueConnections = UniqueConnections & { | ||
updatedAt: Date; | ||
}; | ||
|
||
// id, hll, updated_at | ||
export interface IUniqueConnectionStore { | ||
insert(uniqueConnections: UniqueConnections): Promise<void>; | ||
get(id: 'current' | 'previous'): Promise<TimedUniqueConnections | null>; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import type { Db } from '../../db/db'; | ||
import type { IUniqueConnectionStore } from '../../types'; | ||
import type { UniqueConnections } from './unique-connection-store-type'; | ||
|
||
export class UniqueConnectionStore implements IUniqueConnectionStore { | ||
private db: Db; | ||
|
||
constructor(db: Db) { | ||
this.db = db; | ||
} | ||
|
||
async insert(uniqueConnections: UniqueConnections): Promise<void> { | ||
await this.db<UniqueConnections>('unique_connections') | ||
.insert({ id: uniqueConnections.id, hll: uniqueConnections.hll }) | ||
.onConflict('id') | ||
.merge(); | ||
} | ||
|
||
async get( | ||
id: 'current' | 'previous', | ||
): Promise<(UniqueConnections & { updatedAt: Date }) | null> { | ||
const row = await this.db('unique_connections') | ||
.select('id', 'hll', 'updated_at') | ||
.where('id', id) | ||
.first(); | ||
return row | ||
? { id: row.id, hll: row.hll, updatedAt: row.updated_at } | ||
: null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import * as responseTime from 'response-time'; | ||
import type EventEmitter from 'events'; | ||
import { REQUEST_TIME } from '../metric-events'; | ||
import { REQUEST_TIME, SDK_CONNECTION_ID_RECEIVED } from '../metric-events'; | ||
import type { IFlagResolver } from '../types/experimental'; | ||
import type { InstanceStatsService } from '../services'; | ||
import type { RequestHandler } from 'express'; | ||
|
@@ -66,6 +66,11 @@ export function responseTimeMetrics( | |
req.query.appName; | ||
} | ||
|
||
const connectionId = req.headers['x-unleash-connection-id']; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is where it starts. on each detected connection id received we let the interested parties know |
||
if (connectionId && flagResolver.isEnabled('uniqueSdkTracking')) { | ||
eventBus.emit(SDK_CONNECTION_ID_RECEIVED, connectionId); | ||
} | ||
|
||
const timingInfo = { | ||
path: pathname, | ||
method: req.method, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
exports.up = function(db, cb) { | ||
db.runSql(` | ||
CREATE TABLE IF NOT EXISTS unique_connections | ||
( | ||
id VARCHAR(255) NOT NULL, | ||
updated_at TIMESTAMP DEFAULT now(), | ||
hll BYTEA NOT NULL, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hyperlog is a buffer that we translate to BYTEA type in postgres |
||
PRIMARY KEY (id) | ||
); | ||
`, cb) | ||
}; | ||
|
||
exports.down = function(db, cb) { | ||
db.runSql(`DROP TABLE unique_connections;`, cb); | ||
}; |
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.
sync in-memory HyperLogLogs with DB