import {z} from 'zod' export type Configv1LogFieldPath = {selector?: string | undefined}; export type LogControlRuleDropField = {field_regex: string, parent_path?: Configv1LogFieldPath | undefined}; export type EmitMetricsCounter = {value?: Configv1LogFieldPath | undefined}; export type GaugeAggregationType = 'LAST' | 'MIN' | 'MAX'; export type EmitMetricsGauge = {aggregation_type?: GaugeAggregationType | undefined, value: Configv1LogFieldPath}; export type EmitMetricsHistogram = {value: Configv1LogFieldPath}; export type LogControlRuleEmitMetricsLabel = {key?: string | undefined, value?: Configv1LogFieldPath | undefined}; export type EmitMetricsMetricMode = 'COUNTER' | 'GAUGE' | 'HISTOGRAM'; export type LogControlRuleEmitMetrics = {counter?: EmitMetricsCounter | undefined, drop_log?: boolean | undefined, gauge?: EmitMetricsGauge | undefined, histogram?: EmitMetricsHistogram | undefined, labels?: Array | undefined, mode: EmitMetricsMetricMode, name: string}; export type Configv1LogControlRuleMode = 'ENABLED' | 'DISABLED'; export type LogParserGrokParser = {pattern?: string | undefined}; export type LogParserKeyValueParser = {delimiter: string, pair_separator: string, trim_set?: string | undefined}; export type LogParserParserType = 'JSON' | 'REGEX' | 'KEY_VALUE' | 'GROK'; export type LogParserRegexParser = {regex?: string | undefined}; export type LogIngestConfigLogParser = {grok_parser?: LogParserGrokParser | undefined, key_value_parser?: LogParserKeyValueParser | undefined, parser_type?: LogParserParserType | undefined, regex_parser?: LogParserRegexParser | undefined}; export type LogControlRuleParseField = {destination?: Configv1LogFieldPath | undefined, parser?: LogIngestConfigLogParser | undefined, source?: Configv1LogFieldPath | undefined}; export type MappedValueReplacePair = {key?: string | undefined, value?: string | undefined}; export type ReplaceFieldMappedValue = {default_value?: string | undefined, pairs?: Array | undefined, use_default?: boolean | undefined}; export type ReplaceFieldReplaceMode = 'HASH' | 'STATIC_VALUE' | 'MAPPED_VALUE'; export type ReplaceFieldStaticValue = {value?: string | undefined}; export type LogControlRuleReplaceField = {field: Configv1LogFieldPath, mapped_value?: ReplaceFieldMappedValue | undefined, replace_all?: boolean | undefined, replace_mode: ReplaceFieldReplaceMode, replace_regex: string, static_value?: ReplaceFieldStaticValue | undefined}; export type LogControlRuleSample = {rate: number}; export type Configv1LogControlRuleType = 'DROP' | 'SAMPLE' | 'DROP_FIELD' | 'REPLACE_FIELD' | 'EMIT_METRICS' | 'PARSE_FIELD'; export type Configv1LogControlRule = {drop_field?: LogControlRuleDropField | undefined, emit_metrics?: LogControlRuleEmitMetrics | undefined, filter?: string | undefined, mode?: Configv1LogControlRuleMode | undefined, name?: string | undefined, parse_field?: LogControlRuleParseField | undefined, replace_field?: LogControlRuleReplaceField | undefined, sample?: LogControlRuleSample | undefined, type?: Configv1LogControlRuleType | undefined}; export type Configv1LogControlConfig = {created_at?: string | undefined, rules?: Array | undefined, updated_at?: string | undefined}; export type Configv1CreateLogControlConfigRequest = {dry_run?: boolean | undefined, log_control_config?: Configv1LogControlConfig | undefined}; export type CreateApiApiV1ConfigLogControlConfigArgs = {body: Configv1CreateLogControlConfigRequest}; export type CreateApiApiV1ConfigLogControlConfigOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const configv1LogFieldPath = z.object({selector: z.string().optional()}); export const logControlRuleDropField = z.object({field_regex: z.string(), parent_path: configv1LogFieldPath.optional()}); export const emitMetricsCounter = z.object({value: configv1LogFieldPath.optional()}); export const gaugeAggregationType = z.enum(["LAST", "MIN", "MAX"]); export const emitMetricsGauge = z.object({aggregation_type: gaugeAggregationType.optional(), value: configv1LogFieldPath}); export const emitMetricsHistogram = z.object({value: configv1LogFieldPath}); export const logControlRuleEmitMetricsLabel = z.object({key: z.string().optional(), value: configv1LogFieldPath.optional()}); export const emitMetricsMetricMode = z.enum(["COUNTER", "GAUGE", "HISTOGRAM"]); export const logControlRuleEmitMetrics = z.object({counter: emitMetricsCounter.optional(), drop_log: z.boolean().optional(), gauge: emitMetricsGauge.optional(), histogram: emitMetricsHistogram.optional(), labels: z.array(logControlRuleEmitMetricsLabel).optional(), mode: emitMetricsMetricMode, name: z.string()}); export const configv1LogControlRuleMode = z.enum(["ENABLED", "DISABLED"]); export const logParserGrokParser = z.object({pattern: z.string().optional()}); export const logParserKeyValueParser = z.object({delimiter: z.string(), pair_separator: z.string(), trim_set: z.string().optional()}); export const logParserParserType = z.enum(["JSON", "REGEX", "KEY_VALUE", "GROK"]); export const logParserRegexParser = z.object({regex: z.string().optional()}); export const logIngestConfigLogParser = z.object({grok_parser: logParserGrokParser.optional(), key_value_parser: logParserKeyValueParser.optional(), parser_type: logParserParserType.optional(), regex_parser: logParserRegexParser.optional()}); export const logControlRuleParseField = z.object({destination: configv1LogFieldPath.optional(), parser: logIngestConfigLogParser.optional(), source: configv1LogFieldPath.optional()}); export const mappedValueReplacePair = z.object({key: z.string().optional(), value: z.string().optional()}); export const replaceFieldMappedValue = z.object({default_value: z.string().optional(), pairs: z.array(mappedValueReplacePair).optional(), use_default: z.boolean().optional()}); export const replaceFieldReplaceMode = z.enum(["HASH", "STATIC_VALUE", "MAPPED_VALUE"]); export const replaceFieldStaticValue = z.object({value: z.string().optional()}); export const logControlRuleReplaceField = z.object({field: configv1LogFieldPath, mapped_value: replaceFieldMappedValue.optional(), replace_all: z.boolean().optional(), replace_mode: replaceFieldReplaceMode, replace_regex: z.string(), static_value: replaceFieldStaticValue.optional()}); export const logControlRuleSample = z.object({rate: z.number()}); export const configv1LogControlRuleType = z.enum(["DROP", "SAMPLE", "DROP_FIELD", "REPLACE_FIELD", "EMIT_METRICS", "PARSE_FIELD"]); export const configv1LogControlRule = z.object({drop_field: logControlRuleDropField.optional(), emit_metrics: logControlRuleEmitMetrics.optional(), filter: z.string().optional(), mode: configv1LogControlRuleMode.optional(), name: z.string().optional(), parse_field: logControlRuleParseField.optional(), replace_field: logControlRuleReplaceField.optional(), sample: logControlRuleSample.optional(), type: configv1LogControlRuleType.optional()}); export const configv1LogControlConfig = z.object({created_at: z.string().optional(), rules: z.array(configv1LogControlRule).optional(), updated_at: z.string().optional()}); export const configv1CreateLogControlConfigResponse = z.object({log_control_config: configv1LogControlConfig.optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * POST /api/v1/config/log-control-config */ export const createApiApiV1ConfigLogControlConfig = async (args: CreateApiApiV1ConfigLogControlConfigArgs, options?: CreateApiApiV1ConfigLogControlConfigOptions) => { const {baseUrl = "https://tenant.chronosphere.io", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("API-Token", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/config/log-control-config${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(configv1CreateLogControlConfigResponse, await res.json()) };