Skip to content

Commit 3d0db74

Browse files
authored
feat(telemetry): adding telemetry endpoints (#445)
1 parent bc46738 commit 3d0db74

File tree

4 files changed

+176
-0
lines changed

4 files changed

+176
-0
lines changed

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ export type {
7474
PatchFile,
7575
PatchRecord,
7676
PatchViewResponse,
77+
TelemetryConfig,
78+
PostOrgTelemetryPayload,
79+
PostOrgTelemetryResponse,
7780
QueryParams,
7881
RequestInfo,
7982
RequestOptions,

src/socket-sdk-class.ts

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ import type {
7373
GetOptions,
7474
GotOptions,
7575
PatchViewResponse,
76+
PostOrgTelemetryPayload,
77+
PostOrgTelemetryResponse,
7678
QueryParams,
7779
RequestOptions,
7880
SendOptions,
@@ -3481,6 +3483,113 @@ export class SocketSdk {
34813483
})
34823484
})
34833485
}
3486+
3487+
/**
3488+
* Update organization's telemetry configuration.
3489+
* Enables or disables telemetry for the organization.
3490+
*
3491+
* @param orgSlug - Organization identifier
3492+
* @param telemetryData - Telemetry configuration with enabled flag
3493+
* @returns Updated telemetry configuration
3494+
*
3495+
* @throws {Error} When server returns 5xx status codes
3496+
*/
3497+
async updateOrgTelemetryConfig(
3498+
orgSlug: string,
3499+
telemetryData: { enabled?: boolean | undefined },
3500+
): Promise<SocketSdkResult<'updateOrgTelemetryConfig'>> {
3501+
try {
3502+
const data = await this.#executeWithRetry(
3503+
async () =>
3504+
await getResponseJson(
3505+
await createRequestWithJson(
3506+
'PUT',
3507+
this.#baseUrl,
3508+
`orgs/${encodeURIComponent(orgSlug)}/telemetry/config`,
3509+
telemetryData,
3510+
this.#reqOptions,
3511+
),
3512+
),
3513+
)
3514+
return this.#handleApiSuccess<'updateOrgTelemetryConfig'>(data)
3515+
/* c8 ignore start - Standard API error handling, tested via public method error cases */
3516+
} catch (e) {
3517+
return await this.#handleApiError<'updateOrgTelemetryConfig'>(e)
3518+
}
3519+
/* c8 ignore stop */
3520+
}
3521+
3522+
/**
3523+
* Get organization's telemetry configuration.
3524+
* Returns whether telemetry is enabled for the organization.
3525+
*
3526+
* @param orgSlug - Organization identifier
3527+
* @returns Telemetry configuration with enabled status
3528+
*
3529+
* @throws {Error} When server returns 5xx status codes
3530+
*/
3531+
async getTelemetryConfig(
3532+
orgSlug: string,
3533+
): Promise<SocketSdkResult<'getOrgTelemetryConfig'>> {
3534+
try {
3535+
const data = await this.#executeWithRetry(
3536+
async () =>
3537+
await getResponseJson(
3538+
await createGetRequest(
3539+
this.#baseUrl,
3540+
`orgs/${encodeURIComponent(orgSlug)}/telemetry/config`,
3541+
this.#reqOptions,
3542+
),
3543+
),
3544+
)
3545+
return this.#handleApiSuccess<'getOrgTelemetryConfig'>(data)
3546+
/* c8 ignore start - Standard API error handling, tested via public method error cases */
3547+
} catch (e) {
3548+
return await this.#handleApiError<'getOrgTelemetryConfig'>(e)
3549+
}
3550+
/* c8 ignore stop */
3551+
}
3552+
3553+
/**
3554+
* Post telemetry data for an organization.
3555+
* Sends telemetry events and analytics data for monitoring and analysis.
3556+
*
3557+
* @param orgSlug - Organization identifier
3558+
* @param telemetryData - Telemetry payload containing events and metrics
3559+
* @returns Empty object on successful submission
3560+
*
3561+
* @throws {Error} When server returns 5xx status codes
3562+
*/
3563+
async postOrgTelemetry(
3564+
orgSlug: string,
3565+
telemetryData: PostOrgTelemetryPayload,
3566+
): Promise<SocketSdkGenericResult<PostOrgTelemetryResponse>> {
3567+
try {
3568+
const data = (await this.#executeWithRetry(
3569+
async () =>
3570+
await getResponseJson(
3571+
await createRequestWithJson(
3572+
'POST',
3573+
this.#baseUrl,
3574+
`orgs/${encodeURIComponent(orgSlug)}/telemetry`,
3575+
telemetryData,
3576+
this.#reqOptions,
3577+
),
3578+
),
3579+
)) as PostOrgTelemetryResponse
3580+
return {
3581+
cause: undefined,
3582+
data,
3583+
error: undefined,
3584+
status: 200,
3585+
success: true,
3586+
}
3587+
/* c8 ignore start - Standard API error handling, tested via public method error cases */
3588+
} catch (e) {
3589+
return this.#createQueryErrorResult<PostOrgTelemetryResponse>(e)
3590+
}
3591+
/* c8 ignore stop */
3592+
}
34843593
}
34853594

