Skip to content

Instantly share code, notes, and snippets.

@TonPC64
Last active February 24, 2022 14:41
Show Gist options
  • Save TonPC64/659024510565000cbe6612162a76fc8e to your computer and use it in GitHub Desktop.
Save TonPC64/659024510565000cbe6612162a76fc8e to your computer and use it in GitHub Desktop.
// create http client with transport for inject trace signal to http header automatically
client := &http.Client{
Transport: otelhttp.NewTransport(nil),
}
// create http request with context for inject trace signal to http header in request
request, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://echo:8081", nil)
if err != nil {
// if error should set status to span
span.RecordError(err)
span.SetStatus(codes.Error, codes.Error.String())
// write response like general handler
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("new request error"))
return
}
// use client to call other service
res, err := client.Do(request)
if err != nil {
// if error should set status to span
span.RecordError(err)
span.SetStatus(codes.Error, codes.Error.String())
// write response like general handler
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("http request error"))
return
}
fmt.Println(res.Status)
if res.StatusCode != http.StatusOK {
// if error should set status to span
span.RecordError(err)
span.SetStatus(codes.Error, codes.Error.String())
// write response like general handler
bb, _ := io.ReadAll(res.Body)
w.WriteHeader(http.StatusInternalServerError)
w.Write(bb)
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment