| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package otelmongo |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
"fmt" |
| 10 |
"sync" |
| 11 |
|
| 12 |
"go.mongodb.org/mongo-driver/bson" |
| 13 |
"go.mongodb.org/mongo-driver/event" |
| 14 |
"go.opentelemetry.io/otel/codes" |
| 15 |
"go.opentelemetry.io/otel/trace" |
| 16 |
|
| 17 |
"go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo/internal/semconv" |
| 18 |
) |
| 19 |
|
| 20 |
type spanKey struct { |
| 21 |
ConnectionID string |
| 22 |
RequestID int64 |
| 23 |
} |
| 24 |
|
| 25 |
type monitor struct { |
| 26 |
sync.Mutex |
| 27 |
spans map[spanKey]trace.Span |
| 28 |
cfg config |
| 29 |
semconv semconv.EventMonitor |
| 30 |
} |
| 31 |
|
| 32 |
func (m *monitor) Started(ctx context.Context, evt *event.CommandStartedEvent) { |
| 33 |
attrOptions := []semconv.AttributeOption{ |
| 34 |
semconv.WithCommandAttributeDisabled(m.cfg.CommandAttributeDisabled), |
| 35 |
} |
| 36 |
|
| 37 |
var spanName string |
| 38 |
if collection, err := extractCollection(evt); err == nil && collection != "" { |
| 39 |
spanName = collection + "." |
| 40 |
|
| 41 |
attrOptions = append(attrOptions, semconv.WithCollectionName(collection)) |
| 42 |
} |
| 43 |
|
| 44 |
spanName += evt.CommandName |
| 45 |
opts := []trace.SpanStartOption{ |
| 46 |
trace.WithSpanKind(trace.SpanKindClient), |
| 47 |
trace.WithAttributes(m.semconv.CommandStartedTraceAttrs(evt, attrOptions...)...), |
| 48 |
} |
| 49 |
_, span := m.cfg.Tracer.Start(ctx, spanName, opts...) |
| 50 |
key := spanKey{ |
| 51 |
ConnectionID: evt.ConnectionID, |
| 52 |
RequestID: evt.RequestID, |
| 53 |
} |
| 54 |
m.Lock() |
| 55 |
m.spans[key] = span |
| 56 |
m.Unlock() |
| 57 |
} |
| 58 |
|
| 59 |
func (m *monitor) Succeeded(_ context.Context, evt *event.CommandSucceededEvent) { |
| 60 |
m.Finished(&evt.CommandFinishedEvent, nil) |
| 61 |
} |
| 62 |
|
| 63 |
func (m *monitor) Failed(_ context.Context, evt *event.CommandFailedEvent) { |
| 64 |
m.Finished(&evt.CommandFinishedEvent, fmt.Errorf("%s", evt.Failure)) |
| 65 |
} |
| 66 |
|
| 67 |
func (m *monitor) Finished(evt *event.CommandFinishedEvent, err error) { |
| 68 |
key := spanKey{ |
| 69 |
ConnectionID: evt.ConnectionID, |
| 70 |
RequestID: evt.RequestID, |
| 71 |
} |
| 72 |
m.Lock() |
| 73 |
span, ok := m.spans[key] |
| 74 |
if ok { |
| 75 |
delete(m.spans, key) |
| 76 |
} |
| 77 |
m.Unlock() |
| 78 |
if !ok { |
| 79 |
return |
| 80 |
} |
| 81 |
|
| 82 |
if err != nil { |
| 83 |
span.SetStatus(codes.Error, err.Error()) |
| 84 |
} |
| 85 |
|
| 86 |
span.End() |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
func NewMonitor(opts ...Option) *event.CommandMonitor { |
| 91 |
cfg := newConfig(opts...) |
| 92 |
m := &monitor{ |
| 93 |
spans: make(map[spanKey]trace.Span), |
| 94 |
cfg: cfg, |
| 95 |
semconv: semconv.NewEventMonitor(), |
| 96 |
} |
| 97 |
return &event.CommandMonitor{ |
| 98 |
Started: m.Started, |
| 99 |
Succeeded: m.Succeeded, |
| 100 |
Failed: m.Failed, |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
func extractCollection(evt *event.CommandStartedEvent) (string, error) { |
| 109 |
elt, err := evt.Command.IndexErr(0) |
| 110 |
if err != nil { |
| 111 |
return "", err |
| 112 |
} |
| 113 |
if key, err := elt.KeyErr(); err == nil && key == evt.CommandName { |
| 114 |
var v bson.RawValue |
| 115 |
if v, err = elt.ValueErr(); err != nil || v.Type != bson.TypeString { |
| 116 |
return "", err |
| 117 |
} |
| 118 |
return v.StringValue(), nil |
| 119 |
} |
| 120 |
return "", errors.New("collection name not found") |
| 121 |
} |