Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure net http client code also works with the RoundTripper interface #529

Merged
merged 8 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ updates:
schedule:
interval: weekly
day: sunday
- package-ecosystem: docker
directory: /internal/test/e2e/nethttp_custom
labels:
- dependencies
- docker
- Skip Changelog
schedule:
interval: weekly
day: sunday
- package-ecosystem: gomod
directory: /
labels:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/kind.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
matrix:
k8s-version: ["v1.26.0"]
library: ["nethttp", "gin", "databasesql", "grpc"]
library: ["nethttp", "nethttp_custom", "gin", "databasesql", "grpc"]
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ OpenTelemetry Go Automatic Instrumentation adheres to [Semantic Versioning](http
- The instrumentation scope name for the `google.golang.org/grpc/server` instrumentation is now `go.opentelemtry.io/auto/google.golang.org/grpc`. (#507)
- The instrumentation scope name for the `net/http/client` instrumentation is now `go.opentelemtry.io/auto/net/http`. (#507)
- The instrumentation scope name for the `net/http/server` instrumentation is now `go.opentelemtry.io/auto/net/http`. (#507)
- The instrumentation for `client.Do` was changed to instrumentation for `Transport.roundTrip`. ([#529](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/529))

## [v0.8.0-alpha] - 2023-11-14

Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ license-header-check:
exit 1; \
fi

.PHONY: fixture-nethttp fixture-gin fixture-databasesql
.PHONY: fixture-nethttp fixture-gin fixture-databasesql fixture-nethttp-custom
RonFed marked this conversation as resolved.
Show resolved Hide resolved
fixture-nethttp-custom: fixtures/nethttp_custom
fixture-nethttp: fixtures/nethttp
fixture-gin: fixtures/gin
fixture-databasesql: fixtures/databasesql
Expand Down
24 changes: 12 additions & 12 deletions internal/pkg/instrumentation/bpf/net/http/client/bpf/probe.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ static __always_inline long inject_header(void* headers_ptr, struct span_context
}

// This instrumentation attaches uprobe to the following function:
// func net/http/client.Do(req *Request) (*Response, error)
SEC("uprobe/HttpClient_Do")
int uprobe_HttpClient_Do(struct pt_regs *ctx) {
// func net/http/transport.roundTrip(req *Request) (*Response, error)
SEC("uprobe/Transport_roundTrip")
int uprobe_Transport_roundTrip(struct pt_regs *ctx) {
u64 request_pos = 2;
void *req_ptr = get_argument(ctx, request_pos);

Expand All @@ -172,15 +172,15 @@ int uprobe_HttpClient_Do(struct pt_regs *ctx) {
void *httpReq_ptr = bpf_map_lookup_elem(&http_events, &key);
if (httpReq_ptr != NULL)
{
bpf_printk("uprobe/HttpClient_Do already tracked with the current context");
bpf_printk("uprobe/Transport_RoundTrip already tracked with the current context");
return 0;
}

u32 map_id = 0;
struct http_request_t *httpReq = bpf_map_lookup_elem(&http_client_uprobe_storage_map, &map_id);
if (httpReq == NULL)
{
bpf_printk("uprobe/HttpClient_Do: httpReq is NULL");
bpf_printk("uprobe/Transport_roundTrip: httpReq is NULL");
return 0;
}

Expand All @@ -197,15 +197,15 @@ int uprobe_HttpClient_Do(struct pt_regs *ctx) {
}

if (!get_go_string_from_user_ptr((void *)(req_ptr+method_ptr_pos), httpReq->method, sizeof(httpReq->method))) {
bpf_printk("uprobe_HttpClient_Do: Failed to get method from request");
bpf_printk("uprobe_Transport_roundTrip: Failed to get method from request");
return 0;
}

// get path from Request.URL
void *url_ptr = 0;
bpf_probe_read(&url_ptr, sizeof(url_ptr), (void *)(req_ptr+url_ptr_pos));
if (!get_go_string_from_user_ptr((void *)(url_ptr+path_ptr_pos), httpReq->path, sizeof(httpReq->path))) {
bpf_printk("uprobe_HttpClient_Do: Failed to get path from Request.URL");
bpf_printk("uprobe_Transport_roundTrip: Failed to get path from Request.URL");
return 0;
}

Expand All @@ -214,7 +214,7 @@ int uprobe_HttpClient_Do(struct pt_regs *ctx) {
bpf_probe_read(&headers_ptr, sizeof(headers_ptr), (void *)(req_ptr+headers_ptr_pos));
long res = inject_header(headers_ptr, &httpReq->sc);
if (res < 0) {
bpf_printk("uprobe_HttpClient_Do: Failed to inject header");
bpf_printk("uprobe_Transport_roundTrip: Failed to inject header");
}

// Write event
Expand All @@ -224,16 +224,16 @@ int uprobe_HttpClient_Do(struct pt_regs *ctx) {
}

// This instrumentation attaches uretprobe to the following function:
// func net/http/client.Do(req *Request) (*Response, error)
SEC("uprobe/HttpClient_Do")
int uprobe_HttpClient_Do_Returns(struct pt_regs *ctx) {
// func net/http/transport.roundTrip(req *Request) (*Response, error)
SEC("uprobe/Transport_roundTrip")
int uprobe_Transport_roundTrip_Returns(struct pt_regs *ctx) {
u64 end_time = bpf_ktime_get_ns();
void *req_ctx_ptr = get_Go_context(ctx, 2, ctx_ptr_pos, false);
void *key = get_consistent_key(ctx, req_ctx_ptr);

struct http_request_t *http_req_span = bpf_map_lookup_elem(&http_events, &key);
if (http_req_span == NULL) {
bpf_printk("probe_HttpClient_Do_Returns: entry_state is NULL");
bpf_printk("probe_Transport_roundTrip_Returns: entry_state is NULL");
return 0;
}
bpf_map_delete_elem(&http_events, &key);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions internal/pkg/instrumentation/bpf/net/http/client/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func New(logger logr.Logger) probe.Probe {
},
},
Uprobes: map[string]probe.UprobeFunc[bpfObjects]{
"net/http.(*Client).do": uprobeDo,
"net/http.(*Transport).roundTrip": uprobeRoundTrip,
},

ReaderFn: func(obj bpfObjects) (*perf.Reader, error) {
Expand All @@ -90,14 +90,14 @@ func New(logger logr.Logger) probe.Probe {
}
}

func uprobeDo(name string, exec *link.Executable, target *process.TargetDetails, obj *bpfObjects) ([]link.Link, error) {
func uprobeRoundTrip(name string, exec *link.Executable, target *process.TargetDetails, obj *bpfObjects) ([]link.Link, error) {
offset, err := target.GetFunctionOffset(name)
if err != nil {
return nil, err
}

opts := &link.UprobeOptions{Address: offset}
l, err := exec.Uprobe("", obj.UprobeHttpClientDo, opts)
l, err := exec.Uprobe("", obj.UprobeTransportRoundTrip, opts)
if err != nil {
return nil, err
}
Expand All @@ -110,7 +110,7 @@ func uprobeDo(name string, exec *link.Executable, target *process.TargetDetails,

for _, ret := range retOffsets {
opts := &link.UprobeOptions{Address: ret}
l, err := exec.Uprobe("", obj.UprobeHttpClientDoReturns, opts)
l, err := exec.Uprobe("", obj.UprobeTransportRoundTripReturns, opts)
if err != nil {
return nil, err
}
Expand Down
4 changes: 4 additions & 0 deletions internal/test/e2e/nethttp_custom/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM golang:1.21.4
WORKDIR /sample-app
COPY . .
RUN go mod init go.opentelemetry.io/auto/internal/test/e2e/nethttp && go mod tidy && go build -o main
116 changes: 116 additions & 0 deletions internal/test/e2e/nethttp_custom/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"crypto/tls"
"fmt"
"io"
"log"
"net/http"
"time"
)

type statusRecorder struct {
rw http.ResponseWriter
status int
data []byte
}

func (r *statusRecorder) Header() http.Header {
return r.rw.Header()
}

func (r *statusRecorder) Write(data []byte) (int, error) {
r.data = data
return len(data), nil
}

func (r *statusRecorder) WriteHeader(code int) {
r.status = code
}

func logStatus(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rec := &statusRecorder{rw: w}

next.ServeHTTP(rec, r)

rec.rw.WriteHeader(rec.status)
_, err := rec.rw.Write(rec.data)
if err != nil {
log.Printf("write failed %s\n", err.Error())
return
}

log.Printf("response status: %d\n", rec.status)
})
}

func hello(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "hello\n")
}

var tr = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}

type MyRoundTripper struct{}

func (rt *MyRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Add("X-My-Header", "my-value")

// send the request using the custom transport
res, err := tr.RoundTrip(req)
if err != nil {
return nil, err
}

// process the response as needed
return res, nil
}

func main() {
go func() {
_ = http.ListenAndServe(":8080", logStatus(http.HandlerFunc(hello)))
}()

// give time for auto-instrumentation to start up
time.Sleep(5 * time.Second)

req, err := http.NewRequest("GET", "http://localhost:8080/hello", nil)
if err != nil {
log.Fatal(err)
return
}

mt := &MyRoundTripper{}

resp, err := mt.RoundTrip(req)
if err != nil {
log.Fatal(err)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}

log.Printf("Body: %s\n", string(body))
_ = resp.Body.Close()

// give time for auto-instrumentation to report signal
time.Sleep(5 * time.Second)
}
Loading
Loading