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
12 changes: 12 additions & 0 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ func newSpan(ctx context.Context, f *Func, args []interface{}, trace *Trace,
sctx = observer.Start(sctx, s)
}

var finishers []SpanPluginFinisher
if plugins := s.f.scope.r.getSpanPlugins(); plugins != nil {
finishers = make([]SpanPluginFinisher, len(plugins))
for i, plugin := range plugins {
finishers[i] = plugin.Start(s)
}
}

return sctx, func(errptr *error) {
rec := recover()
panicked := rec != nil
Expand Down Expand Up @@ -151,6 +159,10 @@ func newSpan(ctx context.Context, f *Func, args []interface{}, trace *Trace,
observer.Finish(sctx, s, err, panicked, finish)
}

for _, finisher := range finishers {
finisher.Finish(err, panicked, finish)
}

if panicked {
panic(rec)
}
Expand Down
51 changes: 51 additions & 0 deletions plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (C) 2026
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package monkit

import "time"

type SpanPluginFinisher interface {
Finish(err error, panicked bool, done time.Time)
}

type SpanPlugin interface {
Start(span *Span) SpanPluginFinisher
}

func (r *Registry) AddSpanPlugin(h SpanPlugin) {
for {
pluginsPtr := r.plugins.Load()
if pluginsPtr == nil {
r.plugins.CompareAndSwap(nil, new([]SpanPlugin))
continue
}
plugins := *pluginsPtr

nextPlugins := make([]SpanPlugin, len(plugins)+1)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Probably not worth the optimization, but we can reuse the buffer when retrying, rather than allocating a completely new one.

Copy link
Member Author

Choose a reason for hiding this comment

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

concurrent callers could stomp over each other.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I meant reusing the nextPlugins:

func (r *Registry) AddSpanPlugin(h SpanPlugin) {
	var nextPlugins []SpanPlugin
	for {
		pluginsPtr := r.plugins.Load()
		if pluginsPtr == nil {
			r.plugins.CompareAndSwap(nil, new([]SpanPlugin))
			continue
		}
		plugins := *pluginsPtr

		nextPlugins = slices.Grow(nextPlugins[:0], len(plugins)+1)
		copy(nextPlugins, plugins...)
		nextPlugins[len(nextPlugins)-1] = h

		if r.plugins.CompareAndSwap(pluginsPtr, &nextPlugins) {
			return
		}
	}
}

Copy link
Member Author

Choose a reason for hiding this comment

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

oooh yeah that would be fine of course.

copy(nextPlugins, plugins)
nextPlugins[len(plugins)] = h

if r.plugins.CompareAndSwap(pluginsPtr, &nextPlugins) {
return
}
}
}

func (r *Registry) getSpanPlugins() []SpanPlugin {
if pluginsPtr := r.plugins.Load(); pluginsPtr != nil {
return *pluginsPtr
}
return nil
}
3 changes: 3 additions & 0 deletions registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package monkit
import (
"sort"
"sync"
"sync/atomic"
)

type traceWatcherRef struct {
Expand All @@ -39,6 +40,8 @@ type registryInternal struct {

orphanMtx sync.Mutex
orphans map[*Span]struct{}

plugins atomic.Pointer[[]SpanPlugin]
}

// Registry encapsulates all of the top-level state for a monitoring system.
Expand Down
3 changes: 3 additions & 0 deletions span.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ func (s *Span) Children(cb func(s *Span)) {
}
}

// RawArgs returns the arguments given to the Task that created this Span.
func (s *Span) RawArgs() []interface{} { return s.args }

// Args returns the list of strings associated with the args given to the
// Task that created this Span.
func (s *Span) Args() (rv []string) {
Expand Down