34863595
// Optional live heap trace.

src/types.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,20 @@ export type StreamOrgFullScanOptions = {
423423
output?: boolean | string | undefined
424424
}
425425

426+
export type PostOrgTelemetryPayload = Record<string, unknown>
427+
428+
export type PostOrgTelemetryResponse = Record<string, never>
429+
430+
/**
431+
* Configuration for telemetry collection.
432+
* Controls whether telemetry is enabled and how events are collected.
433+
*/
434+
export interface TelemetryConfig {
435+
telemetry: {
436+
enabled: boolean
437+
}
438+
}
439+
426440
export type UploadManifestFilesOptions = {
427441
pathsRelativeTo?: string | undefined
428442
}

test/unit/socket-sdk-api-methods.coverage.test.mts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,20 @@ describe('SocketSdk - API Methods Coverage', () => {
152152
} else if (url.includes('/quota')) {
153153
// Quota endpoint
154154
res.end(JSON.stringify({ data: { limit: 1000, used: 100 } }))
155+
} else if (url.includes('/telemetry/config')) {
156+
// Telemetry config endpoint
157+
if (req.method === 'PUT') {
158+
res.end(JSON.stringify({ telemetry: { enabled: true } }))
159+
} else {
160+
res.end(JSON.stringify({ telemetry: { enabled: false } }))
161+
}
162+
} else if (url.includes('/telemetry')) {
163+
// Telemetry POST endpoint (must come before generic /settings)
164+
if (req.method === 'POST') {
165+
res.end(JSON.stringify({}))
166+
} else {
167+
res.end(JSON.stringify({}))
168+
}
155169
} else if (url.includes('/settings')) {
156170
// Settings endpoint
157171
res.end(JSON.stringify({ data: { success: true } }))
@@ -492,6 +506,42 @@ describe('SocketSdk - API Methods Coverage', () => {
492506
})
493507
})
494508

509+
describe('Telemetry Methods', () => {
510+
it('covers getTelemetryConfig', async () => {
511+
const result = await client.getTelemetryConfig('test-org')
512+
expect(result.success).toBe(true)
513+
if (result.success) {
514+
expect(result.data).toBeDefined()
515+
expect(result.data.telemetry).toBeDefined()
516+
expect(result.data.telemetry.enabled).toBe(false)
517+
}
518+
})
519+
520+
it('covers updateOrgTelemetryConfig', async () => {
521+
const result = await client.updateOrgTelemetryConfig('test-org', {
522+
enabled: true,
523+
})
524+
expect(result.success).toBe(true)
525+
if (result.success) {
526+
expect(result.data).toBeDefined()
527+
expect(result.data.telemetry).toBeDefined()
528+
expect(result.data.telemetry.enabled).toBe(true)
529+
}
530+
})
531+
532+
it('covers postOrgTelemetry', async () => {
533+
const result = await client.postOrgTelemetry('test-org', {
534+
event: 'test-event',
535+
timestamp: Date.now(),
536+
})
537+
expect(result.success).toBe(true)
538+
if (result.success) {
539+
expect(result.data).toBeDefined()
540+
expect(result.data).toEqual({})
541+
}
542+
})
543+
})
544+
495545
describe('API Token Methods', () => {
496546
it('covers getAPITokens', async () => {
497547
const result = await client.getAPITokens('test-org')

0 commit comments

Comments
 (0)