Skip to content

Commit

Permalink
refactor: move validators into exporter modules and use those for the…
Browse files Browse the repository at this point in the history
… config types
  • Loading branch information
TimBeyer committed Jul 12, 2023
1 parent f774218 commit acc6b0c
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 77 deletions.
3 changes: 2 additions & 1 deletion core/src/config/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { Schema, z } from "zod"
import { Schema, z, infer as inferZodType, ZodType } from "zod"
import { envVarRegex, identifierRegex, joiIdentifierDescription, userIdentifierRegex } from "./constants"

// Add metadata support to schemas. See https://github.com/colinhacks/zod/issues/273#issuecomment-1434077058
Expand Down Expand Up @@ -76,6 +76,7 @@ type GardenSchema = typeof z & {

// This should be imported instead of z because we augment zod with custom methods
export const s = z as GardenSchema
export type inferType<T extends ZodType<any, any, any>> = inferZodType<T>

s.envVars = () => s.record(s.string().regex(envVarRegex).min(1), z.string())

Expand Down
9 changes: 9 additions & 0 deletions core/src/plugins/otel-collector/config/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { sdk } from "../../../plugin/sdk"

const s = sdk.schema

export const baseValidator = s.object({
name: s.string(),
enabled: s.boolean(),
})

export type OtelCollectorBaseConfig = {
processors: {
batch: null | {
Expand Down
20 changes: 14 additions & 6 deletions core/src/plugins/otel-collector/config/datadog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@
*/

import { hostname } from "os"
import { sdk } from "../../../plugin/sdk"
import { baseValidator } from "./base"
import { inferType } from "../../../config/zod"

const s = sdk.schema

export const dataDogValidator = baseValidator.merge(
s.object({
name: s.literal("datadog"),
site: s.string().min(1).default("datadoghq.com"),
apiKey: s.string().min(1),
})
)

export type DatadogExporterConfigPartial = {
exporters: {
Expand All @@ -28,12 +41,7 @@ export type DatadogExporterConfigPartial = {
}
}

export type OtelCollectorDatadogConfiguration = {
name: "datadog"
enabled: boolean
site: string
apiKey: string
}
export type OtelCollectorDatadogConfiguration = inferType<typeof dataDogValidator>

export function makeDatadogPartialConfig(config: OtelCollectorDatadogConfiguration): DatadogExporterConfigPartial {
return {
Expand Down
22 changes: 15 additions & 7 deletions core/src/plugins/otel-collector/config/honeycomb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@
*/

import { OtlpHttpExporterConfigPartial, makeOtlpHttpPartialConfig } from "./otlphttp"
import { sdk } from "../../../plugin/sdk"
import { baseValidator } from "./base"
import { inferType } from "../../../config/zod"

export type OtelCollectorHoneycombConfiguration = {
name: "honeycomb"
enabled: boolean
endpoint: string
apiKey: string
dataset?: string
}
const s = sdk.schema

export const honeycombValidator = baseValidator.merge(
s.object({
name: s.literal("honeycomb"),
endpoint: s.string().url().default("https://api.honeycomb.io"),
apiKey: s.string().min(1),
dataset: s.string().optional(),
})
)

export type OtelCollectorHoneycombConfiguration = inferType<typeof honeycombValidator>

export function makeHoneycombPartialConfig(config: OtelCollectorHoneycombConfiguration): OtlpHttpExporterConfigPartial {
return makeOtlpHttpPartialConfig({
Expand Down
19 changes: 14 additions & 5 deletions core/src/plugins/otel-collector/config/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,26 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { inferType } from "../../../config/zod"
import { sdk } from "../../../plugin/sdk"
import { baseValidator } from "./base"

export type LoggingExporterVerbosityLevel = "detailed" | "normal" | "basic"
export const LoggingExporterVerbosityLevelEnum = [
"detailed",
"normal",
"basic",
] as const satisfies readonly LoggingExporterVerbosityLevel[]

const s = sdk.schema

export const loggingValidator = baseValidator.merge(
s.object({
name: s.literal("logging"),
verbosity: s.enum(LoggingExporterVerbosityLevelEnum).default("normal"),
})
)

export type LoggingExporterConfigPartial = {
exporters: {
logging?: {
Expand All @@ -28,11 +41,7 @@ export type LoggingExporterConfigPartial = {
}
}

export type OtelCollectorLoggingConfiguration = {
name: "logging"
enabled: boolean
verbosity: LoggingExporterVerbosityLevel
}
export type OtelCollectorLoggingConfiguration = inferType<typeof loggingValidator>

export function makeLoggingPartialConfig(config: OtelCollectorLoggingConfiguration): LoggingExporterConfigPartial {
return {
Expand Down
20 changes: 14 additions & 6 deletions core/src/plugins/otel-collector/config/newrelic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,21 @@
*/

import { OtlpHttpExporterConfigPartial, makeOtlpHttpPartialConfig } from "./otlphttp"
import { sdk } from "../../../plugin/sdk"
import { baseValidator } from "./base"
import { inferType } from "../../../config/zod"

export type OtelCollectorNewRelicConfiguration = {
name: "newrelic"
enabled: boolean
endpoint: string
apiKey: string
}
const s = sdk.schema

export const newRelicValidator = baseValidator.merge(
s.object({
name: s.literal("newrelic"),
endpoint: s.string().url().default("https://otlp.nr-data.net:4318"),
apiKey: s.string().min(1),
})
)

export type OtelCollectorNewRelicConfiguration = inferType<typeof newRelicValidator>

export function makeNewRelicPartialConfig(config: OtelCollectorNewRelicConfiguration): OtlpHttpExporterConfigPartial {
return makeOtlpHttpPartialConfig({
Expand Down
21 changes: 15 additions & 6 deletions core/src/plugins/otel-collector/config/otlphttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { inferType } from "../../../config/zod"
import { sdk } from "../../../plugin/sdk"
import { baseValidator } from "./base"

const s = sdk.schema

export const otlpHttpValidator = baseValidator.merge(
s.object({
name: s.literal("otlphttp"),
endpoint: s.string().url(),
headers: s.record(s.string().min(1), s.union([s.number(), s.string().min(1), s.undefined()])).optional(),
})
)

export type OtlpHttpExporterName = `otlphttp/${string | number}`

export type OtlpHttpExporterConfigPartial = {
Expand All @@ -26,12 +40,7 @@ export type OtlpHttpExporterConfigPartial = {
}
}

export type OtelCollectorOtlpHttpConfiguration = {
name: "otlphttp"
enabled: boolean
endpoint: string
headers?: Record<string, string | number | undefined>
}
export type OtelCollectorOtlpHttpConfiguration = inferType<typeof otlpHttpValidator>

export const makeOtlpHttpPartialConfig = (() => {
// We use the counter to make sure every http based config has a unique key
Expand Down
51 changes: 5 additions & 46 deletions core/src/plugins/otel-collector/otel-collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ import { writeFile } from "fs-extra"
import { streamLogs, waitForLogLine, waitForProcessExit } from "../../util/process"
import getPort from "get-port"
import { wrapActiveSpan } from "../../util/tracing/spans"
import { LoggingExporterVerbosityLevelEnum } from "./config/logging"
import { dataDogValidator } from "./config/datadog"
import { honeycombValidator } from "./config/honeycomb"
import { loggingValidator } from "./config/logging"
import { newRelicValidator } from "./config/newrelic"
import { otlpHttpValidator } from "./config/otlphttp"

const OTEL_CONFIG_NAME = "otel-config.yaml"

Expand Down Expand Up @@ -86,51 +90,6 @@ gardenPlugin.addTool({
],
})

const baseValidator = s.object({
name: s.string(),
enabled: s.boolean(),
})

const loggingValidator = baseValidator.merge(
s.object({
name: s.literal("logging"),
verbosity: s.enum(LoggingExporterVerbosityLevelEnum).default("normal"),
})
)

const otlpHttpValidator = baseValidator.merge(
s.object({
name: s.literal("otlphttp"),
endpoint: s.string().url(),
headers: s.record(s.string().min(1), s.number()).optional(),
})
)

const newRelicValidator = baseValidator.merge(
s.object({
name: s.literal("newrelic"),
endpoint: s.string().url().default("https://otlp.nr-data.net:4318"),
apiKey: s.string().min(1),
})
)

const honeycombValidator = baseValidator.merge(
s.object({
name: s.literal("honeycomb"),
endpoint: s.string().url().default("https://api.honeycomb.io"),
apiKey: s.string().min(1),
dataset: s.string().optional(),
})
)

const dataDogValidator = baseValidator.merge(
s.object({
name: s.literal("datadog"),
site: s.string().min(1).default("datadoghq.com"),
apiKey: s.string().min(1),
})
)

const providerConfigSchema = s.object({
exporters: s.array(
s.union([loggingValidator, otlpHttpValidator, newRelicValidator, dataDogValidator, honeycombValidator])
Expand Down

0 comments on commit acc6b0c

Please sign in to comment.