Skip to content

Instantly share code, notes, and snippets.

@phewdry
Created November 13, 2019 00:20
Show Gist options
  • Save phewdry/5887773b2fa2e4547ac15d35c4dda887 to your computer and use it in GitHub Desktop.
Save phewdry/5887773b2fa2e4547ac15d35c4dda887 to your computer and use it in GitHub Desktop.
package main
func main() {
}
func TestA(t *testing.T){
//if len(os.Args) != 2 {
// panic("ERROR: Expecting one argument")
//}
tracer, closer := tracing.Init("hello-world")
defer closer.Close()
opentracing.SetGlobalTracer(tracer)
//helloTo := os.Args[1]
helloTo :="abc"
span := tracer.StartSpan("say-hello")
span.SetTag("hello-to", helloTo)
defer span.Finish()
ctx := opentracing.ContextWithSpan(context.Background(), span)
t.Run("sub1" , func(t *testing.T) {
t.Parallel()
helloStr := FormatString(ctx, helloTo)
fmt.Println(helloStr)
})
t.Run("sub2" , func(t *testing.T) {
//time.Sleep(3 * time.Second)
t.Parallel()
helloStr := FormatString(ctx, helloTo)
PrintHello(ctx, helloStr)
})
time.Sleep(time.Second*2)
}
func FormatString(ctx context.Context, helloTo string) string {
span, _ := opentracing.StartSpanFromContext(ctx, "FormatString")
defer span.Finish()
v := url.Values{}
v.Set("helloTo", helloTo)
url := "http://localhost:8081/format?" + v.Encode()
req, err := http.NewRequest("GET", url, nil)
if err != nil {
panic(err.Error())
}
ext.SpanKindRPCClient.Set(span)
ext.HTTPUrl.Set(span, url)
ext.HTTPMethod.Set(span, "GET")
span.Tracer().Inject(
span.Context(),
opentracing.HTTPHeaders,
opentracing.HTTPHeadersCarrier(req.Header),
)
resp, err := xhttp.Do(req)
if err != nil {
panic(err.Error())
}
helloStr := string(resp)
span.LogFields(
log.String("event", "string-format"),
log.String("value", helloStr),
)
return helloStr
}
func PrintHello(ctx context.Context, helloStr string) {
span, _ := opentracing.StartSpanFromContext(ctx, "PrintHello")
defer span.Finish()
v := url.Values{}
v.Set("helloStr", helloStr)
url := "http://localhost:8082/publish?" + v.Encode()
req, err := http.NewRequest("GET", url, nil)
if err != nil {
panic(err.Error())
}
ext.SpanKindRPCClient.Set(span)
ext.HTTPUrl.Set(span, url)
ext.HTTPMethod.Set(span, "GET")
span.Tracer().Inject(span.Context(), opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(req.Header))
if _, err := xhttp.Do(req); err != nil {
panic(err.Error())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment