Skip to content

Commit 4dee5b6

Browse files
authored
[fix] span set concurrent problem after finish (#34)
* fix concurrent * NewClient set default client if not exist
1 parent c52966a commit 4dee5b6

4 files changed

Lines changed: 48 additions & 24 deletions

File tree

CHANGLOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## [0.1.18] - 2026-01-07
2+
### Fixed
3+
- fix tag concurrent problem, forbid set function after finish span
4+
15
## [0.1.17] - 2025-12-04
26
### Fixed
37
- fix baggage and tag concurrent problem

client.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ func NewClient(opts ...Option) (Client, error) {
174174
})
175175

176176
clientCache.Store(cacheKey, c)
177+
defaultClientLock.Lock()
178+
if defaultClient == nil {
179+
SetDefaultClient(c)
180+
}
181+
defaultClientLock.Unlock()
177182
return c, nil
178183
}
179184

@@ -570,4 +575,4 @@ func (c *loopClient) Flush(ctx context.Context) {
570575
return
571576
}
572577
c.traceProvider.Flush(ctx)
573-
}
578+
}

internal/trace/span.go

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ func (s *Span) GetStatusCode() int32 {
176176
if s == nil {
177177
return 0
178178
}
179+
s.lock.RLock()
180+
defer s.lock.RUnlock()
179181
return s.StatusCode
180182
}
181183

@@ -284,7 +286,7 @@ func fromHeaderParent(h string) (traceID, spanID string, err error) {
284286
}
285287

