-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathconfig.ts
157 lines (141 loc) · 4.69 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import * as log from "std/log/mod.ts";
import { Context, context } from "../../deco.ts";
import {
BatchSpanProcessor,
FetchInstrumentation,
NodeTracerProvider,
opentelemetry,
OTLPTraceExporter,
ParentBasedSampler,
registerInstrumentations,
Resource,
SemanticResourceAttributes,
} from "../../deps.ts";
import meta from "../../meta.json" assert { type: "json" };
import { DenoRuntimeInstrumentation } from "./instrumentation/deno-runtime.ts";
import { DebugSampler } from "./samplers/debug.ts";
import { SamplingOptions, URLBasedSampler } from "./samplers/urlBased.ts";
import { OpenTelemetryHandler } from "https://denopkg.com/hyperdxio/hyperdx-js@cc43f5a2ba5f0062f3e01ea3d162d71971dd1f89/packages/deno/mod.ts";
const tryGetVersionOf = (pkg: string) => {
try {
const [_, ver] = import.meta.resolve(pkg).split("@");
return ver.substring(0, ver.length - 1);
} catch {
return undefined;
}
};
const apps_ver = tryGetVersionOf("apps/") ??
tryGetVersionOf("deco-sites/std/") ?? "_";
export const resource = Resource.default().merge(
new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: Deno.env.get("DECO_SITE_NAME") ??
"deco",
[SemanticResourceAttributes.SERVICE_VERSION]:
Context.active().deploymentId ??
Deno.hostname(),
[SemanticResourceAttributes.SERVICE_INSTANCE_ID]: crypto.randomUUID(),
[SemanticResourceAttributes.CLOUD_PROVIDER]: context.platform,
"deco.runtime.version": meta.version,
"deco.apps.version": apps_ver,
[SemanticResourceAttributes.CLOUD_REGION]: Deno.env.get("DENO_REGION") ??
"unknown",
}),
);
const loggerName = "deco-logger";
log.setup({
handlers: {
otel: new OpenTelemetryHandler("INFO", {
resourceAttributes: resource.attributes,
}),
},
loggers: {
[loggerName]: {
level: "INFO",
handlers: ["otel"],
},
},
});
export const logger = log.getLogger(loggerName);
export const OTEL_IS_ENABLED = Deno.env.has("OTEL_EXPORTER_OTLP_ENDPOINT");
const trackCfHeaders = [
"Cf-Ray",
"Cf-Cache-Status",
"X-Origin-Cf-Cache-Status",
"X-Vtex-Io-Cluster-Id",
"X-Edge-Cache-Status",
];
registerInstrumentations({
instrumentations: [
new FetchInstrumentation(
{
applyCustomAttributesOnSpan: (span, _req, response) => {
if (span && response instanceof Response) {
trackCfHeaders.forEach((header) => {
const val = response.headers.get(header);
if (val) {
span.setAttribute(
`http.response.header.${header.toLocaleLowerCase()}`,
val,
);
}
});
}
},
},
),
new DenoRuntimeInstrumentation(),
],
});
// Monkeypatching to get past FetchInstrumentation's dependence on sdk-trace-web, which has runtime dependencies on some browser-only constructs. See https://github.com/open-telemetry/opentelemetry-js/issues/3413#issuecomment-1496834689 for more details
// Specifically for this line - https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-sdk-trace-web/src/utils.ts#L310
// @ts-ignore: monkey patching location
globalThis.location = {};
const parseSamplingOptions = (): SamplingOptions | undefined => {
const encodedOpts = Deno.env.get("OTEL_SAMPLING_CONFIG");
if (!encodedOpts) {
return undefined;
}
try {
return JSON.parse(atob(encodedOpts));
} catch (err) {
console.error("could not parse sampling config", err);
return undefined;
}
};
const debugSampler = new DebugSampler(
new URLBasedSampler(parseSamplingOptions()),
);
const provider = new NodeTracerProvider({
resource: resource,
sampler: new ParentBasedSampler(
{
root: debugSampler,
},
),
});
if (context.isDeploy && context.platform === "kubernetes") {
setInterval(() => {
const resourceUsage: Record<string, string> = {};
// deno-lint-ignore no-deprecated-deno-api
for (const [rid, resc] of Object.entries(Deno.resources())) {
resourceUsage[`rid.${rid}`] = resc.toString();
}
for (const [mem, usage] of Object.entries(Deno.memoryUsage())) {
resourceUsage[`mem.${mem}`] = `${(+usage / (2 ** 20)).toFixed(2)} MB`;
}
for (const [mem, usage] of Object.entries(Deno.systemMemoryInfo())) {
resourceUsage[`sys.${mem}`] = `${(+usage / (2 ** 20)).toFixed(2)} MB`;
}
console.table(resourceUsage);
}, 30_000);
}
if (OTEL_IS_ENABLED) {
const traceExporter = new OTLPTraceExporter();
provider.addSpanProcessor(new BatchSpanProcessor(traceExporter));
provider.register();
}
export const tracer = opentelemetry.trace.getTracer(
"deco-tracer",
);
export const tracerIsRecording = () =>
opentelemetry.trace.getActiveSpan()?.isRecording() ?? false;