Skip to content

Commit f224173

Browse files
committed
Allign database/sql instrumentation to the latest changes from main branch
1 parent 5226b9a commit f224173

File tree

6 files changed

+92
-32
lines changed

6 files changed

+92
-32
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ OpenTelemetry Go Automatic Instrumentation adheres to [Semantic Versioning](http
88

99
## [Unreleased]
1010

11+
### Added
12+
- Add database/sql instrumentation ([#240](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/240))
13+
1114
### Changed
1215

1316
- The function signature of `"go.opentelemetry.io/auto/offsets-tracker/downloader".DownloadBinary` has changed.
@@ -64,7 +67,6 @@ OpenTelemetry Go Automatic Instrumentation adheres to [Semantic Versioning](http
6467
- Add ARM64 support. ([#82](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/82))
6568
- Add `OTEL_GO_AUTO_SHOW_VERIFIER_LOG` environment variable to control whether
6669
the verifier log is shown. ([#128](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/128))
67-
- Add database/sql instrumentation ([#240](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/240))
6870

6971
### Changed
7072

pkg/instrumentors/bpf/database/sql/bpf/probe.bpf.c

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
115
#include "arguments.h"
216
#include "span_context.h"
317
#include "go_context.h"
418
#include "go_types.h"
19+
#include "uprobe.h"
520

621
char __license[] SEC("license") = "Dual MIT/GPL";
722

823
#define MAX_QUERY_SIZE 100
924
#define MAX_CONCURRENT 50
1025

1126
struct sql_request_t {
12-
u64 start_time;
13-
u64 end_time;
27+
BASE_SPAN_PROPERTIES
1428
char query[MAX_QUERY_SIZE];
15-
struct span_context sc;
16-
struct span_context psc;
1729
};
1830

1931
struct {
@@ -74,22 +86,4 @@ int uprobe_queryDC(struct pt_regs *ctx) {
7486

7587
// This instrumentation attaches uprobe to the following function:
7688
// func (db *DB) queryDC(ctx, txctx context.Context, dc *driverConn, releaseConn func(error), query string, args []any)
77-
SEC("uprobe/queryDC")
78-
int uprobe_queryDC_Returns(struct pt_regs *ctx) {
79-
u64 context_ptr_pos = 3;
80-
// Find the corresponding sql event we return from
81-
void *context_ptr = get_argument(ctx, context_ptr_pos);
82-
void *key = get_consistent_key(ctx, context_ptr);
83-
void *sqlReq_ptr = bpf_map_lookup_elem(&sql_events, &key);
84-
85-
struct sql_request_t sqlReq = {0};
86-
bpf_probe_read(&sqlReq, sizeof(sqlReq), sqlReq_ptr);
87-
sqlReq.end_time = bpf_ktime_get_ns();
88-
89-
// Send event
90-
bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, &sqlReq, sizeof(sqlReq));
91-
// Clean the sql event
92-
bpf_map_delete_elem(&sql_events, &key);
93-
stop_tracking_span(&sqlReq.sc);
94-
return 0;
95-
}
89+
UPROBE_RETURN(queryDC, struct sql_request_t, 3, 0, sql_events, events)

pkg/instrumentors/bpf/database/sql/bpf_bpfel_arm64.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/instrumentors/bpf/database/sql/bpf_bpfel_x86.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/instrumentors/bpf/database/sql/probe.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,13 @@ import (
4040

4141
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target amd64,arm64 -cc clang -cflags $CFLAGS bpf ./bpf/probe.bpf.c
4242

43+
const instrumentedPkg = "database/sql"
44+
4345
// Event represents an event in an SQL database
4446
// request-response.
4547
type Event struct {
46-
StartTime uint64
47-
EndTime uint64
48-
Query [100]byte
49-
SpanContext context.EBPFSpanContext
50-
ParentSpanContext context.EBPFSpanContext
48+
context.BaseSpanProperties
49+
Query [100]byte
5150
}
5251

5352
// Instrumentor is the database/sql instrumentor.
@@ -68,7 +67,7 @@ func New() *Instrumentor {
6867

6968
// LibraryName returns the database/sql/ package name.
7069
func (h *Instrumentor) LibraryName() string {
71-
return "database/sql"
70+
return instrumentedPkg
7271
}
7372

7473
// FuncNames returns the function names from "database/sql" that are instrumented.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package sql
16+
17+
import (
18+
"testing"
19+
"time"
20+
21+
"github.com/stretchr/testify/assert"
22+
23+
"go.opentelemetry.io/auto/pkg/instrumentors/context"
24+
"go.opentelemetry.io/auto/pkg/instrumentors/events"
25+
"go.opentelemetry.io/otel/attribute"
26+
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
27+
"go.opentelemetry.io/otel/trace"
28+
)
29+
30+
func TestInstrumentorConvertEvent(t *testing.T) {
31+
start := time.Now()
32+
end := start.Add(1 * time.Second)
33+
34+
traceID := trace.TraceID{1}
35+
spanID := trace.SpanID{1}
36+
37+
i := New()
38+
got := i.convertEvent(&Event{
39+
BaseSpanProperties: context.BaseSpanProperties{
40+
StartTime: uint64(start.UnixNano()),
41+
EndTime: uint64(end.UnixNano()),
42+
SpanContext: context.EBPFSpanContext{TraceID: traceID, SpanID: spanID},
43+
},
44+
// "SELECT * FROM foo"
45+
Query: [100]byte{0x53, 0x45, 0x4c, 0x45, 0x43, 0x54, 0x20, 0x2a, 0x20, 0x46, 0x52, 0x4f, 0x4d, 0x20, 0x66, 0x6f, 0x6f},
46+
})
47+
48+
sc := trace.NewSpanContext(trace.SpanContextConfig{
49+
TraceID: traceID,
50+
SpanID: spanID,
51+
TraceFlags: trace.FlagsSampled,
52+
})
53+
want := &events.Event{
54+
Library: instrumentedPkg,
55+
Name: "DB",
56+
Kind: trace.SpanKindClient,
57+
StartTime: int64(start.UnixNano()),
58+
EndTime: int64(end.UnixNano()),
59+
SpanContext: &sc,
60+
Attributes: []attribute.KeyValue{
61+
semconv.DBStatementKey.String("SELECT * FROM foo"),
62+
},
63+
}
64+
assert.Equal(t, want, got)
65+
}

0 commit comments

Comments
 (0)