@@ -13,6 +13,8 @@ import { logger } from "./services/logger.server";
1313import { isValidDatabaseUrl } from "./utils/db" ;
1414import { singleton } from "./utils/singleton" ;
1515import { $transaction as transac } from "@trigger.dev/database" ;
16+ import { startActiveSpan } from "./v3/tracer.server" ;
17+ import { Span } from "@opentelemetry/api" ;
1618
1719export type {
1820 PrismaTransactionClient ,
@@ -21,25 +23,76 @@ export type {
2123 PrismaReplicaClient ,
2224} ;
2325
26+ export async function $transaction < R > (
27+ prisma : PrismaClientOrTransaction ,
28+ name : string ,
29+ fn : ( prisma : PrismaTransactionClient , span ?: Span ) => Promise < R > ,
30+ options ?: PrismaTransactionOptions
31+ ) : Promise < R | undefined > ;
2432export async function $transaction < R > (
2533 prisma : PrismaClientOrTransaction ,
2634 fn : ( prisma : PrismaTransactionClient ) => Promise < R > ,
2735 options ?: PrismaTransactionOptions
36+ ) : Promise < R | undefined > ;
37+ export async function $transaction < R > (
38+ prisma : PrismaClientOrTransaction ,
39+ fnOrName : ( ( prisma : PrismaTransactionClient ) => Promise < R > ) | string ,
40+ fnOrOptions ?: ( ( prisma : PrismaTransactionClient ) => Promise < R > ) | PrismaTransactionOptions ,
41+ options ?: PrismaTransactionOptions
2842) : Promise < R | undefined > {
29- return transac (
30- prisma ,
31- fn ,
32- ( error ) => {
33- logger . error ( "prisma.$transaction error" , {
34- code : error . code ,
35- meta : error . meta ,
36- stack : error . stack ,
37- message : error . message ,
38- name : error . name ,
39- } ) ;
40- } ,
41- options
42- ) ;
43+ if ( typeof fnOrName === "string" ) {
44+ return await startActiveSpan ( fnOrName , async ( span ) => {
45+ span . setAttribute ( "$transaction" , true ) ;
46+
47+ if ( options ?. isolationLevel ) {
48+ span . setAttribute ( "isolation_level" , options . isolationLevel ) ;
49+ }
50+
51+ if ( options ?. timeout ) {
52+ span . setAttribute ( "timeout" , options . timeout ) ;
53+ }
54+
55+ if ( options ?. maxWait ) {
56+ span . setAttribute ( "max_wait" , options . maxWait ) ;
57+ }
58+
59+ if ( options ?. swallowPrismaErrors ) {
60+ span . setAttribute ( "swallow_prisma_errors" , options . swallowPrismaErrors ) ;
61+ }
62+
63+ const fn = fnOrOptions as ( prisma : PrismaTransactionClient , span : Span ) => Promise < R > ;
64+
65+ return transac (
66+ prisma ,
67+ ( client ) => fn ( client , span ) ,
68+ ( error ) => {
69+ logger . error ( "prisma.$transaction error" , {
70+ code : error . code ,
71+ meta : error . meta ,
72+ stack : error . stack ,
73+ message : error . message ,
74+ name : error . name ,
75+ } ) ;
76+ } ,
77+ options
78+ ) ;
79+ } ) ;
80+ } else {
81+ return transac (
82+ prisma ,
83+ fnOrName ,
84+ ( error ) => {
85+ logger . error ( "prisma.$transaction error" , {
86+ code : error . code ,
87+ meta : error . meta ,
88+ stack : error . stack ,
89+ message : error . message ,
90+ name : error . name ,
91+ } ) ;
92+ } ,
93+ typeof fnOrOptions === "function" ? undefined : fnOrOptions
94+ ) ;
95+ }
4396}
4497
4598export { Prisma } ;
0 commit comments