Skip to content
Merged
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
4 changes: 4 additions & 0 deletions CHANGLOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [0.1.18] - 2026-01-07
### Fixed
- fix tag concurrent problem, forbid set function after finish span

## [0.1.17] - 2025-12-04
### Fixed
- fix baggage and tag concurrent problem
Expand Down
7 changes: 6 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ func NewClient(opts ...Option) (Client, error) {
})

clientCache.Store(cacheKey, c)
defaultClientLock.Lock()
if defaultClient == nil {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加个锁吧

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okk

SetDefaultClient(c)
}
defaultClientLock.Unlock()
return c, nil
}

Expand Down Expand Up @@ -570,4 +575,4 @@ func (c *loopClient) Flush(ctx context.Context) {
return
}
c.traceProvider.Flush(ctx)
}
}
59 changes: 37 additions & 22 deletions internal/trace/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ func (s *Span) GetStatusCode() int32 {
if s == nil {
return 0
}
s.lock.RLock()
defer s.lock.RUnlock()
return s.StatusCode
}

Expand Down Expand Up @@ -284,7 +286,7 @@ func fromHeaderParent(h string) (traceID, spanID string, err error) {
}

func (s *Span) SetInput(ctx context.Context, input interface{}) {
if s == nil {
if s == nil || s.isSpanFinished() {
return
}

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

func (s *Span) SetOutput(ctx context.Context, output interface{}) {
if s == nil {
if s == nil || s.isSpanFinished() {
return
}
mContent := tracespec.ModelOutput{}
Expand Down Expand Up @@ -515,14 +517,14 @@ func getModelOutputBytesSize(mContent tracespec.ModelOutput) int64 {
}

func (s *Span) SetError(ctx context.Context, err error) {
if s == nil {
if s == nil || s.isSpanFinished() {
return
}
s.SetTags(ctx, oneTag(tracespec.Error, err.Error()))
}

func (s *Span) SetStatusCode(ctx context.Context, code int) {
if s == nil {
if s == nil || s.isSpanFinished() {
return
}
s.lock.Lock()
Expand All @@ -531,49 +533,49 @@ func (s *Span) SetStatusCode(ctx context.Context, code int) {
}

func (s *Span) SetUserID(ctx context.Context, userID string) {
if s == nil {
if s == nil || s.isSpanFinished() {
return
}
s.SetTags(ctx, oneTag(consts.UserID, userID))
}

func (s *Span) SetUserIDBaggage(ctx context.Context, userID string) {
if s == nil {
if s == nil || s.isSpanFinished() {
return
}
s.SetBaggage(ctx, oneBaggage(consts.UserID, userID))
}

func (s *Span) SetMessageID(ctx context.Context, messageID string) {
if s == nil {
if s == nil || s.isSpanFinished() {
return
}
s.SetTags(ctx, oneTag(consts.MessageID, messageID))
}

func (s *Span) SetMessageIDBaggage(ctx context.Context, messageID string) {
if s == nil {
if s == nil || s.isSpanFinished() {
return
}
s.SetBaggage(ctx, oneBaggage(consts.MessageID, messageID))
}

func (s *Span) SetThreadID(ctx context.Context, threadID string) {
if s == nil {
if s == nil || s.isSpanFinished() {
return
}
s.SetTags(ctx, oneTag(consts.ThreadID, threadID))
}

func (s *Span) SetThreadIDBaggage(ctx context.Context, threadID string) {
if s == nil {
if s == nil || s.isSpanFinished() {
return
}
s.SetBaggage(ctx, oneBaggage(consts.ThreadID, threadID))
}

func (s *Span) SetPrompt(ctx context.Context, prompt entity.Prompt) {
if s == nil {
if s == nil || s.isSpanFinished() {
return
}
if len(prompt.PromptKey) > 0 {
Expand All @@ -585,49 +587,49 @@ func (s *Span) SetPrompt(ctx context.Context, prompt entity.Prompt) {
}

func (s *Span) SetModelProvider(ctx context.Context, modelProvider string) {
if s == nil {
if s == nil || s.isSpanFinished() {
return
}
s.SetTags(ctx, oneTag(tracespec.ModelProvider, modelProvider))
}

func (s *Span) SetModelName(ctx context.Context, modelName string) {
if s == nil {
if s == nil || s.isSpanFinished() {
return
}
s.SetTags(ctx, oneTag(tracespec.ModelName, modelName))
}

func (s *Span) SetModelCallOptions(ctx context.Context, callOptions interface{}) {
if s == nil {
if s == nil || s.isSpanFinished() {
return
}
s.SetTags(ctx, oneTag(tracespec.CallOptions, callOptions))
}

func (s *Span) SetInputTokens(ctx context.Context, inputTokens int) {
if s == nil {
if s == nil || s.isSpanFinished() {
return
}
s.SetTags(ctx, oneTag(tracespec.InputTokens, inputTokens))
}

func (s *Span) SetOutputTokens(ctx context.Context, outputTokens int) {
if s == nil {
if s == nil || s.isSpanFinished() {
return
}
s.SetTags(ctx, oneTag(tracespec.OutputTokens, outputTokens))
}

func (s *Span) SetStartTimeFirstResp(ctx context.Context, startTimeFirstResp int64) {
if s == nil {
if s == nil || s.isSpanFinished() {
return
}
s.SetTags(ctx, oneTag(consts.StartTimeFirstResp, startTimeFirstResp))
}

func (s *Span) SetTags(ctx context.Context, tagKVs map[string]interface{}) {
if s == nil || len(tagKVs) == 0 {
if s == nil || len(tagKVs) == 0 || s.isSpanFinished() {
return
}

Expand Down Expand Up @@ -758,7 +760,7 @@ func (s *Span) SetMultiModalityMap(key string) {
}

func (s *Span) SetBaggage(ctx context.Context, baggageItems map[string]string) {
if s == nil {
if s == nil || s.isSpanFinished() {
return
}
if len(baggageItems) == 0 {
Expand Down Expand Up @@ -839,6 +841,10 @@ func (s *Span) isDoFinish() bool {
return atomic.CompareAndSwapInt32(&s.isFinished, spanUnFinished, spanFinished)
}

func (s *Span) isSpanFinished() bool {
return atomic.LoadInt32(&s.isFinished) == spanFinished
}

func (s *Span) setSystemTag(ctx context.Context) {
s.lock.Lock()
defer s.lock.Unlock()
Expand Down Expand Up @@ -958,7 +964,7 @@ func (s *Span) toHeaderParent() string {
}

func (s *Span) SetRuntime(ctx context.Context, runtime tracespec.Runtime) {
if s == nil {
if s == nil || s.isSpanFinished() {
return
}
s.lock.Lock()
Expand All @@ -971,12 +977,18 @@ func (s *Span) SetRuntime(ctx context.Context, runtime tracespec.Runtime) {
}

func (s *Span) SetServiceName(ctx context.Context, serviceName string) {
if s == nil || s.isSpanFinished() {
return
}
s.lock.Lock()
defer s.lock.Unlock()
s.ServiceName = serviceName
}

func (s *Span) SetLogID(ctx context.Context, logID string) {
if s == nil || s.isSpanFinished() {
return
}
s.lock.Lock()
defer s.lock.Unlock()
s.LogID = logID
Expand All @@ -989,6 +1001,9 @@ func (s *Span) IsRootSpan() bool {
// SetFinishTime
// Default is time.Now() when span Finish(). DO NOT set unless you do not use default time.
func (s *Span) SetFinishTime(finishTime time.Time) {
if s == nil || s.isSpanFinished() {
return
}
s.lock.Lock()
defer s.lock.Unlock()
s.FinishTime = finishTime
Expand All @@ -1005,7 +1020,7 @@ func (s *Span) GetFinishTime() time.Time {
}

func (s *Span) SetSystemTags(ctx context.Context, systemTags map[string]interface{}) {
if s == nil {
if s == nil || s.isSpanFinished() {
return
}
s.lock.Lock()
Expand All @@ -1016,7 +1031,7 @@ func (s *Span) SetSystemTags(ctx context.Context, systemTags map[string]interfac
}

func (s *Span) SetDeploymentEnv(ctx context.Context, deploymentEnv string) {
if s == nil {
if s == nil || s.isSpanFinished() {
return
}
s.SetTags(ctx, oneTag(consts.DeploymentEnv, deploymentEnv))
Expand Down
2 changes: 1 addition & 1 deletion internal/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ package internal

// Version returns the version of the loop package.
func Version() string {
return "v0.1.17"
return "v0.1.18"
}