@@ -35,7 +35,7 @@ type Client interface {
35
35
36
36
// GetWorkspaceID return workspace id
37
37
GetWorkspaceID () string
38
- // Close Close the client. Should be called before program exit.
38
+ // Close close the client. Should be called before program exit.
39
39
Close (ctx context.Context )
40
40
}
41
41
@@ -46,6 +46,7 @@ type HttpClient = httpclient.HTTPClient
46
46
47
47
type options struct {
48
48
apiBaseURL string
49
+ apiBasePath * APIBasePath
49
50
workspaceID string
50
51
httpClient HttpClient
51
52
timeout time.Duration
@@ -62,12 +63,16 @@ type options struct {
62
63
promptCacheRefreshInterval time.Duration
63
64
promptTrace bool
64
65
exporter trace.Exporter
66
+ traceFinishEventProcessor func (ctx context.Context , info * FinishEventInfo )
67
+ traceTagTruncateConf * TagTruncateConf
68
+ traceQueueConf * TraceQueueConf
65
69
}
66
70
67
71
func (o * options ) MD5 () string {
68
72
h := md5 .New ()
69
73
separator := "\t "
70
74
h .Write ([]byte (o .apiBaseURL + separator ))
75
+ h .Write ([]byte (fmt .Sprintf ("%p" , o .apiBasePath ) + separator ))
71
76
h .Write ([]byte (o .workspaceID + separator ))
72
77
h .Write ([]byte (fmt .Sprintf ("%p" , o .httpClient ) + separator ))
73
78
h .Write ([]byte (o .timeout .String () + separator ))
@@ -80,6 +85,10 @@ func (o *options) MD5() string {
80
85
h .Write ([]byte (fmt .Sprintf ("%d" , o .promptCacheMaxCount ) + separator ))
81
86
h .Write ([]byte (o .promptCacheRefreshInterval .String () + separator ))
82
87
h .Write ([]byte (fmt .Sprintf ("%v" , o .promptTrace ) + separator ))
88
+ h .Write ([]byte (fmt .Sprintf ("%p" , o .exporter ) + separator ))
89
+ h .Write ([]byte (fmt .Sprintf ("%p" , o .traceFinishEventProcessor ) + separator ))
90
+ h .Write ([]byte (fmt .Sprintf ("%p" , o .traceTagTruncateConf ) + separator ))
91
+ h .Write ([]byte (fmt .Sprintf ("%p" , o .traceQueueConf ) + separator ))
83
92
return hex .EncodeToString (h .Sum (nil ))
84
93
}
85
94
@@ -133,10 +142,28 @@ func NewClient(opts ...Option) (Client, error) {
133
142
Timeout : options .timeout ,
134
143
UploadTimeout : options .uploadTimeout ,
135
144
})
145
+ traceFinishEventProcessor := trace .DefaultFinishEventProcessor
146
+ if options .traceFinishEventProcessor != nil {
147
+ traceFinishEventProcessor = func (ctx context.Context , info * consts.FinishEventInfo ) {
148
+ trace .DefaultFinishEventProcessor (ctx , info )
149
+ options .traceFinishEventProcessor (ctx , (* FinishEventInfo )(info ))
150
+ }
151
+ }
152
+ var spanUploadPath string
153
+ var fileUploadPath string
154
+ if options .apiBasePath != nil {
155
+ spanUploadPath = options .apiBasePath .TraceSpanUploadPath
156
+ fileUploadPath = options .apiBasePath .TraceFileUploadPath
157
+ }
136
158
c .traceProvider = trace .NewTraceProvider (httpClient , trace.Options {
137
- WorkspaceID : options .workspaceID ,
138
- UltraLargeReport : options .ultraLargeReport ,
139
- Exporter : options .exporter ,
159
+ WorkspaceID : options .workspaceID ,
160
+ UltraLargeReport : options .ultraLargeReport ,
161
+ Exporter : options .exporter ,
162
+ FinishEventProcessor : traceFinishEventProcessor ,
163
+ TagTruncateConf : (* trace .TagTruncateConf )(options .traceTagTruncateConf ),
164
+ SpanUploadPath : spanUploadPath ,
165
+ FileUploadPath : fileUploadPath ,
166
+ QueueConf : (* trace .QueueConf )(options .traceQueueConf ),
140
167
})
141
168
c .promptProvider = prompt .NewPromptProvider (httpClient , c .traceProvider , prompt.Options {
142
169
WorkspaceID : options .workspaceID ,
@@ -185,6 +212,12 @@ func WithAPIBaseURL(apiBaseURL string) Option {
185
212
}
186
213
}
187
214
215
+ func WithAPIBasePath (apiBasePath * APIBasePath ) Option {
216
+ return func (p * options ) {
217
+ p .apiBasePath = apiBasePath
218
+ }
219
+ }
220
+
188
221
// WithWorkspaceID set workspace id.
189
222
func WithWorkspaceID (workspaceID string ) Option {
190
223
return func (p * options ) {
@@ -241,12 +274,33 @@ func WithPromptTrace(enable bool) Option {
241
274
}
242
275
}
243
276
277
+ // WithExporter set custom trace exporter.
244
278
func WithExporter (e trace.Exporter ) Option {
245
279
return func (p * options ) {
246
280
p .exporter = e
247
281
}
248
282
}
249
283
284
+ // WithTraceFinishEventProcessor set custom finish event processor, after span finish.
285
+ func WithTraceFinishEventProcessor (f func (ctx context.Context , info * FinishEventInfo )) Option {
286
+ return func (p * options ) {
287
+ p .traceFinishEventProcessor = f
288
+ }
289
+ }
290
+
291
+ // WithTraceTagTruncateConf set span tag truncate conf.
292
+ func WithTraceTagTruncateConf (conf * TagTruncateConf ) Option {
293
+ return func (p * options ) {
294
+ p .traceTagTruncateConf = conf
295
+ }
296
+ }
297
+
298
+ func WithTraceQueueConf (conf * TraceQueueConf ) Option {
299
+ return func (p * options ) {
300
+ p .traceQueueConf = conf
301
+ }
302
+ }
303
+
250
304
// GetWorkspaceID return space id
251
305
func GetWorkspaceID () string {
252
306
return getDefaultClient ().GetWorkspaceID ()
@@ -350,13 +404,27 @@ func buildAuth(opts options) (httpclient.Auth, error) {
350
404
return nil , ErrAuthInfoRequired
351
405
}
352
406
407
+ func SetDefaultClient (client Client ) {
408
+ defaultClientLock .Lock ()
409
+ defer defaultClientLock .Unlock ()
410
+ defaultClient = client
411
+ }
412
+
353
413
func getDefaultClient () Client {
414
+ if defaultClient != nil {
415
+ return defaultClient
416
+ }
354
417
once .Do (func () {
355
418
var err error
356
- defaultClient , err = NewClient ()
419
+ client , err : = NewClient ()
357
420
if err != nil {
421
+ defaultClientLock .Lock ()
358
422
defaultClient = & NoopClient {newClientError : err }
423
+ defaultClientLock .Unlock ()
359
424
} else {
425
+ defaultClientLock .Lock ()
426
+ defaultClient = client
427
+ defaultClientLock .Unlock ()
360
428
sigChan := make (chan os.Signal , 1 )
361
429
signal .Notify (sigChan , syscall .SIGINT , syscall .SIGTERM )
362
430
go func () {
@@ -366,7 +434,9 @@ func getDefaultClient() Client {
366
434
367
435
logger .CtxInfof (ctx , "Received signal: %v, starting graceful shutdown..." , sig )
368
436
defaultClient .Close (ctx )
437
+ defaultClientLock .Lock ()
369
438
defaultClient = & NoopClient {newClientError : consts .ErrClientClosed }
439
+ defaultClientLock .Unlock ()
370
440
logger .CtxInfof (ctx , "Graceful shutdown finished." )
371
441
os .Exit (0 )
372
442
}()
@@ -376,9 +446,10 @@ func getDefaultClient() Client {
376
446
}
377
447
378
448
var (
379
- defaultClient Client
380
- once sync.Once
381
- clientCache sync.Map // client cache to avoid creating multiple clients with the same options
449
+ defaultClient Client
450
+ defaultClientLock sync.RWMutex
451
+ once sync.Once
452
+ clientCache sync.Map // client cache to avoid creating multiple clients with the same options
382
453
)
383
454
384
455
type loopClient struct {
@@ -426,7 +497,7 @@ func (c *loopClient) PromptFormat(ctx context.Context, loopPrompt *entity.Prompt
426
497
427
498
func (c * loopClient ) StartSpan (ctx context.Context , name , spanType string , opts ... StartSpanOption ) (context.Context , Span ) {
428
499
if c .closed {
429
- return ctx , defaultNoopSpan
500
+ return ctx , DefaultNoopSpan
430
501
}
431
502
config := trace.StartSpanOptions {}
432
503
for _ , opt := range opts {
@@ -438,25 +509,25 @@ func (c *loopClient) StartSpan(ctx context.Context, name, spanType string, opts
438
509
ctx , span , err := c .traceProvider .StartSpan (ctx , name , spanType , config )
439
510
if err != nil {
440
511
logger .CtxWarnf (ctx , "start span failed, return noop span. %v" , err )
441
- return ctx , defaultNoopSpan
512
+ return ctx , DefaultNoopSpan
442
513
}
443
514
return ctx , span
444
515
}
445
516
446
517
func (c * loopClient ) GetSpanFromContext (ctx context.Context ) Span {
447
518
if c .closed {
448
- return defaultNoopSpan
519
+ return DefaultNoopSpan
449
520
}
450
521
span := c .traceProvider .GetSpanFromContext (ctx )
451
522
if span == nil {
452
- return defaultNoopSpan
523
+ return DefaultNoopSpan
453
524
}
454
525
return span
455
526
}
456
527
457
528
func (c * loopClient ) GetSpanFromHeader (ctx context.Context , header map [string ]string ) SpanContext {
458
529
if c .closed {
459
- return defaultNoopSpan
530
+ return DefaultNoopSpan
460
531
}
461
532
return c .traceProvider .GetSpanFromHeader (ctx , header )
462
533
}
0 commit comments