|
| 1 | +/** |
| 2 | + * Generic metrics event utilities |
| 3 | + * @description Wrapper for RA binding |
| 4 | + */ |
| 5 | +export class MetricsTracker { |
| 6 | + constructor( |
| 7 | + private wae: AnalyticsEngineDataset, |
| 8 | + //private env: MetricsBindings['ENVIRONMENT'] |
| 9 | + ) {} |
| 10 | + |
| 11 | + logEvent(event: MetricsEvent): void { |
| 12 | + try { |
| 13 | + this.wae.writeDataPoint(event.toDataPoint()) |
| 14 | + } catch (e) { |
| 15 | + console.error(`Failed to log metrics event, ${e}`) |
| 16 | + // rethrow errors in vitest, but failsafe in other environments |
| 17 | + /*if (this.env === 'VITEST') { |
| 18 | + throw e |
| 19 | + }*/ |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * MetricsEvent |
| 26 | + * |
| 27 | + * Each event type is stored with a different indexId and has an associated class which |
| 28 | + * maps a more ergonomic event object to a ReadyAnalyticsEvent |
| 29 | + */ |
| 30 | +export interface MetricsEvent { |
| 31 | + toDataPoint(): AnalyticsEngineDataPoint |
| 32 | +} |
| 33 | + |
| 34 | +export enum MetricsEventIndexIds { |
| 35 | + AUTH_USER = 'auth_user', |
| 36 | + SESSION_START = 'session_start', |
| 37 | + TOOL_CALL = 'tool_call', |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Utility functions to map named blob/double objects to an array |
| 42 | + * We do this so we don't have to annotate `blob1`, `blob2`, etc in comments. |
| 43 | + * |
| 44 | + * I prefer this to just writing it in an array because it'll be easier to reference |
| 45 | + * later when we are writing ready analytics queries. |
| 46 | + * |
| 47 | + * IMO named tuples and raw arrays aren't as ergonomic to work with, but they require less of this code below |
| 48 | + */ |
| 49 | +type Range1To20 = |
| 50 | + | 1 |
| 51 | + | 2 |
| 52 | + | 3 |
| 53 | + | 4 |
| 54 | + | 5 |
| 55 | + | 6 |
| 56 | + | 7 |
| 57 | + | 8 |
| 58 | + | 9 |
| 59 | + | 10 |
| 60 | + | 11 |
| 61 | + | 12 |
| 62 | + | 13 |
| 63 | + | 14 |
| 64 | + | 15 |
| 65 | + | 16 |
| 66 | + | 17 |
| 67 | + | 18 |
| 68 | + | 19 |
| 69 | + | 20 |
| 70 | + |
| 71 | +type Blobs = { |
| 72 | + [key in `blob${Range1To20}`]?: string | null |
| 73 | +} |
| 74 | + |
| 75 | +type Doubles = { |
| 76 | + [key in `double${Range1To20}`]?: number |
| 77 | +} |
| 78 | + |
| 79 | +export class MetricsError extends Error { |
| 80 | + constructor(message: string) { |
| 81 | + super(message) |
| 82 | + this.name = 'MetricsError' |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +export function mapBlobs(blobs: Blobs): Array<string | null> { |
| 87 | + const blobsArray = new Array(Object.keys(blobs).length) |
| 88 | + for (const [key, value] of Object.entries(blobs)) { |
| 89 | + const match = key.match(/^blob(\d+)$/) |
| 90 | + if (match === null || match.length < 2) { |
| 91 | + // we should never hit this because of the typedefinitions above, |
| 92 | + // but this error is for safety |
| 93 | + throw new MetricsError('Failed to map blobs, invalid key') |
| 94 | + } |
| 95 | + const index = parseInt(match[1], 10) |
| 96 | + if (isNaN(index)) { |
| 97 | + // we should never hit this because of the typedefinitions above, |
| 98 | + // but this error is for safety |
| 99 | + throw new MetricsError('Failed to map blobs, invalid index') |
| 100 | + } |
| 101 | + if (index - 1 >= blobsArray.length) { |
| 102 | + throw new MetricsError('Failed to map blobs, missing blob') |
| 103 | + } |
| 104 | + blobsArray[index - 1] = value |
| 105 | + } |
| 106 | + return blobsArray |
| 107 | +} |
| 108 | + |
| 109 | +export function mapDoubles(doubles: Doubles): number[] { |
| 110 | + const doublesArray = new Array(Object.keys(doubles).length) |
| 111 | + for (const [key, value] of Object.entries(doubles)) { |
| 112 | + const match = key.match(/^double(\d+)$/) |
| 113 | + if (match === null || match.length < 2) { |
| 114 | + // we should never hit this because of the typedefinitions above, |
| 115 | + // but this error is for safety |
| 116 | + throw new MetricsError(': Failed to map doubles, invalid key') |
| 117 | + } |
| 118 | + const index = parseInt(match[1], 10) |
| 119 | + if (isNaN(index)) { |
| 120 | + // we should never hit this because of the typedefinitions above, |
| 121 | + // but this error is for safety |
| 122 | + throw new MetricsError('Failed to map doubles, invalid index') |
| 123 | + } |
| 124 | + if (index - 1 >= doublesArray.length) { |
| 125 | + throw new MetricsError('Failed to map doubles, missing blob') |
| 126 | + } |
| 127 | + doublesArray[index - 1] = value |
| 128 | + } |
| 129 | + return doublesArray |
| 130 | +} |
0 commit comments