Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce metrics usage #874

Merged
merged 3 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@ jobs:
if [ -n "$LATEST" ]; then
echo "::set-output name=image_builder_latest::$REGISTRY/$REPOSITORY:$IMAGE_TAG_LATEST"
fi
echo "::set-output name=image_builder_commit::$REGISTRY/$REPOSITORY:$IMAGE_TAG_COMMIT"
echo "::set-output name=image_builder_commit::$REGISTRY/$REPOSITORY:$IMAGE_TAG_COMMIT"
9 changes: 7 additions & 2 deletions blocks/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import type { DecofileProvider } from "../engine/decofile/provider.ts";
import { HttpError } from "../engine/errors.ts";
import type { ResolverMiddlewareContext } from "../engine/middleware.ts";
import { logger } from "../observability/otel/config.ts";
import { meter } from "../observability/otel/metrics.ts";
import {
meter,
OTEL_ENABLE_EXTRA_METRICS,
} from "../observability/otel/metrics.ts";
import { caches, ENABLE_LOADER_CACHE } from "../runtime/caches/mod.ts";
import type { HttpContext } from "./handler.ts";
import {
Expand Down Expand Up @@ -317,7 +320,9 @@ const wrapLoader = (
return await flights.do(request.url, staleWhileRevalidate);
} finally {
const dimension = { loader, status };
stats.latency.record(performance.now() - start, dimension);
if (OTEL_ENABLE_EXTRA_METRICS) {
stats.latency.record(performance.now() - start, dimension);
}
ctx.monitoring?.currentSpan?.setDesc(status);
}
},
Expand Down
2 changes: 1 addition & 1 deletion hooks/useDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const useDevice = (): Device => {
}

if (!ctx) {
console.warn("Missing context in rendering tree")
console.warn("Missing context in rendering tree");
}

return ctx?.device || "desktop";
Expand Down
4 changes: 2 additions & 2 deletions observability/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import { meter } from "./otel/metrics.ts";
const httpDuration = meter.createHistogram("http_request_duration", {
description: "http request duration",
unit: "ms",
valueType: ValueType.DOUBLE,
valueType: ValueType.INT,
});
/**
* @returns a end function that when gets called observe the duration of the operation.
*/
export const startObserve = () => {
const start = performance.now();
return (method: string, path: string, status: number) => {
httpDuration.record(performance.now() - start, {
httpDuration.record(Math.round(performance.now() - start), {
"http.method": method,
"http.route": path,
"http.response.status": `${status}`,
Expand Down
12 changes: 7 additions & 5 deletions observability/observe.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isWrappedError } from "../blocks/loader.ts";
import { ValueType } from "../deps.ts";
import { meter } from "./otel/metrics.ts";
import { meter, OTEL_ENABLE_EXTRA_METRICS } from "./otel/metrics.ts";

const operationDuration = meter.createHistogram("block_op_duration", {
description: "operation duration",
Expand All @@ -27,9 +27,11 @@ export const observe = async <T>(
isError = "true";
throw error;
} finally {
operationDuration.record(performance.now() - start, {
"operation.name": op,
"operation.is_error": isError,
});
if (OTEL_ENABLE_EXTRA_METRICS) {
operationDuration.record(performance.now() - start, {
"operation.name": op,
"operation.is_error": isError,
});
}
}
};
13 changes: 12 additions & 1 deletion observability/otel/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ import {
View,
} from "../../deps.ts";
import { OTEL_IS_ENABLED, resource } from "./config.ts";

export const OTEL_ENABLE_EXTRA_METRICS: boolean = Deno.env.has(
"OTEL_ENABLE_EXTRA_METRICS",
);

// 2 minutes. We don't need frequent updates here.
export const OTEL_EXPORT_INTERVAL: number = parseInt(
Deno.env.get("OTEL_EXPORT_INTERVAL") ?? "60000",
10,
);

// a=b,c=d => {a:b, c:d}
const headersStringToObject = (headersString: string | undefined | null) => {
if (!headersString) {
Expand Down Expand Up @@ -46,7 +57,7 @@ if (OTEL_IS_ENABLED) {
meterProvider.addMetricReader(
new PeriodicExportingMetricReader({
exporter: metricExporter,
exportIntervalMillis: 30_000,
exportIntervalMillis: OTEL_EXPORT_INTERVAL,
}),
);
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/caches/mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const baseTest = async (cacheStorageUT: CacheStorage) => {
const cache = await headersCache(lruCache(cacheStorageUT)).open(CACHE_NAME);
const response = () =>
new Response("Hello, World!", {
headers: { "Content-length": `${MAX_CACHE_SIZE / 2 }` },
headers: { "Content-length": `${MAX_CACHE_SIZE / 2}` },
});
for (let i = 0; i < 5; i++) {
const request = createRequest(i);
Expand Down
1 change: 0 additions & 1 deletion runtime/routes/invoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ function getParsingStrategy(req: Request): keyof typeof propsParsers | null {
return "json";
}


return null;
}

Expand Down
Loading