Skip to content

Commit 3eea06f

Browse files
committed
A working build
1 parent 4363587 commit 3eea06f

File tree

132 files changed

+146
-117
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

132 files changed

+146
-117
lines changed

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"@opentelemetry/core": "./vendor/opentelemetry-core/mod.ts",
55
"@opentelemetry/exporter-prometheus": "./vendor/opentelemetry-exporter-prometheus/mod.ts",
66
"@opentelemetry/resources": "./vendor/opentelemetry-resources/mod.ts",
7-
"@opentelemetry/sdk-metrics": "./vendor/sdk-metrics/mod.ts",
7+
"@opentelemetry/sdk-metrics": "./vendor/opentelemetry-sdk-metrics/mod.ts",
88
"@opentelemetry/semantic-conventions": "./vendor/opentelemetry-semantic-conventions/mod.ts"
99
}
1010
}
Lines changed: 5 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { UpDownCounter } from "@opentelemetry/api";
22

3-
import { getMeter } from "./instrumentation.ts";
4-
import { getRuntime, Runtime } from "./utils.ts";
53
import { BUILD_INFO_DESCRIPTION, BUILD_INFO_NAME } from "./constants.ts";
4+
import { getBranch, getCommit, getVersion } from "./platform.deno.ts";
5+
import { getMeter } from "./instrumentation.ts";
66

77
/**
88
* BuildInfo is used to create the `build_info` metric that
@@ -73,73 +73,13 @@ export function setBuildInfo() {
7373
return buildInfo;
7474
}
7575

76-
const runtime = getRuntime();
77-
7876
buildInfo = {
79-
version: getVersion(runtime),
80-
commit: getCommit(runtime),
81-
branch: getBranch(runtime),
77+
version: getVersion(),
78+
commit: getCommit(),
79+
branch: getBranch(),
8280
};
8381

8482
recordBuildInfo(buildInfo);
8583

8684
return buildInfo;
8785
}
88-
89-
/**
90-
* Gets the version of the application.
91-
*
92-
* @internal
93-
*/
94-
function getVersion(runtime: Runtime): string | undefined {
95-
if (runtime === "node") {
96-
// @ts-ignore
97-
if (process.env.npm_package_version) {
98-
// @ts-ignore
99-
return process.env.npm_package_version;
100-
}
101-
// @ts-ignore
102-
return process.env.PACKAGE_VERSION || process.env.AUTOMETRICS_VERSION;
103-
}
104-
105-
if (runtime === "deno") {
106-
return (
107-
//@ts-ignore
108-
Deno.env.get("AUTOMETRICS_VERSION") || Deno.env.get("PACKAGE_VERSION")
109-
);
110-
}
111-
}
112-
113-
/**
114-
* Gets the commit hash of the current state of the application.
115-
*
116-
* @internal
117-
*/
118-
function getCommit(runtime: Runtime) {
119-
if (runtime === "node") {
120-
// @ts-ignore
121-
return process.env.COMMIT_SHA || process.env.AUTOMETRICS_COMMIT;
122-
}
123-
124-
if (runtime === "deno") {
125-
//@ts-ignore
126-
return Deno.env.get("AUTOMETRICS_COMMIT");
127-
}
128-
}
129-
130-
/**
131-
* Gets the current branch of the application.
132-
*
133-
* @internal
134-
*/
135-
function getBranch(runtime: Runtime) {
136-
if (runtime === "node") {
137-
// @ts-ignore
138-
return process.env.BRANCH_NAME || process.env.AUTOMETRICS_BRANCH;
139-
}
140-
141-
if (runtime === "deno") {
142-
//@ts-ignore
143-
return Deno.env.get("AUTOMETRICS_BRANCH");
144-
}
145-
}
File renamed without changes.
File renamed without changes.

packages/lib/mod.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export { init, type InitOptions } from "./src/instrumentation.ts";
2-
export * from "./src/objectives.ts";
3-
export * from "./src/wrappers.ts";
4-
export * from "./src/buildInfo.ts";
1+
export { init, type InitOptions } from "./instrumentation.ts";
2+
export * from "./objectives.ts";
3+
export * from "./wrappers.ts";
4+
export * from "./buildInfo.ts";
File renamed without changes.

