LogDrains.utils.ts110 lines · main
1/**
2 * Utility functions for log drain management
3 * Extracted for testability
4 */
5
6import { getKeyValueFieldArrayValidationIssues } from 'ui-patterns/form/KeyValueFieldArray/validation'
7import { z } from 'zod'
8
9import { LogDrainType } from './LogDrains.constants'
10import { httpEndpointUrlSchema } from '@/lib/validation/http-url'
11
12export type LogDrainHeaderRow = {
13 key: string
14 value: string
15}
16
17/**
18 * Get the description text for the custom headers section based on log drain type
19 */
20export function getHeadersSectionDescription(type: LogDrainType): string {
21 if (type === 'webhook') {
22 return 'Set custom headers when draining logs to the Endpoint URL'
23 }
24 if (type === 'loki') {
25 return 'Set custom headers when draining logs to the Loki HTTP(S) endpoint'
26 }
27 if (type === 'otlp') {
28 return 'Set custom headers for OTLP authentication (e.g., Authorization, X-API-Key)'
29 }
30 return ''
31}
32
33/**
34 * Validation errors for header management
35 */
36export const HEADER_VALIDATION_ERRORS = {
37 MAX_LIMIT: 'You can only have 20 custom headers',
38 DUPLICATE: 'Header name already exists',
39 KEY_REQUIRED: 'Header name is required',
40 VALUE_REQUIRED: 'Header value is required',
41} as const
42
43const DEFAULT_HEADERS_BY_TYPE: Partial<Record<LogDrainType, Record<string, string>>> = {
44 webhook: { 'Content-Type': 'application/json' },
45 otlp: { 'Content-Type': 'application/x-protobuf' },
46}
47
48export function getDefaultHeadersByType(type: LogDrainType): Record<string, string> {
49 return DEFAULT_HEADERS_BY_TYPE[type] ?? {}
50}
51
52export function headerRecordToRows(headers: Record<string, string> = {}): LogDrainHeaderRow[] {
53 return Object.entries(headers).map(([key, value]) => ({ key, value }))
54}
55
56export function headerRowsToRecord(rows: LogDrainHeaderRow[] = []): Record<string, string> {
57 return rows.reduce<Record<string, string>>((acc, row) => {
58 const key = row.key.trim()
59 const value = row.value.trim()
60
61 if (key && value) {
62 acc[key] = value
63 }
64
65 return acc
66 }, {})
67}
68
69export const logDrainHeaderEntriesSchema = z
70 .array(
71 z.object({
72 key: z.string().trim(),
73 value: z.string().trim(),
74 })
75 )
76 .max(20, HEADER_VALIDATION_ERRORS.MAX_LIMIT)
77 .superRefine((rows, ctx) => {
78 getKeyValueFieldArrayValidationIssues({
79 rows,
80 keyFieldName: 'key',
81 valueFieldName: 'value',
82 keyRequiredMessage: HEADER_VALIDATION_ERRORS.KEY_REQUIRED,
83 valueRequiredMessage: HEADER_VALIDATION_ERRORS.VALUE_REQUIRED,
84 duplicateKeyMessage: HEADER_VALIDATION_ERRORS.DUPLICATE,
85 }).forEach((issue) => {
86 ctx.addIssue({
87 code: z.ZodIssueCode.custom,
88 message: issue.message,
89 path: issue.path,
90 })
91 })
92 })
93
94/**
95 * Zod schema for OTLP log drain configuration
96 * Extracted for testing purposes
97 */
98export const otlpConfigSchema = z.object({
99 type: z.literal('otlp'),
100 endpoint: httpEndpointUrlSchema({
101 requiredMessage: 'OTLP endpoint is required',
102 invalidMessage: 'OTLP endpoint must be a valid URL',
103 prefixMessage: 'OTLP endpoint must start with http:// or https://',
104 }),
105 protocol: z.string().optional().default('http/protobuf'),
106 gzip: z.boolean().optional().default(true),
107 headers: z.record(z.string(), z.string()).optional(),
108})
109
110export type OtlpConfig = z.infer<typeof otlpConfigSchema>