/
client.go
67 lines (60 loc) · 2.13 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 (
"io/ioutil"
"log"
"net"
"net/http"
"time"
"github.com/openzipkin/zipkin-go"
"github.com/openzipkin/zipkin-go/model"
"github.com/openzipkin/zipkin-go/propagation/b3"
zhttp "github.com/openzipkin/zipkin-go/reporter/http"
trace "mosn.io/mosn/pkg/trace/zipkin"
)
func main() {
httpReport := zhttp.NewReporter("http://localhost:9411/api/v2/spans", zhttp.BatchSize(1))
localEndpint := &model.Endpoint{
ServiceName: "http_client",
IPv4: net.ParseIP("127.0.0.1"),
IPv6: nil,
Port: 1,
}
tracer, err := zipkin.NewTracer(httpReport, zipkin.WithSampler(zipkin.AlwaysSample), zipkin.WithLocalEndpoint(localEndpint))
if err != nil {
log.Fatalf("create tracer error: %v", err)
}
span := tracer.StartSpan("client request", zipkin.Kind(model.Client), zipkin.StartTime(time.Now()))
span.Tag(trace.CallerAppName.String(), "ClientApp")
// execute http request
req, _ := http.NewRequest("GET", "http://localhost:2046/hello", nil)
err = b3.InjectHTTP(req)(span.Context())
if err != nil {
log.Fatalf("injet http error: %v", err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatalf("do request error: %v", resp)
}
defer resp.Body.Close()
data, _ := ioutil.ReadAll(resp.Body)
log.Printf("response: %v", string(data))
span.Finish()
// util trigger http report
time.Sleep(2 * time.Second)
}