|
| 1 | +// Copyright (c) 2024 Alibaba Group Holding Ltd. |
| 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 main |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "github.com/alibaba/opentelemetry-go-auto-instrumentation/test/verifier" |
| 20 | + "go.opentelemetry.io/otel/sdk/trace/tracetest" |
| 21 | + "log" |
| 22 | + "net/http" |
| 23 | + "strconv" |
| 24 | + "time" |
| 25 | +) |
| 26 | + |
| 27 | +func setupBasicHttp() { |
| 28 | + http.HandleFunc("/a", redirectHandler) |
| 29 | + http.HandleFunc("/b", helloHandler) |
| 30 | + var err error |
| 31 | + port, err = verifier.GetFreePort() |
| 32 | + if err != nil { |
| 33 | + panic(err) |
| 34 | + } |
| 35 | + err = http.ListenAndServe(":"+strconv.Itoa(port), nil) |
| 36 | + if err != nil { |
| 37 | + panic(err) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func main() { |
| 42 | + go setupBasicHttp() |
| 43 | + time.Sleep(1 * time.Second) |
| 44 | + _, err := http.Get("http://127.0.0.1:" + strconv.Itoa(port) + "/a") |
| 45 | + if err != nil { |
| 46 | + panic(err) |
| 47 | + } |
| 48 | + jsonData := []byte(`{"key1": "value1", "key2": "value2"}`) |
| 49 | + req, err := http.NewRequest("POST", "http://127.0.0.1:"+strconv.Itoa(port)+"/a", bytes.NewBuffer(jsonData)) |
| 50 | + if err != nil { |
| 51 | + panic(err) |
| 52 | + } |
| 53 | + req.Header.Set("Content-Type", "application/json") |
| 54 | + client := http.Client{} |
| 55 | + resp, err := client.Do(req) |
| 56 | + if err != nil { |
| 57 | + panic(err) |
| 58 | + } |
| 59 | + defer resp.Body.Close() |
| 60 | + time.Sleep(1 * time.Second) |
| 61 | + verifier.WaitAndAssertTraces(func(stubs []tracetest.SpanStubs) { |
| 62 | + verifier.VerifyHttpClientAttributes(stubs[0][0], "GET", "GET", "http://127.0.0.1:"+strconv.Itoa(port)+"/a", "http", "1.1", "tcp", "ipv4", "", "127.0.0.1:"+strconv.Itoa(port), 200, 0, int64(port)) |
| 63 | + verifier.VerifyHttpServerAttributes(stubs[0][1], "GET /a", "GET", "http", "tcp", "ipv4", "", "127.0.0.1:"+strconv.Itoa(port), "Go-http-client/1.1", "http", "/a", "", "/a", 200) |
| 64 | + verifier.VerifyHttpClientAttributes(stubs[0][2], "GET", "GET", "http://127.0.0.1:"+strconv.Itoa(port)+"/b", "http", "1.1", "tcp", "ipv4", "", "127.0.0.1:"+strconv.Itoa(port), 200, 0, int64(port)) |
| 65 | + verifier.VerifyHttpServerAttributes(stubs[0][3], "GET /b", "GET", "http", "tcp", "ipv4", "", "127.0.0.1:"+strconv.Itoa(port), "Go-http-client/1.1", "http", "/b", "", "/b", 200) |
| 66 | + if stubs[0][1].Parent.TraceID().String() != stubs[0][0].SpanContext.TraceID().String() { |
| 67 | + log.Fatal("span 1 should be child of span 0") |
| 68 | + } |
| 69 | + if stubs[0][2].Parent.TraceID().String() != stubs[0][1].SpanContext.TraceID().String() { |
| 70 | + log.Fatal("span 2 should be child of span 1") |
| 71 | + } |
| 72 | + if stubs[0][3].Parent.TraceID().String() != stubs[0][2].SpanContext.TraceID().String() { |
| 73 | + log.Fatal("span 3 should be child of span 2") |
| 74 | + } |
| 75 | + |
| 76 | + verifier.VerifyHttpClientAttributes(stubs[1][0], "POST", "POST", "http://127.0.0.1:"+strconv.Itoa(port)+"/a", "http", "1.1", "tcp", "ipv4", "", "127.0.0.1:"+strconv.Itoa(port), 200, 0, int64(port)) |
| 77 | + verifier.VerifyHttpServerAttributes(stubs[1][1], "POST /a", "POST", "http", "tcp", "ipv4", "", "127.0.0.1:"+strconv.Itoa(port), "Go-http-client/1.1", "http", "/a", "", "/a", 200) |
| 78 | + verifier.VerifyHttpClientAttributes(stubs[1][2], "GET", "GET", "http://127.0.0.1:"+strconv.Itoa(port)+"/b", "http", "1.1", "tcp", "ipv4", "", "127.0.0.1:"+strconv.Itoa(port), 200, 0, int64(port)) |
| 79 | + verifier.VerifyHttpServerAttributes(stubs[1][3], "GET /b", "GET", "http", "tcp", "ipv4", "", "127.0.0.1:"+strconv.Itoa(port), "Go-http-client/1.1", "http", "/b", "", "/b", 200) |
| 80 | + if stubs[1][1].Parent.TraceID().String() != stubs[1][0].SpanContext.TraceID().String() { |
| 81 | + log.Fatal("span 1 should be child of span 0") |
| 82 | + } |
| 83 | + if stubs[1][2].Parent.TraceID().String() != stubs[1][1].SpanContext.TraceID().String() { |
| 84 | + log.Fatal("span 2 should be child of span 1") |
| 85 | + } |
| 86 | + if stubs[1][3].Parent.TraceID().String() != stubs[1][2].SpanContext.TraceID().String() { |
| 87 | + log.Fatal("span 3 should be child of span 2") |
| 88 | + } |
| 89 | + }, 2) |
| 90 | +} |
0 commit comments