Skip to content

Commit

Permalink
update oTel, add ConsoleExporter, tracing level
Browse files Browse the repository at this point in the history
  • Loading branch information
fetsorn committed Jul 13, 2022
1 parent 009b90d commit d914308
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 270 deletions.
54 changes: 46 additions & 8 deletions packages/js/client/src/PolywrapClient.ts
Expand Up @@ -49,11 +49,13 @@ import {
} from "@polywrap/core-js";
import { msgpackEncode, msgpackDecode } from "@polywrap/msgpack-js";
import { WrapManifest } from "@polywrap/wrap-manifest-types-js";
import { Tracer } from "@polywrap/tracing-js";
import { Tracer, TracingLevel } from "@polywrap/tracing-js";

export interface PolywrapClientConfig<TUri extends Uri | string = string>
extends ClientConfig<TUri> {
tracingEnabled: boolean;
tracingLevel: TracingLevel;
tracingDetailed: boolean;
}

export class PolywrapClient implements Client {
Expand All @@ -69,6 +71,8 @@ export class PolywrapClient implements Client {
envs: [],
uriResolvers: [],
tracingEnabled: false,
tracingLevel: TracingLevel.High,
tracingDetailed: false,
};

// Invoke specific contexts
Expand All @@ -79,7 +83,11 @@ export class PolywrapClient implements Client {
options?: { noDefaults?: boolean }
) {
try {
this.setTracingEnabled(!!config?.tracingEnabled);
this.setTracingEnabled(
!!config?.tracingEnabled,
config?.tracingLevel ?? TracingLevel.High,
!!config?.tracingDetailed
);

Tracer.startSpan("PolywrapClient: constructor");

Expand All @@ -97,6 +105,10 @@ export class PolywrapClient implements Client {
: [],
uriResolvers: config.uriResolvers ?? [],
tracingEnabled: !!config.tracingEnabled,
tracingLevel: config.tracingLevel
? config.tracingLevel
: TracingLevel.High,
tracingDetailed: !!config.tracingDetailed,
};
}

Expand All @@ -117,13 +129,19 @@ export class PolywrapClient implements Client {
}
}

public setTracingEnabled(enable: boolean): void {
public setTracingEnabled(
enable: boolean,
tracingLevel: TracingLevel,
detail: boolean
): void {
if (enable) {
Tracer.enableTracing("PolywrapClient");
Tracer.enableTracing("PolywrapClient", tracingLevel, detail);
} else {
Tracer.disableTracing();
}
this._config.tracingEnabled = enable;
this._config.tracingLevel = tracingLevel;
this._config.tracingDetailed = detail;
}

@Tracer.traceMethod("PolywrapClient: getRedirects")
Expand Down Expand Up @@ -222,7 +240,7 @@ export class PolywrapClient implements Client {
) as TUri[]);
}

@Tracer.traceMethod("PolywrapClient: query")
@Tracer.traceMethod("PolywrapClient: query", TracingLevel.High)
public async query<
TData extends Record<string, unknown> = Record<string, unknown>,
TVariables extends Record<string, unknown> = Record<string, unknown>,
Expand Down Expand Up @@ -359,7 +377,7 @@ export class PolywrapClient implements Client {
return { error };
}

@Tracer.traceMethod("PolywrapClient: run")
@Tracer.traceMethod("PolywrapClient: run", TracingLevel.High)
public async run<
TData extends Record<string, unknown> = Record<string, unknown>,
TUri extends Uri | string = string
Expand Down Expand Up @@ -467,7 +485,7 @@ export class PolywrapClient implements Client {
return subscription;
}

