Skip to content
This repository was archived by the owner on Jul 19, 2023. It is now read-only.

Commit 8363ed7

Browse files
committedJul 11, 2023
WIP: Write up gopatch
1 parent 3a9306c commit 8363ed7

13 files changed

+217
-158
lines changed
 

‎migrate-to-otel.gopatch

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
@@
2+
var a expression
3+
var b expression
4+
var s identifier
5+
var t identifier
6+
@@
7+
-s, t := opentracing.StartSpanFromContext(a,b)
8+
-...
9+
- defer s.Finish()
10+
+import "go.opentelemetry.io/otel"
11+
+t, s := otel.Tracer("github.com/grafana/pyroscope").Start(a,b)
12+
+defer s.End()
13+
14+
@@
15+
var foo,x identifier
16+
@@
17+
18+
-import foo "github.com/opentracing/opentracing-go/log"
19+
+import foo "go.opentelemetry.io/otel/attribute"
20+
foo.x
21+
22+
@@
23+
@@
24+
- otlog
25+
+ attribute
26+
27+
@@
28+
var span identifier
29+
var x expression
30+
@@
31+
- span.LogFields(...)
32+
+import "go.opentelemetry.io/otel/trace"
33+
+ span.AddEvent("TODO", trace.WithAttributes(...))
34+
35+
36+
@@
37+
@@
38+
-opentracing.Span
39+
+import "go.opentelemetry.io/otel/trace"
40+
+trace.Span

‎pkg/phlaredb/block_querier.go

