forked from vfarcic/cncf-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping.go
38 lines (35 loc) · 889 Bytes
/
ping.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
package main
import (
"errors"
"log"
"net/http"
"os"
"github.com/gin-gonic/gin"
"github.com/go-resty/resty/v2"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
)
func pingHandler(ctx *gin.Context) {
req := resty.New().R().SetHeader("Content-Type", "application/text")
otelCtx := ctx.Request.Context()
span := trace.SpanFromContext(otelCtx)
defer span.End()
otel.GetTextMapPropagator().Inject(otelCtx, propagation.HeaderCarrier(req.Header))
url := ctx.Query("url")
if len(url) == 0 {
url = os.Getenv("PING_URL")
if len(url) == 0 {
httpErrorBadRequest(errors.New("url is empty"), span, ctx)
return
}
}
log.Printf("Sending a ping to %s", url)
resp, err := req.Get(url)
if err != nil {
httpErrorBadRequest(err, span, ctx)
return
}
log.Println(resp.String())
ctx.String(http.StatusOK, resp.String())
}