@Tracer.traceMethod("PolywrapClient: resolveUri")
@Tracer.traceMethod("PolywrapClient: resolveUri", TracingLevel.High)
public async resolveUri<TUri extends Uri | string>(
uri: TUri,
options?: ResolveUriOptions<ClientConfig>
Expand Down Expand Up @@ -508,6 +526,20 @@ export class PolywrapClient implements Client {
this._clearContext(contextId);
}

let uriHistoryTrace = "";
for (const item of uriHistory.getResolutionPath().stack) {
const itemTrace = `${item.uriResolver} resolved uri to ${
item.result.uri
}${item.result.wrapper ? ", found wrapper" : ""}`;
uriHistoryTrace = uriHistoryTrace + "\n" + itemTrace;
}

Tracer.setAttribute(
"label",
uriHistoryTrace.substring(1),
TracingLevel.High
);

return {
wrapper,
uri: resolvedUri,
Expand Down Expand Up @@ -743,6 +775,10 @@ export class PolywrapClient implements Client {
envs: context?.envs ? sanitizeEnvs(context.envs) : config.envs,
uriResolvers: context?.uriResolvers ?? config.uriResolvers,
tracingEnabled: context?.tracingEnabled || config.tracingEnabled,
tracingLevel: context?.tracingLevel
? context.tracingLevel
: config.tracingLevel,
tracingDetailed: context?.tracingDetailed || config.tracingDetailed,
});

return {
Expand All @@ -758,11 +794,13 @@ export class PolywrapClient implements Client {
}
}

@Tracer.traceMethod("PolywrapClient: _loadWrapper")
@Tracer.traceMethod("PolywrapClient: _loadWrapper", TracingLevel.High)
private async _loadWrapper(
uri: Uri,
options?: Contextualized
): Promise<Wrapper> {
Tracer.setAttribute("label", `Wrapper loaded: ${uri}`, TracingLevel.High);

const { wrapper, uriHistory, error } = await this.resolveUri(uri, {
contextId: options?.contextId,
});
Expand Down
9 changes: 7 additions & 2 deletions packages/js/client/src/plugin/PluginWrapper.ts
Expand Up @@ -13,7 +13,7 @@ import {
} from "@polywrap/core-js";
import { WrapManifest } from "@polywrap/wrap-manifest-types-js";
import { msgpackDecode } from "@polywrap/msgpack-js";
import { Tracer } from "@polywrap/tracing-js";
import { Tracer, TracingLevel } from "@polywrap/tracing-js";

export class PluginWrapper extends Wrapper {
private _instance: PluginModule<unknown> | undefined;
Expand Down Expand Up @@ -52,11 +52,16 @@ export class PluginWrapper extends Wrapper {
throw Error("client.getManifest(...) is not implemented for Plugins.");
}

@Tracer.traceMethod("PluginWrapper: invoke")
@Tracer.traceMethod("PluginWrapper: invoke", TracingLevel.High)
public async invoke(
options: InvokeOptions<Uri>,
client: Client
): Promise<InvocableResult<unknown>> {
Tracer.setAttribute(
"label",
`Plugin Wrapper invoked: ${options.uri.uri}, with method ${options.method}`,
TracingLevel.High
);
try {
const { method } = options;
const args = options.args || {};
Expand Down
9 changes: 7 additions & 2 deletions packages/js/client/src/wasm/WasmWrapper.ts
Expand Up @@ -21,7 +21,7 @@ import {
WrapManifest,
} from "@polywrap/wrap-manifest-types-js";
import { msgpackEncode } from "@polywrap/msgpack-js";
import { Tracer } from "@polywrap/tracing-js";
import { Tracer, TracingLevel } from "@polywrap/tracing-js";
import { AsyncWasmInstance } from "@polywrap/asyncify-js";

type InvokeResultOrError =
Expand Down Expand Up @@ -147,11 +147,16 @@ export class WasmWrapper extends Wrapper {
return deserializeWrapManifest(data);
}

@Tracer.traceMethod("WasmWrapper: invoke")
@Tracer.traceMethod("WasmWrapper: invoke", TracingLevel.High)
public async invoke(
options: InvokeOptions<Uri>,
client: Client
): Promise<InvocableResult<Uint8Array>> {
Tracer.setAttribute(
"label",
`WASM Wrapper invoked: ${options.uri.uri}, with method ${options.method}`,
TracingLevel.High
);
try {
const { method } = options;
const args = options.args || {};
Expand Down
1 change: 1 addition & 0 deletions packages/js/react/package.json
Expand Up @@ -20,6 +20,7 @@
},
"dependencies": {
"@polywrap/client-js": "0.1.1",
"@polywrap/tracing-js": "0.1.1",
"@polywrap/core-js": "0.1.1"
},
"devDependencies": {
Expand Down
5 changes: 3 additions & 2 deletions packages/js/react/src/provider.tsx
@@ -1,6 +1,7 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import React from "react";
import { PolywrapClient, PolywrapClientConfig } from "@polywrap/client-js";
import { TracingLevel } from "@polywrap/tracing-js";

type ClientContext = React.Context<PolywrapClient>

Expand Down Expand Up @@ -33,7 +34,7 @@ export function createPolywrapProvider(
ClientContext: React.createContext({} as PolywrapClient)
};

return ({ envs, redirects, plugins, interfaces, children }) => {
return ({ envs, redirects, plugins, interfaces, tracingEnabled = false, tracingLevel = TracingLevel.High, tracingDetailed = false, children }) => {

const [clientCreated, setClientCreated] = React.useState(false);

Expand All @@ -47,7 +48,7 @@ export function createPolywrapProvider(
}

// Instantiate the client
PROVIDERS[name].client = new PolywrapClient({ redirects, plugins, interfaces, envs });
PROVIDERS[name].client = new PolywrapClient({ redirects, plugins, interfaces, envs, tracingEnabled, tracingLevel, tracingDetailed });

setClientCreated(true);

Expand Down
19 changes: 9 additions & 10 deletions packages/js/tracing/package.json
Expand Up @@ -19,16 +19,15 @@
"test:watch": "jest --watch --passWithNoTests --verbose"
},
"dependencies": {
"@opentelemetry/api": "0.20.0",
"@opentelemetry/context-zone": "0.20.0",
"@opentelemetry/core": "0.20.0",
"@opentelemetry/exporter-collector": "0.20.0",
"@opentelemetry/exporter-jaeger": "0.20.0",
"@opentelemetry/exporter-zipkin": "0.20.0",
"@opentelemetry/node": "0.20.0",
"@opentelemetry/propagator-b3": "0.20.0",
"@opentelemetry/tracing": "0.20.0",
"@opentelemetry/web": "0.20.0",
"@opentelemetry/api": "1.1.0",
"@opentelemetry/context-zone": "1.4.0",
"@opentelemetry/core": "1.4.0",
"@opentelemetry/exporter-zipkin": "1.4.0",
"@opentelemetry/sdk-trace-node": "1.4.0",
"@opentelemetry/propagator-b3": "1.4.0",
"@opentelemetry/sdk-trace-base": "1.4.0",
"@opentelemetry/sdk-trace-web": "1.4.0",
"@fetsorn/opentelemetry-console-exporter": "0.0.1",
"browser-util-inspect": "0.2.0"
},
"devDependencies": {
Expand Down
4 changes: 0 additions & 4 deletions packages/js/tracing/src/declarations.d.ts

This file was deleted.

0 comments on commit d914308

Please sign in to comment.