286288
func (s *Span) SetInput(ctx context.Context, input interface{}) {
287-
if s == nil {
289+
if s == nil || s.isSpanFinished() {
288290
return
289291
}
290292

@@ -414,7 +416,7 @@ func parseModelMessageParts(mContents []*tracespec.ModelMessagePart) (isMultiMod
414416
}
415417

416418
func (s *Span) SetOutput(ctx context.Context, output interface{}) {
417-
if s == nil {
419+
if s == nil || s.isSpanFinished() {
418420
return
419421
}
420422
mContent := tracespec.ModelOutput{}
@@ -515,14 +517,14 @@ func getModelOutputBytesSize(mContent tracespec.ModelOutput) int64 {
515517
}
516518

517519
func (s *Span) SetError(ctx context.Context, err error) {
518-
if s == nil {
520+
if s == nil || s.isSpanFinished() {
519521
return
520522
}
521523
s.SetTags(ctx, oneTag(tracespec.Error, err.Error()))
522524
}
523525

524526
func (s *Span) SetStatusCode(ctx context.Context, code int) {
525-
if s == nil {
527+
if s == nil || s.isSpanFinished() {
526528
return
527529
}
528530
s.lock.Lock()
@@ -531,49 +533,49 @@ func (s *Span) SetStatusCode(ctx context.Context, code int) {
531533
}
532534

533535
func (s *Span) SetUserID(ctx context.Context, userID string) {
534-
if s == nil {
536+
if s == nil || s.isSpanFinished() {
535537
return
536538
}
537539
s.SetTags(ctx, oneTag(consts.UserID, userID))
538540
}
539541

540542
func (s *Span) SetUserIDBaggage(ctx context.Context, userID string) {
541-
if s == nil {
543+
if s == nil || s.isSpanFinished() {
542544
return
543545
}
544546
s.SetBaggage(ctx, oneBaggage(consts.UserID, userID))
545547
}
546548

547549
func (s *Span) SetMessageID(ctx context.Context, messageID string) {
548-
if s == nil {
550+
if s == nil || s.isSpanFinished() {
549551
return
550552
}
551553
s.SetTags(ctx, oneTag(consts.MessageID, messageID))
552554
}
553555

554556
func (s *Span) SetMessageIDBaggage(ctx context.Context, messageID string) {
555-
if s == nil {
557+
if s == nil || s.isSpanFinished() {
556558
return
557559
}
558560
s.SetBaggage(ctx, oneBaggage(consts.MessageID, messageID))
559561
}
560562

561563
func (s *Span) SetThreadID(ctx context.Context, threadID string) {
562-
if s == nil {
564+
if s == nil || s.isSpanFinished() {
563565
return
564566
}
565567
s.SetTags(ctx, oneTag(consts.ThreadID, threadID))
566568
}
567569

568570
func (s *Span) SetThreadIDBaggage(ctx context.Context, threadID string) {
569-
if s == nil {
571+
if s == nil || s.isSpanFinished() {
570572
return
571573
}
572574
s.SetBaggage(ctx, oneBaggage(consts.ThreadID, threadID))
573575
}
574576

575577
func (s *Span) SetPrompt(ctx context.Context, prompt entity.Prompt) {
576-
if s == nil {
578+
if s == nil || s.isSpanFinished() {
577579
return
578580
}
579581
if len(prompt.PromptKey) > 0 {
@@ -585,49 +587,49 @@ func (s *Span) SetPrompt(ctx context.Context, prompt entity.Prompt) {
585587
}
586588

587589
func (s *Span) SetModelProvider(ctx context.Context, modelProvider string) {
588-
if s == nil {
590+
if s == nil || s.isSpanFinished() {
589591
return
590592
}
591593
s.SetTags(ctx, oneTag(tracespec.ModelProvider, modelProvider))
592594
}
593595

594596
func (s *Span) SetModelName(ctx context.Context, modelName string) {
595-
if s == nil {
597+
if s == nil || s.isSpanFinished() {
596598
return
597599
}
598600
s.SetTags(ctx, oneTag(tracespec.ModelName, modelName))
599601
}
600602

601603
func (s *Span) SetModelCallOptions(ctx context.Context, callOptions interface{}) {
602-
if s == nil {
604+
if s == nil || s.isSpanFinished() {
603605
return
604606
}
605607
s.SetTags(ctx, oneTag(tracespec.CallOptions, callOptions))
606608
}
607609

608610
func (s *Span) SetInputTokens(ctx context.Context, inputTokens int) {
609-
if s == nil {
611+
if s == nil || s.isSpanFinished() {
610612
return
611613
}
612614
s.SetTags(ctx, oneTag(tracespec.InputTokens, inputTokens))
613615
}
614616

615617
func (s *Span) SetOutputTokens(ctx context.Context, outputTokens int) {
616-
if s == nil {
618+
if s == nil || s.isSpanFinished() {
617619
return
618620
}
619621
s.SetTags(ctx, oneTag(tracespec.OutputTokens, outputTokens))
620622
}
621623

622624
func (s *Span) SetStartTimeFirstResp(ctx context.Context, startTimeFirstResp int64) {
623-
if s == nil {
625+
if s == nil || s.isSpanFinished() {
624626
return
625627
}
626628
s.SetTags(ctx, oneTag(consts.StartTimeFirstResp, startTimeFirstResp))
627629
}
628630

629631
func (s *Span) SetTags(ctx context.Context, tagKVs map[string]interface{}) {
630-
if s == nil || len(tagKVs) == 0 {
632+
if s == nil || len(tagKVs) == 0 || s.isSpanFinished() {
631633
return
632634
}
633635

@@ -758,7 +760,7 @@ func (s *Span) SetMultiModalityMap(key string) {
758760
}
759761

760762
func (s *Span) SetBaggage(ctx context.Context, baggageItems map[string]string) {
761-
if s == nil {
763+
if s == nil || s.isSpanFinished() {
762764
return
763765
}
764766
if len(baggageItems) == 0 {
@@ -839,6 +841,10 @@ func (s *Span) isDoFinish() bool {
839841
return atomic.CompareAndSwapInt32(&s.isFinished, spanUnFinished, spanFinished)
840842
}
841843

844+
func (s *Span) isSpanFinished() bool {
845+
return atomic.LoadInt32(&s.isFinished) == spanFinished
846+
}
847+
842848
func (s *Span) setSystemTag(ctx context.Context) {
843849
s.lock.Lock()
844850
defer s.lock.Unlock()
@@ -958,7 +964,7 @@ func (s *Span) toHeaderParent() string {
958964
}
959965

960966
func (s *Span) SetRuntime(ctx context.Context, runtime tracespec.Runtime) {
961-
if s == nil {
967+
if s == nil || s.isSpanFinished() {
962968
return
963969
}
964970
s.lock.Lock()
@@ -971,12 +977,18 @@ func (s *Span) SetRuntime(ctx context.Context, runtime tracespec.Runtime) {
971977
}
972978

973979
func (s *Span) SetServiceName(ctx context.Context, serviceName string) {
980+
if s == nil || s.isSpanFinished() {
981+
return
982+
}
974983
s.lock.Lock()
975984
defer s.lock.Unlock()
976985
s.ServiceName = serviceName
977986
}
978987

979988
func (s *Span) SetLogID(ctx context.Context, logID string) {
989+
if s == nil || s.isSpanFinished() {
990+
return
991+
}
980992
s.lock.Lock()
981993
defer s.lock.Unlock()
982994
s.LogID = logID
@@ -989,6 +1001,9 @@ func (s *Span) IsRootSpan() bool {
9891001
// SetFinishTime
9901002
// Default is time.Now() when span Finish(). DO NOT set unless you do not use default time.
9911003
func (s *Span) SetFinishTime(finishTime time.Time) {
1004+
if s == nil || s.isSpanFinished() {
1005+
return
1006+
}
9921007
s.lock.Lock()
9931008
defer s.lock.Unlock()
9941009
s.FinishTime = finishTime
@@ -1005,7 +1020,7 @@ func (s *Span) GetFinishTime() time.Time {
10051020
}
10061021

10071022
func (s *Span) SetSystemTags(ctx context.Context, systemTags map[string]interface{}) {
1008-
if s == nil {
1023+
if s == nil || s.isSpanFinished() {
10091024
return
10101025
}
10111026
s.lock.Lock()
@@ -1016,7 +1031,7 @@ func (s *Span) SetSystemTags(ctx context.Context, systemTags map[string]interfac
10161031
}
10171032

10181033
func (s *Span) SetDeploymentEnv(ctx context.Context, deploymentEnv string) {
1019-
if s == nil {
1034+
if s == nil || s.isSpanFinished() {
10201035
return
10211036
}
10221037
s.SetTags(ctx, oneTag(consts.DeploymentEnv, deploymentEnv))

internal/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ package internal
55

66
// Version returns the version of the loop package.
77
func Version() string {
8-
return "v0.1.17"
8+
return "v0.1.18"
99
}

0 commit comments

Comments
 (0)