Skip to content

Commit

Permalink
feat(js): Add Lightstep Adapter (#84)
Browse files Browse the repository at this point in the history
* feat(js): Honeycomb adapter

* feat: abstract interval logic in Adapter, modify OTEL payload to parse correctly

* feat: abstract interval logic in Adapter, modify OTEL payload to parse correctly

* fix(js): use nanosecond scale for timing. fix parentId assignment

* fix(js): Use nanoseconds for timing metrics in collector

* fix(js): demangle multi-initialization issue, handling multiple traces

* fix(js): demangle multi-initialization issue, handling multiple traces

* chore: update types and remove build artifacts from web test

* feat(js): Add Lightstep adapter

* feat(js): Add Lightstep adapter

* fix: naming conventions, tests

* fix: remove unnecessary deletion of traceId
  • Loading branch information
wikiwong authored Aug 11, 2023
1 parent 959f00f commit 4d40cbd
Show file tree
Hide file tree
Showing 18 changed files with 382 additions and 4 deletions.
4 changes: 2 additions & 2 deletions js/packages/observe-sdk-honeycomb/test/web/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { File, OpenFile, WASI } from "@bjorn3/browser_wasi_shim";

const f = async () => {
const config = {
apiKey: '',
apiKey: 'YOUR_API_KEY_HERE',
dataset: 'web',
emitTracesInterval: 1000,
traceBatchMax: 100,
host: 'https://api.honeycomb.io',
}
const adapter = new HoneycombAdapter(config);
const resp = await fetch("count_vowels.instr.wasm");
const resp = await fetch("test.c.instr.wasm");

const bytes = await resp.arrayBuffer();
const traceContext = await adapter.start(bytes);
Expand Down
Binary file not shown.
4 changes: 4 additions & 0 deletions js/packages/observe-sdk-lightstep/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export {
LightstepAdapter,
LightstepConfig,
} from '../../src/lib/adapters/lightstep/mod.js';
13 changes: 13 additions & 0 deletions js/packages/observe-sdk-lightstep/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions js/packages/observe-sdk-lightstep/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "@dylibso/observe-sdk-lightstep",
"version": "1.0.0",
"description": "A library that produces function tracing to lightstep",
"directories": {
"test": "test"
},
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/types/index.d.ts",
"exports": {
".": {
"types": "./dist/types/index.d.ts",
"require": "./dist/cjs/index.js",
"default": "./dist/esm/index.js"
}
},
"scripts": {
"build:esm": "node ../../esbuild/esbuild.js -b -e ./index.js -o ../observe-sdk-lightstep/dist/esm/index.js -p browser -f esm",
"build:cjs": "node ../../esbuild/esbuild.js -b -e ./index.js -o ../observe-sdk-lightstep/dist/cjs/index.js -p browser -f cjs",
"build:types": "tsc -b",
"build": "npm run build:esm && npm run build:cjs && npm run build:types",
"build:web-test": "node ../../esbuild/esbuild.js -b -e ./test/web/index.js -o ./test/web/build.js -p browser",
"test:node": "node test/node/index.js",
"test:deno": "deno run -A test/deno/index.ts",
"test:web": "npm run build:web-test && npx serve ./test/web"
},
"keywords": [
"dylibso",
"lightstep",
"tracing",
"observe",
"opentelemetry",
"otel",
"wasm",
"webassembly"
],
"author": "",
"license": "ISC"
}
36 changes: 36 additions & 0 deletions js/packages/observe-sdk-lightstep/test/deno/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { LightstepAdapter, LightstepConfig } from "../../dist/esm/index.js";
import Context from "https://deno.land/[email protected]/wasi/snapshot_preview1.ts";
import { load } from "https://deno.land/std/dotenv/mod.ts";

const env = await load();
const apiKey = env["LIGHTSTEP_API_KEY"];

const config: LightstepConfig = {
apiKey: apiKey,
serviceName: 'deno',
emitTracesInterval: 1000,
traceBatchMax: 100,
host: 'https://ingest.lightstep.com',
}
const adapter = new LightstepAdapter(config);

const bytes = await Deno.readFile("../../test-data/test.c.instr.wasm");
const traceContext = await adapter.start(bytes);
const module = new WebAssembly.Module(bytes);

const runtime = new Context({
stdin: Deno.stdin.rid,
stdout: Deno.stdout.rid,
});
const instance = new WebAssembly.Instance(
module,
{
"wasi_snapshot_preview1": runtime.exports,
...traceContext.getImportObject(),
},
);
runtime.start(instance);

traceContext.stop();

setTimeout(() => { }, 3000);
37 changes: 37 additions & 0 deletions js/packages/observe-sdk-lightstep/test/node/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const fs = require("fs");
const { WASI } = require("wasi");
const { env, argv } = require('node:process');
const { LightstepAdapter } = require("@dylibso/observe-sdk-lightstep");
require('dotenv').config();

const wasi = new WASI({
version: "preview1",
args: argv.slice(1),
env,
});

const config = {
apiKey: process.env.LIGHTSTEP_API_KEY,
serviceName: 'node',
emitTracesInterval: 1000,
traceBatchMax: 100,
host: 'https://ingest.lightstep.com',
}
const adapter = new LightstepAdapter(config);

const bytes = fs.readFileSync("../../test-data/test.c.instr.wasm");
adapter.start(bytes).then((traceContext) => {
const module = new WebAssembly.Module(bytes);

WebAssembly.instantiate(module, {
...wasi.getImportObject(),
...traceContext.getImportObject(),
}).then((instance) => {
wasi.start(instance);
// adapter.setMetadata({
// http_status_code: 200,
// http_url: "https://example.com",
// });
traceContext.stop();
});
});
33 changes: 33 additions & 0 deletions js/packages/observe-sdk-lightstep/test/node/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions js/packages/observe-sdk-lightstep/test/node/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"dotenv": "^16.3.1"
}
}
Binary file not shown.
13 changes: 13 additions & 0 deletions js/packages/observe-sdk-lightstep/test/web/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<script src="./build.js"></script>
</head>