packages/lib/platform.deno.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Guard all the calls to the `Deno` global so that the Deno build is usable
2+
// in browsers too.
3+
const isDeno =
4+
"Deno" in globalThis &&
5+
Deno &&
6+
"cwd" in Deno &&
7+
typeof Deno.cwd === "function";
8+
9+
export function getRootDir(): string {
10+
return isDeno ? Deno.cwd() : "";
11+
}
12+
13+
/**
14+
* Returns the version of the application.
15+
*
16+
* @internal
17+
*/
18+
export function getVersion(): string | undefined {
19+
if (isDeno) {
20+
return (
21+
Deno.env.get("AUTOMETRICS_VERSION") || Deno.env.get("PACKAGE_VERSION")
22+
);
23+
}
24+
}
25+
26+
/**
27+
* Returns the commit hash of the current state of the application.
28+
*
29+
* @internal
30+
*/
31+
export function getCommit(): string | undefined {
32+
if (isDeno) {
33+
return Deno.env.get("COMMIT_SHA") || Deno.env.get("AUTOMETRICS_COMMIT");
34+
}
35+
}
36+
37+
/**
38+
* Returns the current branch of the application.
39+
*
40+
* @internal
41+
*/
42+
export function getBranch(): string | undefined {
43+
if (isDeno) {
44+
return Deno.env.get("BRANCH_NAME") || Deno.env.get("AUTOMETRICS_BRANCH");
45+
}
46+
}

packages/lib/platform.node.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Guard the call to `process.cwd()` so that the NPM build is usable in browsers
2+
// too. We *don't* guard the accesses to `process.env` since bundlers commonly
3+
// allow implementing replacements on that object.
4+
const isNode =
5+
"process" in globalThis &&
6+
// @ts-ignore
7+
process &&
8+
// @ts-ignore
9+
"cwd" in process &&
10+
// @ts-ignore
11+
typeof process.cwd === "function";
12+
13+
export function getRootDir(): string {
14+
// @ts-ignore
15+
return isNode ? process.cwd() : "";
16+
}
17+
18+
/**
19+
* Returns the version of the application.
20+
*
21+
* @internal
22+
*/
23+
export function getVersion(): string | undefined {
24+
return (
25+
// @ts-ignore
26+
process.env.npm_package_version ||
27+
// @ts-ignore
28+
process.env.PACKAGE_VERSION ||
29+
// @ts-ignore
30+
process.env.AUTOMETRICS_VERSION
31+
);
32+
}
33+
34+
/**
35+
* Returns the commit hash of the current state of the application.
36+
*
37+
* @internal
38+
*/
39+
export function getCommit(): string | undefined {
40+
// @ts-ignore
41+
return process.env.COMMIT_SHA || process.env.AUTOMETRICS_COMMIT;
42+
}
43+
44+
/**
45+
* Returns the current branch of the application.
46+
*
47+
* @internal
48+
*/
49+
export function getBranch(): string | undefined {
50+
// @ts-ignore
51+
return process.env.BRANCH_NAME || process.env.AUTOMETRICS_BRANCH;
52+
}

packages/lib/tests/buildInfo.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import {
44
AggregationTemporality,
55
} from "@opentelemetry/sdk-metrics";
66
import { assertMatch } from "./deps.ts";
7-
import { init } from "../src/instrumentation.ts";
8-
import { getMetricsProvider } from "../src/instrumentation.ts";
7+
import { init } from "../instrumentation.ts";
8+
import { getMetricsProvider } from "../instrumentation.ts";
99
import { collectAndSerialize } from "./util.ts";
1010

1111
const buildInfo = {

packages/lib/tests/concurrency.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
} from "@opentelemetry/sdk-metrics";
55
import { assertMatch } from "./deps.ts";
66
import { autometrics, init } from "../mod.ts";
7-
import { getMetricsProvider } from "../src/instrumentation.ts";
7+
import { getMetricsProvider } from "../instrumentation.ts";
88
import { collectAndSerialize } from "./util.ts";
99