+33-33
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ import (
2121
"github.com/grafana/dskit/runutil"
2222
"github.com/oklog/ulid"
2323
"github.com/opentracing/opentracing-go"
24-
otlog "github.com/opentracing/opentracing-go/log"
2524
"github.com/pkg/errors"
2625
"github.com/prometheus/common/model"
2726
"github.com/prometheus/prometheus/promql/parser"
2827
"github.com/samber/lo"
2928
"github.com/segmentio/parquet-go"
3029
"github.com/thanos-io/objstore"
30+
"go.opentelemetry.io/otel"
31+
attribute "go.opentelemetry.io/otel/attribute"
32+
"go.opentelemetry.io/otel/trace"
3133
"golang.org/x/sync/errgroup"
3234
"google.golang.org/grpc/codes"
3335

@@ -557,8 +559,8 @@ func SelectMatchingProfiles(ctx context.Context, request *ingestv1.SelectProfile
557559
}
558560

559561
func MergeProfilesStacktraces(ctx context.Context, stream *connect.BidiStream[ingestv1.MergeProfilesStacktracesRequest, ingestv1.MergeProfilesStacktracesResponse], blockGetter BlockGetter) error {
560-
sp, ctx := opentracing.StartSpanFromContext(ctx, "MergeProfilesStacktraces")
561-
defer sp.Finish()
562+
ctx, sp := otel.Tracer("github.com/grafana/pyroscope").Start(ctx, "MergeProfilesStacktraces")
563+
defer sp.End()
562564

563565
r, err := stream.Receive()
564566
if err != nil {
@@ -572,12 +574,11 @@ func MergeProfilesStacktraces(ctx context.Context, stream *connect.BidiStream[in
572574
return connect.NewError(connect.CodeInvalidArgument, errors.New("missing initial select request"))
573575
}
574576
request := r.Request
575-
sp.LogFields(
576-
otlog.String("start", model.Time(request.Start).Time().String()),
577-
otlog.String("end", model.Time(request.End).Time().String()),
578-
otlog.String("selector", request.LabelSelector),
579-
otlog.String("profile_id", request.Type.ID),
580-
)
577+
sp.AddEvent("TODO", trace.WithAttributes(
578+
attribute.String("start", model.Time(request.Start).Time().String()),
579+
attribute.String("end", model.Time(request.End).Time().String()),
580+
attribute.String("selector", request.LabelSelector),
581+
attribute.String("profile_id", request.Type.ID)))
581582

582583
queriers, err := blockGetter(ctx, model.Time(request.Start), model.Time(request.End))
583584
if err != nil {
@@ -621,7 +622,7 @@ func MergeProfilesStacktraces(ctx context.Context, stream *connect.BidiStream[in
621622

622623
// Signals the end of the profile streaming by sending an empty response.
623624
// This allows the client to not block other streaming ingesters.
624-
sp.LogFields(otlog.String("msg", "signaling the end of the profile streaming"))
625+
sp.AddEvent("TODO", trace.WithAttributes(attribute.String("msg", "signaling the end of the profile streaming")))
625626
if err = stream.Send(&ingestv1.MergeProfilesStacktracesResponse{}); err != nil {
626627
return err
627628
}
@@ -631,7 +632,7 @@ func MergeProfilesStacktraces(ctx context.Context, stream *connect.BidiStream[in
631632
}
632633

633634
// sends the final result to the client.
634-
sp.LogFields(otlog.String("msg", "sending the final result to the client"))
635+
sp.AddEvent("TODO", trace.WithAttributes(attribute.String("msg", "sending the final result to the client")))
635636
err = stream.Send(&ingestv1.MergeProfilesStacktracesResponse{
636637
Result: &ingestv1.MergeProfilesStacktracesResult{
637638
Format: ingestv1.StacktracesMergeFormat_MERGE_FORMAT_TREE,
@@ -649,8 +650,8 @@ func MergeProfilesStacktraces(ctx context.Context, stream *connect.BidiStream[in
649650
}
650651

651652
func MergeProfilesLabels(ctx context.Context, stream *connect.BidiStream[ingestv1.MergeProfilesLabelsRequest, ingestv1.MergeProfilesLabelsResponse], blockGetter BlockGetter) error {
652-
sp, ctx := opentracing.StartSpanFromContext(ctx, "MergeProfilesLabels")
653-
defer sp.Finish()
653+
ctx, sp := otel.Tracer("github.com/grafana/pyroscope").Start(ctx, "MergeProfilesLabels")
654+
defer sp.End()
654655

655656
r, err := stream.Receive()
656657
if err != nil {
@@ -666,13 +667,12 @@ func MergeProfilesLabels(ctx context.Context, stream *connect.BidiStream[ingestv
666667
request := r.Request
667668
by := r.By
668669
sort.Strings(by)
669-
sp.LogFields(
670-
otlog.String("start", model.Time(request.Start).Time().String()),
671-
otlog.String("end", model.Time(request.End).Time().String()),
672-
otlog.String("selector", request.LabelSelector),
673-
otlog.String("profile_id", request.Type.ID),
674-
otlog.String("by", strings.Join(by, ",")),
675-
)
670+
sp.AddEvent("TODO", trace.WithAttributes(
671+
attribute.String("start", model.Time(request.Start).Time().String()),
672+
attribute.String("end", model.Time(request.End).Time().String()),
673+
attribute.String("selector", request.LabelSelector),
674+
attribute.String("profile_id", request.Type.ID),
675+
attribute.String("by", strings.Join(by, ","))))
676676

677677
queriers, err := blockGetter(ctx, model.Time(request.Start), model.Time(request.End))
678678
if err != nil {
@@ -743,8 +743,8 @@ func MergeProfilesLabels(ctx context.Context, stream *connect.BidiStream[ingestv
743743
}
744744

745745
func MergeProfilesPprof(ctx context.Context, stream *connect.BidiStream[ingestv1.MergeProfilesPprofRequest, ingestv1.MergeProfilesPprofResponse], blockGetter BlockGetter) error {
746-
sp, ctx := opentracing.StartSpanFromContext(ctx, "MergeProfilesPprof")
747-
defer sp.Finish()
746+
ctx, sp := otel.Tracer("github.com/grafana/pyroscope").Start(ctx, "MergeProfilesPprof")
747+
defer sp.End()
748748

749749
r, err := stream.Receive()
750750
if err != nil {
@@ -758,12 +758,11 @@ func MergeProfilesPprof(ctx context.Context, stream *connect.BidiStream[ingestv1
758758
return connect.NewError(connect.CodeInvalidArgument, errors.New("missing initial select request"))
759759
}
760760
request := r.Request
761-
sp.LogFields(
762-
otlog.String("start", model.Time(request.Start).Time().String()),
763-
otlog.String("end", model.Time(request.End).Time().String()),
764-
otlog.String("selector", request.LabelSelector),
765-
otlog.String("profile_id", request.Type.ID),
766-
)
761+
sp.AddEvent("TODO", trace.WithAttributes(
762+
attribute.String("start", model.Time(request.Start).Time().String()),
763+
attribute.String("end", model.Time(request.End).Time().String()),
764+
attribute.String("selector", request.LabelSelector),
765+
attribute.String("profile_id", request.Type.ID)))
767766

768767
queriers, err := blockGetter(ctx, model.Time(request.Start), model.Time(request.End))
769768
if err != nil {
@@ -889,8 +888,9 @@ func retrieveStacktracePartition(buf [][]parquet.Value, pos int) uint64 {
889888
}
890889

891890
func (b *singleBlockQuerier) SelectMatchingProfiles(ctx context.Context, params *ingestv1.SelectProfilesRequest) (iter.Iterator[Profile], error) {
892-
sp, ctx := opentracing.StartSpanFromContext(ctx, "SelectMatchingProfiles - Block")
893-
defer sp.Finish()
891+
ctx, sp := otel.Tracer("github.com/grafana/pyroscope").Start(ctx, "SelectMatchingProfiles - Block")
892+
defer sp.End()
893+
894894
if err := b.Open(ctx); err != nil {
895895
return nil, err
896896
}
@@ -1045,9 +1045,9 @@ func (q *singleBlockQuerier) openFiles(ctx context.Context) error {
10451045
sp, ctx := opentracing.StartSpanFromContext(ctx, "BlockQuerier - open")
10461046
defer func() {
10471047
q.metrics.blockOpeningLatency.Observe(time.Since(start).Seconds())
1048-
sp.LogFields(
1049-
otlog.String("block_ulid", q.meta.ULID.String()),
1050-
)
1048+
sp.AddEvent("TODO", trace.WithAttributes(
1049+
attribute.String("block_ulid", q.meta.ULID.String())))
1050+
10511051
sp.Finish()
10521052
}()
10531053
g, ctx := errgroup.WithContext(ctx)

‎pkg/phlaredb/head.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ import (
1717
"github.com/google/pprof/profile"
1818
"github.com/google/uuid"
1919
"github.com/opentracing/opentracing-go"
20-
otlog "github.com/opentracing/opentracing-go/log"
2120
"github.com/pkg/errors"
2221
"github.com/prometheus/common/model"
2322
"github.com/prometheus/prometheus/model/labels"
2423
"github.com/prometheus/prometheus/promql/parser"
2524
"github.com/prometheus/prometheus/tsdb/fileutil"
2625
"github.com/samber/lo"
26+
"go.opentelemetry.io/otel"
27+
attribute "go.opentelemetry.io/otel/attribute"
28+
"go.opentelemetry.io/otel/trace"
2729
"go.uber.org/atomic"
2830
"google.golang.org/grpc/codes"
2931

@@ -533,8 +535,8 @@ func (h *Head) Queriers() Queriers {
533535

534536
// add the location IDs to the stacktraces
535537
func (h *Head) resolveStacktraces(ctx context.Context, stacktracesByMapping stacktracesByMapping) *ingestv1.MergeProfilesStacktracesResult {
536-
sp, _ := opentracing.StartSpanFromContext(ctx, "resolveStacktraces - Head")
537-
defer sp.Finish()
538+
_, sp := otel.Tracer("github.com/grafana/pyroscope").Start(ctx, "resolveStacktraces - Head")
539+
defer sp.End()
538540

539541
names := []string{}
540542
functions := map[int64]int{}
@@ -548,7 +550,7 @@ func (h *Head) resolveStacktraces(ctx context.Context, stacktracesByMapping stac
548550
h.strings.lock.RUnlock()
549551
}()
550552

551-
sp.LogFields(otlog.String("msg", "building MergeProfilesStacktracesResult"))
553+
sp.AddEvent("TODO", trace.WithAttributes(attribute.String("msg", "building MergeProfilesStacktracesResult")))
552554
_ = stacktracesByMapping.ForEach(
553555
func(mapping uint64, stacktraceSamples stacktraceSampleMap) error {
554556
mp, ok := h.symbolDB.MappingReader(mapping)
@@ -595,8 +597,8 @@ func (h *Head) resolveStacktraces(ctx context.Context, stacktracesByMapping stac
595597
}
596598

597599
func (h *Head) resolvePprof(ctx context.Context, stacktracesByMapping profileSampleByMapping) *profile.Profile {
598-
sp, _ := opentracing.StartSpanFromContext(ctx, "resolvePprof - Head")
599-
defer sp.Finish()
600+
_, sp := otel.Tracer("github.com/grafana/pyroscope").Start(ctx, "resolvePprof - Head")
601+
defer sp.End()
600602

601603
locations := map[int32]*profile.Location{}
602604
functions := map[uint64]*profile.Function{}

‎pkg/phlaredb/head_queriers.go

+17-16
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/pkg/errors"
1111
"github.com/prometheus/common/model"
1212
"github.com/segmentio/parquet-go"
13+
"go.opentelemetry.io/otel"
1314

1415
ingestv1 "github.com/grafana/phlare/api/gen/proto/go/ingester/v1"
1516
typesv1 "github.com/grafana/phlare/api/gen/proto/go/types/v1"
@@ -34,8 +35,8 @@ func (q *headOnDiskQuerier) Open(_ context.Context) error {
3435
}
3536

3637
func (q *headOnDiskQuerier) SelectMatchingProfiles(ctx context.Context, params *ingestv1.SelectProfilesRequest) (iter.Iterator[Profile], error) {
37-
sp, ctx := opentracing.StartSpanFromContext(ctx, "SelectMatchingProfiles - HeadOnDisk")
38-
defer sp.Finish()
38+
ctx, sp := otel.Tracer("github.com/grafana/pyroscope").Start(ctx, "SelectMatchingProfiles - HeadOnDisk")
39+
defer sp.End()
3940

4041
// query the index for rows
4142
rowIter, labelsPerFP, err := q.head.profiles.index.selectMatchingRowRanges(ctx, params, q.rowGroupIdx)
@@ -106,8 +107,8 @@ func (q *headOnDiskQuerier) Bounds() (model.Time, model.Time) {
106107
}
107108

108109
func (q *headOnDiskQuerier) MergeByStacktraces(ctx context.Context, rows iter.Iterator[Profile]) (*ingestv1.MergeProfilesStacktracesResult, error) {
109-
sp, ctx := opentracing.StartSpanFromContext(ctx, "MergeByStacktraces - HeadOnDisk")
110-
defer sp.Finish()
110+
ctx, sp := otel.Tracer("github.com/grafana/pyroscope").Start(ctx, "MergeByStacktraces - HeadOnDisk")
111+
defer sp.End()
111112

112113
stacktraceSamples := stacktracesByMapping{}
113114

@@ -120,8 +121,8 @@ func (q *headOnDiskQuerier) MergeByStacktraces(ctx context.Context, rows iter.It
120121
}
121122

122123
func (q *headOnDiskQuerier) MergePprof(ctx context.Context, rows iter.Iterator[Profile]) (*profile.Profile, error) {
123-
sp, ctx := opentracing.StartSpanFromContext(ctx, "MergeByPprof - HeadOnDisk")
124-
defer sp.Finish()
124+
ctx, sp := otel.Tracer("github.com/grafana/pyroscope").Start(ctx, "MergeByPprof - HeadOnDisk")
125+
defer sp.End()
125126

126127
stacktraceSamples := profileSampleByMapping{}
127128

@@ -133,8 +134,8 @@ func (q *headOnDiskQuerier) MergePprof(ctx context.Context, rows iter.Iterator[P
133134
}
134135

135136
func (q *headOnDiskQuerier) MergeByLabels(ctx context.Context, rows iter.Iterator[Profile], by ...string) ([]*typesv1.Series, error) {
136-
sp, ctx := opentracing.StartSpanFromContext(ctx, "MergeByLabels - HeadOnDisk")
137-
defer sp.Finish()
137+
ctx, sp := otel.Tracer("github.com/grafana/pyroscope").Start(ctx, "MergeByLabels - HeadOnDisk")
138+
defer sp.End()
138139

139140
seriesByLabels := make(seriesByLabels)
140141

@@ -168,8 +169,8 @@ func (q *headInMemoryQuerier) Open(_ context.Context) error {
168169
}
169170

170171
func (q *headInMemoryQuerier) SelectMatchingProfiles(ctx context.Context, params *ingestv1.SelectProfilesRequest) (iter.Iterator[Profile], error) {
171-
sp, ctx := opentracing.StartSpanFromContext(ctx, "SelectMatchingProfiles - HeadInMemory")
172-
defer sp.Finish()
172+
ctx, sp := otel.Tracer("github.com/grafana/pyroscope").Start(ctx, "SelectMatchingProfiles - HeadInMemory")
173+
defer sp.End()
173174

174175
index := q.head.profiles.index
175176

@@ -215,8 +216,8 @@ func (q *headInMemoryQuerier) Bounds() (model.Time, model.Time) {
215216
}
216217

217218
func (q *headInMemoryQuerier) MergeByStacktraces(ctx context.Context, rows iter.Iterator[Profile]) (*ingestv1.MergeProfilesStacktracesResult, error) {
218-
sp, _ := opentracing.StartSpanFromContext(ctx, "MergeByStacktraces - HeadInMemory")
219-
defer sp.Finish()
219+
_, sp := otel.Tracer("github.com/grafana/pyroscope").Start(ctx, "MergeByStacktraces - HeadInMemory")
220+
defer sp.End()
220221

221222
stacktraceSamples := stacktracesByMapping{}
222223

@@ -243,8 +244,8 @@ func (q *headInMemoryQuerier) MergeByStacktraces(ctx context.Context, rows iter.
243244
}
244245

245246
func (q *headInMemoryQuerier) MergePprof(ctx context.Context, rows iter.Iterator[Profile]) (*profile.Profile, error) {
246-
sp, _ := opentracing.StartSpanFromContext(ctx, "MergePprof - HeadInMemory")
247-
defer sp.Finish()
247+
_, sp := otel.Tracer("github.com/grafana/pyroscope").Start(ctx, "MergePprof - HeadInMemory")
248+
defer sp.End()
248249

249250
stacktraceSamples := profileSampleByMapping{}
250251

@@ -267,8 +268,8 @@ func (q *headInMemoryQuerier) MergePprof(ctx context.Context, rows iter.Iterator
267268
}
268269

269270
func (q *headInMemoryQuerier) MergeByLabels(ctx context.Context, rows iter.Iterator[Profile], by ...string) ([]*typesv1.Series, error) {
270-
sp, _ := opentracing.StartSpanFromContext(ctx, "MergeByLabels - HeadInMemory")
271-
defer sp.Finish()
271+
_, sp := otel.Tracer("github.com/grafana/pyroscope").Start(ctx, "MergeByLabels - HeadInMemory")
272+
defer sp.End()
272273

273274
labelsByFingerprint := map[model.Fingerprint]string{}
274275
seriesByLabels := make(seriesByLabels)

‎pkg/phlaredb/phlaredb.go

+9-11
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ import (
1818
"github.com/grafana/dskit/services"
1919
"github.com/oklog/ulid"
2020
"github.com/opentracing/opentracing-go"
21-
otlog "github.com/opentracing/opentracing-go/log"
2221
"github.com/pkg/errors"
2322
"github.com/prometheus/common/model"
23+
"go.opentelemetry.io/otel"
24+
attribute "go.opentelemetry.io/otel/attribute"
25+
"go.opentelemetry.io/otel/trace"
2426

2527
profilev1 "github.com/grafana/phlare/api/gen/proto/go/google/v1"
2628
ingestv1 "github.com/grafana/phlare/api/gen/proto/go/ingester/v1"
@@ -380,12 +382,8 @@ func filterProfiles[B BidiServerMerge[Res, Req],
380382
Profile: maxBlockProfile,
381383
Index: 0,
382384
}, true, its...), batchProfileSize, func(ctx context.Context, batch []ProfileWithIndex) error {
383-
sp, _ := opentracing.StartSpanFromContext(ctx, "filterProfiles - Filtering batch")
384-
sp.LogFields(
385-
otlog.Int("batch_len", len(batch)),
386-
otlog.Int("batch_requested_size", batchProfileSize),
387-
)
388-
defer sp.Finish()
385+
_, sp := otel.Tracer("github.com/grafana/pyroscope").Start(ctx, "filterProfiles - Filtering batch")
386+
defer sp.End()
389387

390388
seriesByFP := map[model.Fingerprint]labelWithIndex{}
391389
selectProfileResult.LabelsSets = selectProfileResult.LabelsSets[:0]
@@ -409,7 +407,7 @@ func filterProfiles[B BidiServerMerge[Res, Req],
409407
})
410408

411409
}
412-
sp.LogFields(otlog.String("msg", "sending batch to client"))
410+
sp.AddEvent("TODO", trace.WithAttributes(attribute.String("msg", "sending batch to client")))
413411
var err error
414412
switch s := BidiServerMerge[Res, Req](stream).(type) {
415413
case BidiServerMerge[*ingestv1.MergeProfilesStacktracesResponse, *ingestv1.MergeProfilesStacktracesRequest]:
@@ -433,9 +431,9 @@ func filterProfiles[B BidiServerMerge[Res, Req],
433431
}
434432
return err
435433
}
436-
sp.LogFields(otlog.String("msg", "batch sent to client"))
434+
sp.AddEvent("TODO", trace.WithAttributes(attribute.String("msg", "batch sent to client")))
437435

438-
sp.LogFields(otlog.String("msg", "reading selection from client"))
436+
sp.AddEvent("TODO", trace.WithAttributes(attribute.String("msg", "reading selection from client")))
439437

440438
// handle response for the batch.
441439
var selected []bool
@@ -462,7 +460,7 @@ func filterProfiles[B BidiServerMerge[Res, Req],
462460
}
463461
return err
464462
}
465-
sp.LogFields(otlog.String("msg", "selection received"))
463+
sp.AddEvent("TODO", trace.WithAttributes(attribute.String("msg", "selection received")))
466464
for i, k := range selected {
467465
if k {
468466
selection[batch[i].Index] = append(selection[batch[i].Index], batch[i].Profile)

0 commit comments

Comments
 (0)
Failed to load comments.