<body>

</body>

</html>
39 changes: 39 additions & 0 deletions js/packages/observe-sdk-lightstep/test/web/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { LightstepAdapter } from "@dylibso/observe-sdk-lightstep";
import { File, OpenFile, WASI } from "@bjorn3/browser_wasi_shim";

const f = async () => {
const config = {
apiKey: 'YOUR_API_KEY_HERE',
serviceName: 'web',
emitTracesInterval: 1000,
traceBatchMax: 100,
host: 'https://ingest.lightstep.com',
}
const adapter = new LightstepAdapter(config);
const resp = await fetch("test.c.instr.wasm");

const bytes = await resp.arrayBuffer();
const traceContext = await adapter.start(bytes);

let fds = [
new OpenFile(
new File(
new TextEncoder("utf-8").encode(`count these vowels for me please`),
),
), // stdin
new OpenFile(new File([])), // stdout
new OpenFile(new File([])), // stderr
];
let wasi = new WASI([], [], fds);
const instance = await WebAssembly.instantiate(bytes, {
"wasi_snapshot_preview1": wasi.wasiImport,
...traceContext.getImportObject(),
});

wasi.start(instance.instance);
let utf8decoder = new TextDecoder();
console.log(utf8decoder.decode(fds[1].file.data));
traceContext.stop();
};

f().then(() => { });
33 changes: 33 additions & 0 deletions js/packages/observe-sdk-lightstep/test/web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions js/packages/observe-sdk-lightstep/test/web/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"dotenv": "^16.3.1"
}
}
Binary file not shown.
18 changes: 18 additions & 0 deletions js/packages/observe-sdk-lightstep/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"include": [
"index.ts",
],
"compilerOptions": {
"allowJs": true,
"declaration": true,
"emitDeclarationOnly": true,
"outDir": "dist/types",
"outFile": "dist/types/index.d.ts",
"declarationMap": true,
"allowImportingTsExtensions": true,
"lib": [
"DOM",
"ESNext",
]
}
}
4 changes: 2 additions & 2 deletions js/src/lib/adapters/honeycomb/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const defaultConfig: HoneycombConfig = {
dataset: 'default-dataset',
emitTracesInterval: 1000,
traceBatchMax: 100,
host: 'https://api.honeycomb.io/',
host: 'https://api.honeycomb.io',
}

export interface HoneycombConfig extends AdapterConfig {
Expand Down Expand Up @@ -46,7 +46,7 @@ export class HoneycombAdapter extends Adapter {

private tracesEndpoint() {
const endpoint = new URL(this.config.host);
endpoint.pathname = `v1/traces`;
endpoint.pathname = `/v1/traces`;
return endpoint;
}

Expand Down
Loading

0 comments on commit 4d40cbd

Please sign in to comment.