tracing: skip health checks for tracing, small logging improvements
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing

This commit is contained in:
2025-01-12 23:31:40 -08:00
parent abbff86db3
commit a2fc7786f7
3 changed files with 50 additions and 40 deletions

View File

@@ -105,7 +105,13 @@ func setupHTTP(ctx context.Context) error {
BaseContext: func(_ net.Listener) context.Context { return ctx },
ReadTimeout: time.Second,
WriteTimeout: 10 * time.Second,
Handler: otelhttp.NewHandler(versionHandler(mux), "geoipapi"),
Handler: otelhttp.NewHandler(
versionHandler(mux),
"geoipapi",
otelhttp.WithFilter(func(r *http.Request) bool {
return r.URL.Path != "/healthz"
}),
),
}
srvErr := make(chan error, 1)
go func() {
@@ -122,21 +128,22 @@ func setupHTTP(ctx context.Context) error {
return srv.Shutdown(context.Background())
}
func getCityIP(ip net.IP) (*geoip2.City, error) {
func getCityIP(ctx context.Context, ip net.IP) (*geoip2.City, error) {
rdr, err := open(cityDB)
if err != nil {
return nil, err
}
city, err := rdr.City(ip)
if err != nil {
log.Printf("error looking up %q: %s", ip, err)
logger.FromContext(ctx).WarnContext(ctx, "error looking up city", "ip", ip, "err", err)
return nil, fmt.Errorf("db lookup error")
}
return city, nil
}
func getCity(req *http.Request) (*geoip2.City, error) {
span := trace.SpanFromContext(req.Context())
ctx := req.Context()
span := trace.SpanFromContext(ctx)
req.ParseForm()
ipStr := req.FormValue("ip")
span.SetAttributes(attribute.String("ip", ipStr))
@@ -144,22 +151,23 @@ func getCity(req *http.Request) (*geoip2.City, error) {
if ip == nil {
return nil, fmt.Errorf("missing IP address")
}
return getCityIP(ip)
return getCityIP(ctx, ip)
}
func handleJSON(w http.ResponseWriter, req *http.Request) {
span := trace.SpanFromContext(req.Context())
ctx := req.Context()
span := trace.SpanFromContext(ctx)
span.SetName("/api/json")
city, err := getCity(req)
if err != nil {
log.Printf("getCity error: %s", err)
logger.FromContext(ctx).ErrorContext(ctx, "getCity error ", "err", err)
http.Error(w, "data error", 500)
return
}
b, err := json.Marshal(&city)
if err != nil {
log.Printf("Error marshaling JSON: %s", err)
logger.FromContext(ctx).ErrorContext(ctx, "error marshaling JSON", "err", err)
http.Error(w, "internal error", 500)
return
}
@@ -168,12 +176,13 @@ func handleJSON(w http.ResponseWriter, req *http.Request) {
}
func handleCountry(w http.ResponseWriter, req *http.Request) {
span := trace.SpanFromContext(req.Context())
ctx := req.Context()
span := trace.SpanFromContext(ctx)
span.SetName("/api/country")
city, err := getCity(req)
if err != nil {
log.Printf("getCity error: %s", err)
logger.FromContext(ctx).ErrorContext(ctx, "getCity error ", "err", err)
http.Error(w, "data error", 500)
return
}
@@ -183,14 +192,15 @@ func handleCountry(w http.ResponseWriter, req *http.Request) {
}
func handleHealth(w http.ResponseWriter, req *http.Request) {
span := trace.SpanFromContext(req.Context())
ctx := req.Context()
span := trace.SpanFromContext(ctx)
span.SetAttributes(attribute.Bool("app.drop_sample", true))
span.SetName("/healthz")
ip := net.ParseIP("199.43.0.43")
city, err := getCityIP(ip)
city, err := getCityIP(ctx, ip)
if err != nil {
log.Printf("getCity error: %s", err)
logger.FromContext(ctx).WarnContext(ctx, "health check getCity error ", "ip", ip, "err", err)
http.Error(w, "data error", 500)
return
}