@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 158 lines 5.13 KB
Raw
1 /**
2 * OpenTelemetry resource service and layers.
3 *
4 * An OpenTelemetry resource identifies the process or service that emits spans,
5 * metrics, and logs. This module stores that resource in Effect context and
6 * provides layers for creating it from explicit service metadata, from
7 * `OTEL_SERVICE_NAME` and `OTEL_RESOURCE_ATTRIBUTES`, or as an empty resource.
8 * It also includes `configToAttributes` for turning service metadata into raw
9 * OpenTelemetry attributes.
10 *
11 * @stability unstable
12 * @since 4.0.0
13 */
14 import type * as OtelApi from "@opentelemetry/api"
15 import * as Resources from "@opentelemetry/resources"
16 import * as OtelSemConv from "@opentelemetry/semantic-conventions"
17 import * as Config from "effect/Config"
18 import * as Context from "effect/Context"
19 import * as Effect from "effect/Effect"
20 import * as Layer from "effect/Layer"
21 import * as Schema from "effect/Schema"
22 import * as Version from "effect/Version"
23
24 /**
25 * Service tag for OpenTelemetry metadata attached to emitted telemetry.
26 *
27 * **When to use**
28 *
29 * Use to provide process, service, and deployment metadata that should be
30 * attached to spans, metrics, and logs.
31 *
32 * @stability unstable
33 * @category services
34 * @since 4.0.0
35 */
36 export class Resource extends Context.Service<
37 Resource,
38 Resources.Resource
39 >()("@effect/opentelemetry/Resource") {}
40
41 /**
42 * Creates a `Resource` layer from service metadata and additional OpenTelemetry attributes.
43 *
44 * @stability unstable
45 * @category layers
46 * @since 4.0.0
47 */
48 export const layer = (config: {
49 readonly serviceName: string
50 readonly serviceVersion?: string
51 readonly attributes?: OtelApi.Attributes
52 }) =>
53 Layer.succeed(
54 Resource,
55 Resources.resourceFromAttributes(configToAttributes(config))
56 )
57
58 const sdkAttributes = (): Record<string, string> => ({
59 [OtelSemConv.ATTR_TELEMETRY_SDK_NAME]: "@effect/opentelemetry",
60 [OtelSemConv.ATTR_TELEMETRY_SDK_LANGUAGE]: typeof (globalThis as any).document === "undefined"
61 ? OtelSemConv.TELEMETRY_SDK_LANGUAGE_VALUE_NODEJS
62 : OtelSemConv.TELEMETRY_SDK_LANGUAGE_VALUE_WEBJS,
63 [OtelSemConv.ATTR_TELEMETRY_SDK_VERSION]: Version.getCurrentVersion()
64 })
65
66 /**
67 * Converts service metadata into OpenTelemetry attributes with SDK defaults.
68 *
69 * **When to use**
70 *
71 * Use to turn explicit service metadata into a raw OpenTelemetry attribute map
72 * for lower-level resource construction or merging with environment-derived
73 * attributes via `layerFromEnv`.
74 *
75 * **Details**
76 *
77 * Custom attributes override `telemetry.sdk.*` defaults. Service options override
78 * matching attributes. An empty `serviceVersion` is treated as absent.
79 *
80 * @see {@link layer} for creating a `Resource` layer from explicit metadata
81 * @see {@link layerFromEnv} for merging attributes with OpenTelemetry environment variables
82 *
83 * @stability unstable
84 * @category converting
85 * @since 4.0.0
86 */
87 export const configToAttributes = (options: {
88 readonly serviceName: string
89 readonly serviceVersion?: string
90 readonly attributes?: OtelApi.Attributes
91 }): Record<string, string> => {
92 const attributes: Record<string, string> = {
93 ...sdkAttributes(),
94 ...(options.attributes ?? undefined),
95 [OtelSemConv.ATTR_SERVICE_NAME]: options.serviceName
96 }
97 if (options.serviceVersion) {
98 attributes[OtelSemConv.ATTR_SERVICE_VERSION] = options.serviceVersion
99 }
100 return attributes
101 }
102
103 /**
104 * Creates a `Resource` layer from SDK defaults, then environment variables,
105 * then additional attributes (later values take precedence).
106 *
107 * **Details**
108 *
109 * `OTEL_RESOURCE_ATTRIBUTES` is read with `Config.Record`, so a structured
110 * config value is accepted as well as a `key=value` list. In the list, keys and
111 * values are trimmed, then percent-decoded once. Entries with an empty value
112 * are kept as `""`; entries without `=` or with an empty key are skipped. When
113 * a key is repeated as written, the last entry wins and the earlier ones are
114 * not decoded. If any remaining key or value is not valid percent-encoding,
115 * the whole variable is discarded and a warning is logged.
116 *
117 * @stability unstable
118 * @category layers
119 * @since 4.0.0
120 */
121 export const layerFromEnv = (
122 additionalAttributes?:
123 | OtelApi.Attributes
124 | undefined
125 ): Layer.Layer<Resource> =>
126 Layer.effect(
127 Resource,
128 Effect.gen(function*() {
129 const serviceName = yield* Config.option(Config.String("OTEL_SERVICE_NAME"))
130 const attributes = yield* Config.Record(
131 Schema.StringFromUriComponent,
132 Schema.StringFromUriComponent,
133 "OTEL_RESOURCE_ATTRIBUTES",
134 { trim: true, keepEmptyValues: true }
135 ).pipe(
136 Config.withDefault({}),
137 Effect.catch((error) => Effect.as(Effect.logWarning("Discarding OTEL_RESOURCE_ATTRIBUTES", error), {}))
138 )
139 return Resources.resourceFromAttributes({
140 ...sdkAttributes(),
141 ...attributes,
142 ...(serviceName._tag === "Some" ? { [OtelSemConv.ATTR_SERVICE_NAME]: serviceName.value } : undefined),
143 ...additionalAttributes
144 })
145 }).pipe(Effect.orDie)
146 )
147
148 /**
149 * Layer that provides an empty OpenTelemetry resource.
150 *
151 * @stability unstable
152 * @category layers
153 * @since 4.0.0
154 */
155 export const layerEmpty = Layer.succeed(
156 Resource,
157 Resources.emptyResource()
158 )