|
| 1 | +//golangcitest:args -Espancheck |
| 2 | +package spancheck |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + |
| 9 | + "go.opentelemetry.io/otel" |
| 10 | + "go.opentelemetry.io/otel/codes" |
| 11 | +) |
| 12 | + |
| 13 | +type testDefaultError struct{} |
| 14 | + |
| 15 | +func (e *testDefaultError) Error() string { |
| 16 | + return "foo" |
| 17 | +} |
| 18 | + |
| 19 | +// incorrect |
| 20 | + |
| 21 | +func _() { |
| 22 | + otel.Tracer("foo").Start(context.Background(), "bar") // want "span is unassigned, probable memory leak" |
| 23 | + ctx, _ := otel.Tracer("foo").Start(context.Background(), "bar") // want "span is unassigned, probable memory leak" |
| 24 | + fmt.Print(ctx) |
| 25 | +} |
| 26 | + |
| 27 | +func _() { |
| 28 | + ctx, span := otel.Tracer("foo").Start(context.Background(), "bar") // want "span.End is not called on all paths, possible memory leak" |
| 29 | + print(ctx.Done(), span.IsRecording()) |
| 30 | +} // want "return can be reached without calling span.End" |
| 31 | + |
| 32 | +func _() { |
| 33 | + var ctx, span = otel.Tracer("foo").Start(context.Background(), "bar") // want "span.End is not called on all paths, possible memory leak" |
| 34 | + print(ctx.Done(), span.IsRecording()) |
| 35 | +} // want "return can be reached without calling span.End" |
| 36 | + |
| 37 | +func _() { |
| 38 | + _, span := otel.Tracer("foo").Start(context.Background(), "bar") // want "span.End is not called on all paths, possible memory leak" |
| 39 | + _, span = otel.Tracer("foo").Start(context.Background(), "bar") |
| 40 | + fmt.Print(span) |
| 41 | + defer span.End() |
| 42 | +} // want "return can be reached without calling span.End" |
| 43 | + |
| 44 | +// correct |
| 45 | + |
| 46 | +func _() error { |
| 47 | + _, span := otel.Tracer("foo").Start(context.Background(), "bar") |
| 48 | + defer span.End() |
| 49 | + |
| 50 | + return nil |
| 51 | +} |
| 52 | + |
| 53 | +func _() error { |
| 54 | + _, span := otel.Tracer("foo").Start(context.Background(), "bar") |
| 55 | + defer span.End() |
| 56 | + |
| 57 | + if true { |
| 58 | + return nil |
| 59 | + } |
| 60 | + |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +func _() error { |
| 65 | + _, span := otel.Tracer("foo").Start(context.Background(), "bar") |
| 66 | + defer span.End() |
| 67 | + |
| 68 | + if false { |
| 69 | + err := errors.New("foo") |
| 70 | + span.SetStatus(codes.Error, err.Error()) |
| 71 | + span.RecordError(err) |
| 72 | + return err |
| 73 | + } |
| 74 | + |
| 75 | + if true { |
| 76 | + span.SetStatus(codes.Error, "foo") |
| 77 | + span.RecordError(errors.New("foo")) |
| 78 | + return errors.New("bar") |
| 79 | + } |
| 80 | + |
| 81 | + return nil |
| 82 | +} |
| 83 | + |
| 84 | +func _() { |
| 85 | + _, span := otel.Tracer("foo").Start(context.Background(), "bar") |
| 86 | + defer span.End() |
| 87 | + |
| 88 | + _, span = otel.Tracer("foo").Start(context.Background(), "bar") |
| 89 | + defer span.End() |
| 90 | +} |
0 commit comments