Skip to content

Commit

Permalink
ci: Test on Deno v2 (#8)
Browse files Browse the repository at this point in the history
* ci: Test on Deno v2

* Update typescript signatures for Deno 2

* Remove dead Deno APIs: run, metrics, resources

https://docs.deno.com/runtime/reference/migration_guide/

* Deno.run() is replaced by new Deno.Command()
* Deno.metrics() is gone
* Deno.resources() is gone

* Continue being ok on deno v1
  • Loading branch information
danopia authored Dec 18, 2024
1 parent c6ba11a commit 8c2a6aa
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 137 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/deno-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
- v1.37
- v1.39
- v1.43
- v2.0
- canary
fail-fast: false # run each branch to completion

Expand All @@ -25,14 +26,14 @@ jobs:
uses: actions/checkout@v4

- name: Use Deno ${{ matrix.deno-version }}
uses: denoland/setup-deno@v1
uses: denoland/setup-deno@v2
with:
deno-version: ${{ matrix.deno-version }}

# "https" cache: code from the Internet
# External sources won't change much so we use less precise keys
- name: Cache https://
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ~/.cache/deno/deps/https
key: deno-https/v1-${{ github.sha }}
Expand Down
3 changes: 2 additions & 1 deletion demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ async function handler(req: Request): Promise<Response> {
stderr: 'inherit',
}).output();
return new Response('No failure happened??');
} catch (err) {
} catch (thrown: unknown) {
const err = thrown as Error;
return new Response(`Failed as expected.\n${err.message}`);
}
}
Expand Down
2 changes: 0 additions & 2 deletions instrumentation/auto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { InstrumentationBase } from "../opentelemetry/instrumentation.js";

import { DenoCommandInstrumentation } from "./deno-command.ts";
import { DenoKvInstrumentation } from "./deno-kv.ts";
import { DenoRunInstrumentation } from "./deno-run.ts";
import { DenoRuntimeInstrumentation } from "./deno-runtime.ts";
import { FetchInstrumentation } from "./fetch.ts";

