Skip to content

Commit 5226b9a

Browse files
committed
Merge branch 'main' into database/sql_instrumentation
2 parents 21be897 + c134632 commit 5226b9a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+240
-213
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,20 @@ OpenTelemetry Go Automatic Instrumentation adheres to [Semantic Versioning](http
88

99
## [Unreleased]
1010

11+
### Changed
12+
13+
- The function signature of `"go.opentelemetry.io/auto/offsets-tracker/downloader".DownloadBinary` has changed.
14+
It now has an additional flag indicating whether it'll build a dummy app for Go stdlib packages or not. ([#256]https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/256)
15+
- The function signature of `"go.opentelemetry.io/auto/offsets-tracker/target".New` has changed.
16+
It now accepts a flag to determine if the returned `Data` is from the Go stdlib or not. ([#256]https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/256)
17+
- Use UPROBE_RETURN to declare the common uprobe return logic (finding the corresponding context, setting up end time, and sending the event via perf buffer) ([#257]https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/257)
18+
- BASE_SPAN_PROPERTIES as common fields (start time, end time, SpanContext and ParentSpanContext) for all instrumentations events (consistent between C and Go structs). ([#257]https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/257)
19+
- Header guards in eBPF code. ([#257]https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/257)
20+
1121
### Fixed
1222

1323
- Fix context propagation across different goroutines. ([#118](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/118))
24+
- The offset tracker can once again build binaries for the Go stdlib. ([#256]https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/256)
1425

1526
## [v0.2.2-alpha] - 2023-07-12
1627

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ offsets:
8787

8888
.PHONY: docker-offsets
8989
docker-offsets:
90-
docker run --rm -v $(shell pwd):/app golang:1.20 /bin/sh -c "cd ../app && make offsets"
90+
docker run --rm -v $(shell pwd):/app golang:1.21 /bin/sh -c "cd ../app && make offsets"
9191

9292
.PHONY: update-licenses
9393
update-licenses: generate $(GOLICENSES)

include/alloc.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#ifndef _ALLOC_H_
16+
#define _ALLOC_H_
17+
1518
#include "bpf_helpers.h"
1619

1720
#define MAX_ENTRIES 50
@@ -119,3 +122,5 @@ static __always_inline void *write_target_data(void *data, s32 size)
119122
return NULL;
120123
}
121124
}
125+
126+
#endif

include/arguments.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#ifndef _ARGUMENTS_H_
16+
#define _ARGUMENTS_H_
17+
1518
#include "common.h"
1619
#include "bpf_tracing.h"
1720
#include "bpf_helpers.h"
@@ -80,4 +83,6 @@ static __always_inline void *get_consistent_key(struct pt_regs *ctx, void *conte
8083
void *ctx_ptr = 0;
8184
bpf_probe_read(&ctx_ptr, sizeof(ctx_ptr), contextContext);
8285
return ctx_ptr;
83-
}
86+
}
87+
88+
#endif

include/go_context.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#ifndef _GO_CONTEXT_H_
16+
#define _GO_CONTEXT_H_
17+
1518
#include "bpf_helpers.h"
1619

1720
#define MAX_DISTANCE 10
@@ -83,4 +86,6 @@ static __always_inline void stop_tracking_span(struct span_context *sc) {
8386

8487
bpf_map_delete_elem(&tracked_spans, &ctx);
8588
bpf_map_delete_elem(&tracked_spans_by_sc, sc);
86-
}
89+
}
90+
91+
#endif

include/go_types.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#ifndef _GO_TYPES_H
16+
#define _GO_TYPES_H
17+
1518
#include "alloc.h"
1619
#include "bpf_helpers.h"
1720

@@ -121,4 +124,6 @@ static __always_inline void append_item_to_slice(struct go_slice *slice, void *n
121124
// Update len
122125
slice->len++;
123126
long success = bpf_probe_write_user(slice_user_ptr->len, &slice->len, sizeof(slice->len));
124-
}
127+
}
128+
129+
#endif

include/span_context.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#ifndef _SPAN_CONTEXT_H_
16+
#define _SPAN_CONTEXT_H_
17+
1518
#include "utils.h"
1619

1720
#define SPAN_CONTEXT_STRING_SIZE 55
@@ -63,3 +66,5 @@ static __always_inline void w3c_string_to_span_context(char *str, struct span_co
6366
hex_string_to_bytes(str + trace_id_start_pos, TRACE_ID_STRING_SIZE, ctx->TraceID);
6467
hex_string_to_bytes(str + span_id_start_pod, SPAN_ID_STRING_SIZE, ctx->SpanID);
6568
}
69+
70+
#endif

include/uprobe.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
#ifndef _UPROBE_H_
16+
#define _UPROBE_H_
17+
18+
#include "common.h"
19+
#include "span_context.h"
20+
#include "go_context.h"
21+
22+
#define BASE_SPAN_PROPERTIES \
23+
u64 start_time; \
24+
u64 end_time; \
25+
struct span_context sc; \
26+
struct span_context psc;
27+
28+
// Common flow for uprobe return:
29+
// 1. Find consistend key for the current uprobe context
30+
// 2. Use the key to lookup for the uprobe context in the uprobe_context_map
31+
// 3. Update the end time of the found span
32+
// 4. Submit the constructed event to the agent code using perf buffer events_map
33+
// 5. Delete the span from the uprobe_context_map
34+
// 6. Delete the span from the global active spans map
35+
#define UPROBE_RETURN(name, event_type, ctx_struct_pos, ctx_struct_offset, uprobe_context_map, events_map) \
36+
SEC("uprobe/##name##") \
37+
int uprobe_##name##_Returns(struct pt_regs *ctx) { \
38+
void *req_ptr = get_argument(ctx, ctx_struct_pos); \
39+
void *key = get_consistent_key(ctx, (void *)(req_ptr + ctx_struct_offset)); \
40+
void *req_ptr_map = bpf_map_lookup_elem(&uprobe_context_map, &key); \
41+
event_type tmpReq = {}; \
42+
bpf_probe_read(&tmpReq, sizeof(tmpReq), req_ptr_map); \
43+
tmpReq.end_time = bpf_ktime_get_ns(); \
44+
bpf_perf_event_output(ctx, &events_map, BPF_F_CURRENT_CPU, &tmpReq, sizeof(tmpReq)); \
45+
bpf_map_delete_elem(&uprobe_context_map, &key); \
46+
stop_tracking_span(&tmpReq.sc); \
47+
return 0; \
48+
}
49+
50+
#endif

offsets-tracker/README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,24 @@ For example, here is the tracking of `method` field inside `transport.Stream` st
5757
5858
## Versions Discovery
5959
60-
By default, offsets-tracker finds availble versions by executing `go list -versions <target-name>`.
60+
By default, offsets-tracker finds available versions by executing `go list -versions <target-name>`.
6161
6262
Unfortunately, Go standard library versions are not discoverable via `go list`.
63-
In order to discover Go versions, offsets-tracker can fetch the versions published at `https://go.dev/dl`.
63+
In order to discover Go versions, the offsets-tracker fetch the versions published at `https://go.dev/dl`.
6464
Fetching `go.dev` for discovering versions can be enabled by setting`.FindVersionsBy(target.GoDevFileVersionsStrategy)` when registering a new target.
6565

6666
## Download Strategy
6767

6868
offsets-tracker wraps every Go module version as a Go application that depends on that module.
6969
Those applications are the result of [generating template files](./downloader/wrapper) with the appropriate version.
7070

71-
In the case of the Go standard library, offsets-tracker downloads the published binary for the specified version.
71+
In the case of the Go standard library, the offsets-tracker creates a dummy application that depends on the specific library. For example
72+
73+
```go
74+
import (
75+
_ "net/http"
76+
)
77+
```
7278

7379
## Version Constraints
7480

offsets-tracker/downloader/go_wrapper.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
_ "embed"
1919
"fmt"
2020
"io/fs"
21-
"io/ioutil"
21+
"os"
2222
"path"
2323

2424
"go.opentelemetry.io/auto/offsets-tracker/utils"
@@ -30,25 +30,35 @@ var (
3030
//go:embed wrapper/go.mod.txt
3131
goMod string
3232

33+
//go:embed wrapper/go.modstd.txt
34+
goModStdLib string
35+
3336
//go:embed wrapper/main.go.txt
3437
goMain string
3538
)
3639

3740
// DownloadBinary downloads the module with modName at version.
38-
func DownloadBinary(modName string, version string) (string, string, error) {
39-
dir, err := ioutil.TempDir("", appName)
41+
// revive:disable-next-line:flag-parameter
42+
func DownloadBinary(modName string, version string, isGoStandartLib bool) (string, string, error) {
43+
dir, err := os.MkdirTemp("", appName)
4044
if err != nil {
4145
return "", "", err
4246
}
4347

44-
goModContent := fmt.Sprintf(goMod, modName, version)
45-
err = ioutil.WriteFile(path.Join(dir, "go.mod"), []byte(goModContent), fs.ModePerm)
48+
var goModContent string
49+
if isGoStandartLib {
50+
goModContent = fmt.Sprintf(goModStdLib, version)
51+
} else {
52+
goModContent = fmt.Sprintf(goMod, modName, version)
53+
}
54+
55+
err = os.WriteFile(path.Join(dir, "go.mod"), []byte(goModContent), fs.ModePerm)
4656
if err != nil {
4757
return "", "", err
4858
}
4959

5060
goMainContent := fmt.Sprintf(goMain, modName)
51-
err = ioutil.WriteFile(path.Join(dir, "main.go"), []byte(goMainContent), fs.ModePerm)
61+
err = os.WriteFile(path.Join(dir, "main.go"), []byte(goMainContent), fs.ModePerm)
5262
if err != nil {
5363
return "", "", err
5464
}
@@ -58,7 +68,7 @@ func DownloadBinary(modName string, version string) (string, string, error) {
5868
return "", "", err
5969
}
6070

61-
_, _, err = utils.RunCommand("GOOS=linux GOARCH=amd64 go build", dir)
71+
_, _, err = utils.RunCommand(fmt.Sprintf("GOOS=linux GOARCH=amd64 go build -o %s", appName), dir)
6272
if err != nil {
6373
return "", "", err
6474
}

0 commit comments

Comments
 (0)