1010
/*Deno.test("Autometrics concurrency tests", async (t) => {

packages/lib/tests/instrumentation.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { assertEquals } from "./deps.ts";
2-
import { getMetricsProvider } from "../src/instrumentation.ts";
2+
import { getMetricsProvider } from "../instrumentation.ts";
33

44
Deno.test("Autometrics initializer", async (t) => {
55
await t.step("Test if autometrics initializes only once", () => {

packages/lib/tests/integration.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
PeriodicExportingMetricReader,
66
} from "@opentelemetry/sdk-metrics";
77
import { assertMatch, assertRejects } from "./deps.ts";
8-
import { getMetricsProvider } from "../src/instrumentation.ts";
8+
import { getMetricsProvider } from "../instrumentation.ts";
99
import { collectAndSerialize } from "./util.ts";
1010

1111
/*Deno.test("Autometrics integration test", async (t) => {

packages/lib/tests/objectives.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
ObjectiveLatency,
1010
ObjectivePercentile,
1111
} from "../mod.ts";
12-
import { getMetricsProvider } from "../src/instrumentation.ts";
12+
import { getMetricsProvider } from "../instrumentation.ts";
1313
import { collectAndSerialize } from "./util.ts";
1414

1515
/*Deno.test("Autometrics objectives test", async (t) => {

packages/lib/tests/utils.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { assertEquals } from "./deps.ts";
2-
import { getModulePath } from "../src/utils.ts";
2+
import { getModulePath } from "../utils.ts";
33

44
// the existing getModulePath function is a hack that parses the stack trace
55
// as string and extracts the module path from it. This test is to ensure that

packages/lib/src/utils.ts renamed to packages/lib/utils.ts

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,4 @@
1-
export type Runtime = "node" | "deno" | "browser" | "unknown";
2-
3-
export function getRuntime(): Runtime {
4-
// @ts-ignore
5-
if (typeof process === "object" && "cwd" in process) {
6-
return "node";
7-
}
8-
9-
// @ts-ignore
10-
if (typeof Deno === "object") {
11-
return "deno";
12-
}
13-
14-
if (typeof window === "object") {
15-
return "browser";
16-
}
17-
18-
return "unknown";
19-
}
1+
import { getRootDir } from "./platform.deno.ts";
202

213
// HACK: this entire function is a hacky way to acquire the module name for a
224
// given function e.g.: dist/index.js
@@ -32,23 +14,6 @@ export function getModulePath(): string | undefined {
3214
name: callSite.getFunctionName(),
3315
file: callSite.getFileName(),
3416
}));
35-
36-
let rootDir: string;
37-
const runtime = getRuntime();
38-
if (runtime === "browser") {
39-
rootDir = "";
40-
} else if (runtime === "deno") {
41-
//@ts-ignore
42-
rootDir = Deno.cwd();
43-
} else if (runtime === "node") {
44-
// HACK: this assumes the entire app was run from the root directory of the
45-
// project
46-
// @ts-ignore
47-
rootDir = process.cwd();
48-
} else {
49-
rootDir = "";
50-
}
51-
5217
if (!stack) {
5318
return;
5419
}
@@ -74,6 +39,7 @@ export function getModulePath(): string | undefined {
7439

7540
// We split away everything up to the root directory of the project,
7641
// if the path contains file:// we need to remove it
42+
const rootDir = getRootDir();
7743
return wrappedFunctionPath.replace(
7844
containsFileProtocol ? `file://${rootDir}` : rootDir,
7945
"",

packages/lib/src/wrappers.ts renamed to packages/lib/wrappers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Attributes } from "npm:@opentelemetry/api";
1+
import { Attributes } from "@opentelemetry/api";
22
import { setBuildInfo } from "./buildInfo.ts";
33
import {
44
COUNTER_DESCRIPTION,

scripts/build_npm.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,44 @@ const packageJsonFields = {
3434
publishConfig: {
3535
access: "public",
3636
},
37+
devDependencies: {
38+
"@opentelemetry/api": "^1.4.0",
39+
"@types/node": "^18.6.5",
40+
},
3741
};
3842

3943
for (const [dir, name] of Object.entries(packages)) {
4044
await build({
4145
entryPoints: [`packages/${dir}/mod.ts`],
4246
outDir: `${OUT_DIR}/${dir}`,
43-
shims: {
44-
deno: true,
45-
},
47+
shims: { deno: false },
4648
package: {
4749
name,
4850
...packageJsonFields,
4951
},
52+
importMap: "./deno.json",
53+
mappings: {
54+
"./packages/lib/platform.deno.ts": "./packages/lib/platform.node.ts",
55+
"./vendor/opentelemetry-api/mod.ts": {
56+
name: "@opentelemetry/api",
57+
peerDependency: true,
58+
version: "^1.4.0",
59+
},
60+
"./vendor/opentelemetry-exporter-prometheus/mod.ts": {
61+
name: "@opentelemetry/exporter-prometheus",
62+
version: "^0.41.0",
63+
},
64+
"./vendor/opentelemetry-sdk-metrics/mod.ts": {
65+
name: "@opentelemetry/sdk-metrics",
66+
version: "^1.15.0",
67+
},
68+
},
69+
packageManager: "yarn",
70+
test: false,
71+
filterDiagnostic: (diagnostic) =>
72+
// Ignore messages about `fetch` in Node.js. It's a TODO to have a better
73+
// fallback, but there's already a guard around its usage.
74+
!diagnostic.messageText.toString().includes("Cannot find name 'fetch'"),
5075
postBuild() {
5176
// steps to run after building and before running the tests
5277
Deno.copyFileSync("LICENSE", `${OUT_DIR}/${dir}/LICENSE`);
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)