-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure net http client code also works with the RoundTripper interface (
#529) * Add tests with custom response and request code * Put the probe on roundTrip to handle all cases * Lint and changelog fixes * rename functions as per PR review feedback * update github tests * update CHANGELOG as per the review suggestion --------- Co-authored-by: Mike Goldsmith <[email protected]>
- Loading branch information
1 parent
32c6faa
commit dd90376
Showing
12 changed files
with
341 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 6 additions & 6 deletions
12
internal/pkg/instrumentation/bpf/net/http/client/bpf_bpfel_arm64.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
12 changes: 6 additions & 6 deletions
12
internal/pkg/instrumentation/bpf/net/http/client/bpf_bpfel_x86.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.