Expand All @@ -14,7 +13,6 @@ export function getDenoAutoInstrumentations() {
// Rough check to exclude Deno Deploy, which doesn't have subprocesses etc.
if (Deno.version?.deno) {
instrs.push(new DenoCommandInstrumentation());
instrs.push(new DenoRunInstrumentation());
instrs.push(new DenoRuntimeInstrumentation());
}

Expand Down
4 changes: 2 additions & 2 deletions instrumentation/deno-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class DenoCommandInstrumentation extends InstrumentationBase {
throw err;
}
}
outputSync(): Deno.CommandOutput {
override outputSync(): Deno.CommandOutput {
const span = this._span ??= plugin.tracer.startSpan(this._spanName, this._attributes);
try {
const output = super.outputSync();
Expand All @@ -105,7 +105,7 @@ export class DenoCommandInstrumentation extends InstrumentationBase {
throw err;
}
}
spawn(): Deno.ChildProcess {
override spawn(): Deno.ChildProcess {
const span = this._span ??= plugin.tracer.startSpan(this._spanName, this._attributes);
try {
const process = super.spawn();
Expand Down
21 changes: 14 additions & 7 deletions instrumentation/deno-kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ export class DenoKvInstrumentation extends InstrumentationBase {
});
span.end();
return result;
} catch (err) {
} catch (thrown: unknown) {
const err = thrown as Error;
span.recordException(err);
span.end();
throw err;
Expand Down Expand Up @@ -107,7 +108,8 @@ export class DenoKvInstrumentation extends InstrumentationBase {
});
span.end();
return result;
} catch (err) {
} catch (thrown: unknown) {
const err = thrown as Error;
span.recordException(err);
span.end();
throw err;
Expand Down Expand Up @@ -145,7 +147,8 @@ export class DenoKvInstrumentation extends InstrumentationBase {
const result = original.call(this, selector, opts);
listSpans.set(result, {span, docCount: 0});
return result;
} catch (err) {
} catch (thrown: unknown) {
const err = thrown as Error;
span.recordException(err);
span.end();
throw err;
Expand Down Expand Up @@ -179,7 +182,8 @@ export class DenoKvInstrumentation extends InstrumentationBase {
});
span.end();
return result;
} catch (err) {
} catch (thrown: unknown) {
const err = thrown as Error;
span.recordException(err);
span.end();
throw err;
Expand Down Expand Up @@ -209,7 +213,8 @@ export class DenoKvInstrumentation extends InstrumentationBase {
const result = await original.call(this, key);
span.end();
return result;
} catch (err) {
} catch (thrown: unknown) {
const err = thrown as Error;
span.recordException(err);
span.end();
throw err;
Expand Down Expand Up @@ -238,7 +243,8 @@ export class DenoKvInstrumentation extends InstrumentationBase {
const result = await original.call(this);
span.end();
return result;
} catch (err) {
} catch (thrown: unknown) {
const err = thrown as Error;
span.recordException(err);
span.end();
throw err;
Expand All @@ -260,7 +266,8 @@ export class DenoKvInstrumentation extends InstrumentationBase {
const result = await original.call(this);
ref.span.end();
return result;
} catch (err) {
} catch (thrown: unknown) {
const err = thrown as Error;
ref.span.recordException(err);
ref.span.end();
throw err;
Expand Down
72 changes: 0 additions & 72 deletions instrumentation/deno-run.ts

This file was deleted.

48 changes: 0 additions & 48 deletions instrumentation/deno-runtime.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import {
type Attributes,
type BatchObservableResult,
type ObservableCounter,
type ObservableGauge,
type ObservableResult,
type ObservableUpDownCounter,
ValueType,
} from "../opentelemetry/api.js";
import {
Expand All @@ -21,23 +18,11 @@ export class DenoRuntimeInstrumentation extends InstrumentationBase {
}

metrics!: {
openResources: ObservableUpDownCounter<Attributes>;
memoryUsage: ObservableGauge<Attributes>;
dispatchedCtr: ObservableCounter<Attributes>;
inflightCtr: ObservableUpDownCounter<Attributes>;
};

protected init() {}

private gatherOpenResources = (x: ObservableResult<Attributes>) => {
for (const entry of Object
.values(Deno.resources())
.reduce<Map<string,number>>((acc,x) => (acc.set(x, 1 + (acc.get(x) ?? 0)), acc), new Map())
) {
x.observe(entry[1], { 'deno.resource.type': entry[0] });
}
}

private gatherMemoryUsage = (x: ObservableResult<Attributes>) => {
const usage = Deno.memoryUsage();
x.observe(usage.rss, {"deno.memory.type": "rss"});
Expand All @@ -46,51 +31,18 @@ export class DenoRuntimeInstrumentation extends InstrumentationBase {
x.observe(usage.external, {"deno.memory.type": "external"});
}

private gatherOps = (x: BatchObservableResult<Attributes>) => {
for (const [op, data] of Object.entries(Deno.metrics().ops)) {
if (data.opsDispatched == 0) continue;
x.observe(this.metrics.dispatchedCtr, data.opsDispatched, { "deno.op": op });
x.observe(this.metrics.inflightCtr, data.opsDispatched - data.opsCompleted, { "deno.op": op });
}
}

enable() {
this.metrics ??= {
openResources: this.meter
.createObservableUpDownCounter("deno.open_resources", {
valueType: ValueType.INT,
description: "Number of open resources of a particular type.",
}),
memoryUsage: this.meter
.createObservableGauge("deno.memory_usage", {
valueType: ValueType.INT,
}),
dispatchedCtr: this.meter
.createObservableCounter("deno.ops_dispatched", {
valueType: ValueType.INT,
description: "Total number of Deno op invocations.",
}),
inflightCtr: this.meter
.createObservableUpDownCounter("deno.ops_inflight", {
valueType: ValueType.INT,
description: "Number of currently-inflight Deno ops.",
}),
};

this.metrics.openResources.addCallback(this.gatherOpenResources);
this.metrics.memoryUsage.addCallback(this.gatherMemoryUsage);
this.meter.addBatchObservableCallback(this.gatherOps, [
this.metrics.dispatchedCtr,
this.metrics.inflightCtr,
]);
}

disable() {
this.metrics.openResources.removeCallback(this.gatherOpenResources);
this.metrics.memoryUsage.removeCallback(this.gatherMemoryUsage);
this.meter.removeBatchObservableCallback(this.gatherOps, [
this.metrics.dispatchedCtr,
this.metrics.inflightCtr,
]);
}
}
3 changes: 2 additions & 1 deletion instrumentation/http-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ export function httpTracer(inner: Deno.ServeHandler, opts?: {

return new Response(respSnoop.newBody, resp);

} catch (err) {
} catch (thrown: unknown) {
const err = thrown as Error;
serverSpan.recordException(err);
serverSpan.setStatus({
code: SpanStatusCode.ERROR,
Expand Down
1 change: 0 additions & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ export { logs } from './opentelemetry/api-logs.js';
export { Resource } from "./opentelemetry/resources.js";

export { httpTracer } from './instrumentation/http-server.ts';
export { DenoRunInstrumentation } from './instrumentation/deno-run.ts';
export { FetchInstrumentation } from './instrumentation/fetch.ts';
export { DenoRuntimeInstrumentation } from './instrumentation/deno-runtime.ts';
export { getDenoAutoInstrumentations } from './instrumentation/auto.ts';
Expand Down
2 changes: 1 addition & 1 deletion otel-platform/detectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const processResource = new Resource({
});
export class DenoProcessDetector implements DetectorSync {
detect() {
// deno deploy currently lacks querySync, but can read
//@ts-ignore deno deploy currently lacks querySync, but can take the action
const canRead = (Deno.permissions.querySync?.({name: 'read'}).state == 'granted') ?? true;
if (!canRead) return processResource;

Expand Down

0 comments on commit 8c2a6aa

Please sign in to comment.