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

try to fix typescript tracing #731

Closed
wants to merge 2 commits into from
Closed
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
36 changes: 24 additions & 12 deletions engine/baml-runtime/src/tracing/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
pub mod api_wrapper;
#[cfg(not(target_arch = "wasm32"))]
mod threaded_tracer;
#[cfg(target_arch = "wasm32")]
mod wasm_tracer;

use crate::on_log_event::LogEventCallbackSync;
use anyhow::Result;
use baml_types::{BamlMap, BamlMediaType, BamlValue};
use cfg_if::cfg_if;
use colored::Colorize;
use internal_baml_jinja::RenderedPrompt;
use std::collections::HashMap;
Expand All @@ -27,16 +24,17 @@ use self::api_wrapper::{
},
APIWrapper,
};
#[cfg(not(target_arch = "wasm32"))]
use self::threaded_tracer::ThreadedTracer;

#[cfg(target_arch = "wasm32")]
use self::wasm_tracer::NonThreadedTracer;
cfg_if! {
if #[cfg(target_arch = "wasm32")] {
mod wasm_tracer;
use self::wasm_tracer::NonThreadedTracer as TracerImpl;
} else {
mod threaded_tracer;
use self::threaded_tracer::ThreadedTracer as TracerImpl;
}
}

#[cfg(not(target_arch = "wasm32"))]
type TracerImpl = ThreadedTracer;
#[cfg(target_arch = "wasm32")]
type TracerImpl = NonThreadedTracer;
#[derive(Debug)]
pub struct TracingSpan {
span_id: Uuid,
Expand Down Expand Up @@ -95,6 +93,7 @@ impl BamlTracer {
params: &BamlMap<String, BamlValue>,
) -> (Option<TracingSpan>, RuntimeContext) {
let span_id = ctx.enter(function_name);
log::trace!("Entering span: {:#?}:::{:?}", span_id, function_name);
if !self.enabled {
return (None, ctx.create_ctx(tb));
}
Expand Down Expand Up @@ -150,6 +149,12 @@ impl BamlTracer {
ctx
);
};
log::trace!(
"Finishing span: {:#?} {}\nevent chain {:?}",
span,
span_id,
event_chain
);

if span.span_id != span_id {
anyhow::bail!("Span ID mismatch: {} != {}", span.span_id, span_id);
Expand Down Expand Up @@ -211,6 +216,13 @@ impl BamlTracer {
anyhow::bail!("Attempting to finish a span without first starting one");
};

log::trace!(
"Finishing baml span: {:#?} {}\nevent chain {:?}",
span,
span_id,
event_chain
);

if span.span_id != span_id {
anyhow::bail!("Span ID mismatch: {} != {}", span.span_id, span_id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ use crate::GeneratorArgs;
use super::ToTypeReferenceInClientDefinition;

#[derive(askama::Template)]
#[template(path = "type_builder.js.j2", escape = "none")]
#[template(path = "type_builder.ts.j2", escape = "none")]
pub(crate) struct TypeBuilder<'ir> {
enums: Vec<TypescriptEnum<'ir>>,
classes: Vec<TypescriptClass<'ir>>,
}

#[derive(askama::Template)]
#[template(path = "types.js.j2", escape = "none")]
#[template(path = "types.ts.j2", escape = "none")]
pub(crate) struct TypescriptTypes<'ir> {
enums: Vec<TypescriptEnum<'ir>>,
classes: Vec<TypescriptClass<'ir>>,
Expand Down
2 changes: 1 addition & 1 deletion engine/language-client-codegen/src/typescript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use self::typescript_language_features::{ToTypescript, TypescriptLanguageFeature
use crate::dir_writer::FileCollector;

#[derive(askama::Template)]
#[template(path = "client.js.j2", escape = "none")]
#[template(path = "client.ts.j2", escape = "none")]
struct TypescriptClient {
funcs: Vec<TypescriptFunction>,
types: Vec<String>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ export type RecursivePartialNull<T> = T extends object
: T | null;

export class BamlClient {
private runtime: BamlRuntime
private ctx_manager: BamlCtxManager
private stream_client: BamlStreamClient

constructor(private runtime: BamlRuntime, private ctx_manager: BamlCtxManager) {
constructor(runtime: BamlRuntime, ctx_manager: BamlCtxManager) {
this.runtime = runtime
this.ctx_manager = ctx_manager
this.stream_client = new BamlStreamClient(runtime, ctx_manager)
}

Expand Down
6 changes: 3 additions & 3 deletions engine/language_client_typescript/async_context_vars.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { BamlSpan, RuntimeContextManager, BamlRuntime, BamlLogEvent } from './native';
export declare class CtxManager {
export declare class BamlCtxManager {
private rt;
private ctx;
constructor(rt: BamlRuntime);
upsertTags(tags: Record<string, string>): void;
get(): RuntimeContextManager;
startTraceSync(name: string, args: Record<string, any>): BamlSpan;
startTraceAsync(name: string, args: Record<string, any>): BamlSpan;
startTraceSync(name: string, args: Record<string, any>): [RuntimeContextManager, BamlSpan];
startTraceAsync(name: string, args: Record<string, any>): [RuntimeContextManager, BamlSpan];
endTrace(span: BamlSpan, response: any): void;
flush(): void;
onLogEvent(callback: (event: BamlLogEvent) => void): void;
Expand Down

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

60 changes: 30 additions & 30 deletions engine/language_client_typescript/async_context_vars.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CtxManager = void 0;
exports.BamlCtxManager = void 0;
const native_1 = require("./native");
const async_hooks_1 = require("async_hooks");
class CtxManager {
class BamlCtxManager {
rt;
ctx;
constructor(rt) {
Expand All @@ -28,15 +28,11 @@ class CtxManager {
}
startTraceSync(name, args) {
const mng = this.get();
// const clone = mng.deepClone()
// this.ctx.enterWith(clone)
return native_1.BamlSpan.new(this.rt, name, args, mng);
return [mng, native_1.BamlSpan.new(this.rt, name, args, mng)];
}
startTraceAsync(name, args) {
const mng = this.get();
const clone = mng.deepClone();
this.ctx.enterWith(clone);
return native_1.BamlSpan.new(this.rt, name, args, clone);
const mng = this.get().deepClone();
return [mng, native_1.BamlSpan.new(this.rt, name, args, mng)];
}
endTrace(span, response) {
const manager = this.ctx.getStore();
Expand All @@ -62,16 +58,18 @@ class CtxManager {
...acc,
[`arg${i}`]: arg, // generic way to label args
}), {});
const span = this.startTraceSync(name, params);
try {
const response = func(...args);
this.endTrace(span, response);
return response;
}
catch (e) {
this.endTrace(span, e);
throw e;
}
const [mng, span] = this.startTraceSync(name, params);
this.ctx.run(mng, () => {
try {
const response = func(...args);
this.endTrace(span, response);
return response;
}
catch (e) {
this.endTrace(span, e);
throw e;
}
});
});
}
traceFnAync(name, func) {
Expand All @@ -81,17 +79,19 @@ class CtxManager {
...acc,
[`arg${i}`]: arg, // generic way to label args
}), {});
const span = this.startTraceAsync(funcName, params);
try {
const response = await func(...args);
this.endTrace(span, response);
return response;
}
catch (e) {
this.endTrace(span, e);
throw e;
}
const [mng, span] = this.startTraceAsync(name, params);
await this.ctx.run(mng, async () => {
try {
const response = await func(...args);
this.endTrace(span, response);
return response;
}
catch (e) {
this.endTrace(span, e);
throw e;
}
});
});
}
}
exports.CtxManager = CtxManager;
exports.BamlCtxManager = BamlCtxManager;
4 changes: 2 additions & 2 deletions engine/language_client_typescript/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { BamlRuntime, FunctionResult, FunctionResultStream, BamlImage as Image, BamlAudio as Audio, invoke_runtime_cli } from './native';
export { BamlRuntime, FunctionResult, FunctionResultStream, BamlImage as Image, BamlAudio as Audio, invoke_runtime_cli, } from './native';
export { BamlStream } from './stream';
export { CtxManager as BamlCtxManager } from './async_context_vars';
export { BamlCtxManager } from './async_context_vars';
//# sourceMappingURL=index.d.ts.map
2 changes: 1 addition & 1 deletion engine/language_client_typescript/index.d.ts.map

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

2 changes: 1 addition & 1 deletion engine/language_client_typescript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ Object.defineProperty(exports, "invoke_runtime_cli", { enumerable: true, get: fu
var stream_1 = require("./stream");
Object.defineProperty(exports, "BamlStream", { enumerable: true, get: function () { return stream_1.BamlStream; } });
var async_context_vars_1 = require("./async_context_vars");
Object.defineProperty(exports, "BamlCtxManager", { enumerable: true, get: function () { return async_context_vars_1.CtxManager; } });
Object.defineProperty(exports, "BamlCtxManager", { enumerable: true, get: function () { return async_context_vars_1.BamlCtxManager; } });
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BamlSpan, RuntimeContextManager, BamlRuntime, BamlLogEvent } from './native'
import { AsyncLocalStorage } from 'async_hooks'

export class CtxManager {
export class BamlCtxManager {
private rt: BamlRuntime
private ctx: AsyncLocalStorage<RuntimeContextManager>

Expand All @@ -28,18 +28,14 @@ export class CtxManager {
return store
}

startTraceSync(name: string, args: Record<string, any>): BamlSpan {
startTraceSync(name: string, args: Record<string, any>): [RuntimeContextManager, BamlSpan] {
const mng = this.get()
// const clone = mng.deepClone()
// this.ctx.enterWith(clone)
return BamlSpan.new(this.rt, name, args, mng)
return [mng, BamlSpan.new(this.rt, name, args, mng)]
}

startTraceAsync(name: string, args: Record<string, any>): BamlSpan {
const mng = this.get()
const clone = mng.deepClone()
this.ctx.enterWith(clone)
return BamlSpan.new(this.rt, name, args, clone)
startTraceAsync(name: string, args: Record<string, any>): [RuntimeContextManager, BamlSpan] {
const mng = this.get().deepClone()
return [mng, BamlSpan.new(this.rt, name, args, mng)]
}

endTrace(span: BamlSpan, response: any): void {
Expand Down Expand Up @@ -72,16 +68,17 @@ export class CtxManager {
}),
{},
)
const span = this.startTraceSync(name, params)

try {
const response = func(...args)
this.endTrace(span, response)
return response
} catch (e) {
this.endTrace(span, e)
throw e
}
const [mng, span] = this.startTraceSync(name, params)
this.ctx.run(mng, () => {
try {
const response = func(...args)
this.endTrace(span, response)
return response
} catch (e) {
this.endTrace(span, e)
throw e
}
})
})
}

Expand All @@ -95,15 +92,17 @@ export class CtxManager {
}),
{},
)
const span = this.startTraceAsync(funcName, params)
try {
const response = await func(...args)
this.endTrace(span, response)
return response
} catch (e) {
this.endTrace(span, e)
throw e
}
const [mng, span] = this.startTraceAsync(name, params)
await this.ctx.run(mng, async () => {
try {
const response = await func(...args)
this.endTrace(span, response)
return response
} catch (e) {
this.endTrace(span, e)
throw e
}
})
})
}
}
11 changes: 9 additions & 2 deletions engine/language_client_typescript/typescript_src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
export { BamlRuntime, FunctionResult, FunctionResultStream, BamlImage as Image, BamlAudio as Audio, invoke_runtime_cli } from './native'
export {
BamlRuntime,
FunctionResult,
FunctionResultStream,
BamlImage as Image,
BamlAudio as Audio,
invoke_runtime_cli,
} from './native'
export { BamlStream } from './stream'
export { CtxManager as BamlCtxManager } from './async_context_vars'
export { BamlCtxManager } from './async_context_vars'
25 changes: 17 additions & 8 deletions integ-tests/baml_src/clients.baml
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,27 @@ client<llm> Gemini {
}

client<llm> AwsBedrock {
provider aws-bedrock
provider anthropic
options {
inference_configuration {
max_tokens 100
}
model_id "anthropic.claude-3-haiku-20240307-v1:0"
// model_id "meta.llama3-8b-instruct-v1:0"
// model_id "mistral.mistral-7b-instruct-v0:2"
api_key ""
model claude-3-haiku-20240307
api_key env.ANTHROPIC_API_KEY
max_tokens 1000
}
}

// client<llm> AwsBedrock {
// provider aws-bedrock
// options {
// inference_configuration {
// max_tokens 100
// }
// model_id "anthropic.claude-3-haiku-20240307-v1:0"
// // model_id "meta.llama3-8b-instruct-v1:0"
// // model_id "mistral.mistral-7b-instruct-v0:2"
// api_key ""
// }
// }

client<llm> Claude {
provider anthropic
options {
Expand Down
Loading
Loading