Skip to content

Commit 6386b10

Browse files
committed
following the review
1 parent 6c3fa73 commit 6386b10

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

plugin/skywalking/plugin-skywalking_test.go

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,50 @@ package skywalking
33
import (
44
"context"
55
"fmt"
6+
"io/ioutil"
67
"net/http"
8+
"sync"
79
"testing"
810

911
"github.com/OpenFunction/functions-framework-go/framework"
1012
"github.com/OpenFunction/functions-framework-go/plugin"
13+
"github.com/SkyAPM/go2sky"
14+
httpP "github.com/SkyAPM/go2sky/plugins/http"
15+
"k8s.io/klog/v2"
1116
)
1217

18+
var (
19+
initHttpClientOnce sync.Once
20+
client *http.Client
21+
)
22+
23+
func initHTTPClient() {
24+
initHttpClientOnce.Do(func() {
25+
client, _ = httpP.NewClient(go2sky.GetGlobalTracer())
26+
})
27+
}
28+
1329
// HelloWorld writes "Hello, World!" to the HTTP response.
1430
func HelloWorldWithHttp(w http.ResponseWriter, r *http.Request) error {
15-
fmt.Fprint(w, "Hello, World!\n")
31+
initHTTPClient()
32+
// call end service
33+
request, err := http.NewRequest("POST", fmt.Sprintf("%s/correlation", "http://127.0.0.1:9090"), nil)
34+
if err != nil {
35+
klog.Errorf("unable to create http request: %+v\n", err)
36+
return err
37+
}
38+
39+
request = request.WithContext(r.Context())
40+
res, err := client.Do(request)
41+
if err != nil {
42+
return err
43+
}
44+
body, err := ioutil.ReadAll(res.Body)
45+
if err != nil {
46+
return err
47+
}
48+
defer res.Body.Close()
49+
fmt.Fprint(w, string(body))
1650
return nil
1751
}
1852

@@ -37,14 +71,15 @@ func TestHTTP(t *testing.T) {
3771
"tag2": "value2"
3872
},
3973
"baggage": {
40-
"key": "sw8-correlation",
41-
"value": "base64(string key):base64(string value),base64(string key2):base64(string value2)"
74+
"key": "CONSUMER_KEY",
75+
"value": "test"
4276
}
4377
}
4478
}
4579
`)
46-
t.Setenv("POD_NAME", "prometheus-node-exporter-vhct4")
80+
t.Setenv("POD_NAME", "function-test-vhct4")
4781
t.Setenv("POD_NAMESPACE", "test")
82+
t.Setenv("SW_AGENT_COLLECTOR_GET_AGENT_DYNAMIC_CONFIG_INTERVAL", "-1")
4883
ctx := context.Background()
4984
fwk, err := framework.NewFramework()
5085
if err != nil {

plugin/skywalking/sync-request.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package skywalking
22

33
import (
4-
"fmt"
54
"time"
65

76
ofctx "github.com/OpenFunction/functions-framework-go/context"
@@ -21,7 +20,7 @@ func preSyncRequestLogic(ofCtx *ofctx.Context, tracer *go2sky.Tracer) error {
2120

2221
span.SetComponent(5004)
2322
span.Tag(go2sky.TagHTTPMethod, request.Method)
24-
span.Tag(go2sky.TagURL, fmt.Sprintf("%s%s", request.Host, request.URL.Path))
23+
span.Tag(go2sky.TagURL, request.URL.String())
2524
setPublicAttrs(nCtx, ofCtx, span)
2625
return nil
2726
}
@@ -32,14 +31,13 @@ func postSyncRequestLogic(ctx *ofctx.Context) error {
3231
if span == nil {
3332
return nil
3433
}
34+
defer span.End()
3535
if ofctx.InternalError == ctx.Out.Code {
3636
span.Error(time.Now(), "Error on handling request")
3737
}
3838

3939
if ctx.Error != nil {
4040
span.Error(time.Now(), ctx.Error.Error())
4141
}
42-
43-
span.End()
4442
return nil
4543
}

runtime/knative/knative.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (r *Runtime) RegisterHTTPFunction(
9999

100100
rm.ProcessPreHooks()
101101

102-
rm.FuncContext.Error = fn(w, r)
102+
rm.FuncContext.Error = fn(*rm.FuncContext.SyncRequestMeta.ResponseWriter, rm.FuncContext.SyncRequestMeta.Request)
103103

104104
rm.ProcessPostHooks()
105105

0 commit comments

Comments
 (0)