3 Commits

Author SHA1 Message Date
0edce7bab9 tracing: better span names
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2024-12-22 17:40:51 -08:00
d08c73a528 Add Server and Traceparent http headers
All checks were successful
continuous-integration/drone/push Build is passing
2024-12-22 17:35:48 -08:00
14edfaf0e9 Update base image and Go
All checks were successful
continuous-integration/drone/push Build is passing
2024-12-22 17:19:10 -08:00
4 changed files with 32 additions and 7 deletions

View File

@@ -5,7 +5,7 @@ name: default
steps: steps:
- name: test - name: test
image: golang:1.20.5 image: golang:1.23.4
volumes: volumes:
- name: deps - name: deps
path: /go path: /go
@@ -31,6 +31,6 @@ steps:
from_secret: harbor_password from_secret: harbor_password
--- ---
kind: signature kind: signature
hmac: 004f812ff29a1e2546eeafbf449c36c901f69b5f2591f83152eebde7258453fd hmac: d92e5a575088ed17ad15dbf7bf34f79f1d6b61c09d8b05fc7aab58f94c011e01
... ...

View File

@@ -1,11 +1,11 @@
FROM golang:1.20.5-alpine AS build FROM golang:1.23.4-alpine AS build
RUN apk --no-cache add git RUN apk --no-cache add git
WORKDIR /go/src/github.com/abh/geoipapi WORKDIR /go/src/github.com/abh/geoipapi
ADD . /go/src/github.com/abh/geoipapi ADD . /go/src/github.com/abh/geoipapi
RUN go install -v ./... RUN go install -v ./...
FROM alpine:3.18 FROM alpine:3.21
USER root USER root
RUN apk --no-cache add ca-certificates RUN apk --no-cache add ca-certificates
RUN apk --no-cache upgrade RUN apk --no-cache upgrade

View File

@@ -15,11 +15,13 @@ import (
"time" "time"
"github.com/oschwald/geoip2-golang" "github.com/oschwald/geoip2-golang"
"go.ntppool.org/common/logger"
"go.ntppool.org/common/tracing"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace" "go.opentelemetry.io/otel/trace"
"go.ntppool.org/common/logger"
"go.ntppool.org/common/tracing"
"go.ntppool.org/common/version"
) )
type geoType uint8 type geoType uint8
@@ -86,12 +88,24 @@ func setupHTTP(ctx context.Context) error {
mux.HandleFunc("/api/json", handleJSON) mux.HandleFunc("/api/json", handleJSON)
mux.HandleFunc("/healthz", handleHealth) mux.HandleFunc("/healthz", handleHealth)
versionHandler := func(next http.Handler) http.Handler {
vinfo := version.VersionInfo()
v := "geoipapi/" + vinfo.Version + "+" + vinfo.GitRevShort
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Server", v)
span := trace.SpanFromContext(r.Context())
w.Header().Set("Traceparent", span.SpanContext().TraceID().String())
next.ServeHTTP(w, r)
})
}
srv := &http.Server{ srv := &http.Server{
Addr: ":8009", Addr: ":8009",
BaseContext: func(_ net.Listener) context.Context { return ctx }, BaseContext: func(_ net.Listener) context.Context { return ctx },
ReadTimeout: time.Second, ReadTimeout: time.Second,
WriteTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second,
Handler: otelhttp.NewHandler(mux, "geoipapi"), Handler: otelhttp.NewHandler(versionHandler(mux), "geoipapi"),
} }
srvErr := make(chan error, 1) srvErr := make(chan error, 1)
go func() { go func() {
@@ -134,6 +148,8 @@ func getCity(req *http.Request) (*geoip2.City, error) {
} }
func handleJSON(w http.ResponseWriter, req *http.Request) { func handleJSON(w http.ResponseWriter, req *http.Request) {
span := trace.SpanFromContext(req.Context())
span.SetName("/api/json")
city, err := getCity(req) city, err := getCity(req)
if err != nil { if err != nil {
log.Printf("getCity error: %s", err) log.Printf("getCity error: %s", err)
@@ -152,6 +168,9 @@ func handleJSON(w http.ResponseWriter, req *http.Request) {
} }
func handleCountry(w http.ResponseWriter, req *http.Request) { func handleCountry(w http.ResponseWriter, req *http.Request) {
span := trace.SpanFromContext(req.Context())
span.SetName("/api/country")
city, err := getCity(req) city, err := getCity(req)
if err != nil { if err != nil {
log.Printf("getCity error: %s", err) log.Printf("getCity error: %s", err)
@@ -164,6 +183,10 @@ func handleCountry(w http.ResponseWriter, req *http.Request) {
} }
func handleHealth(w http.ResponseWriter, req *http.Request) { func handleHealth(w http.ResponseWriter, req *http.Request) {
span := trace.SpanFromContext(req.Context())
span.SetAttributes(attribute.Bool("app.drop_sample", true))
span.SetName("/healthz")
ip := net.ParseIP("199.43.0.43") ip := net.ParseIP("199.43.0.43")
city, err := getCityIP(ip) city, err := getCityIP(ip)
if err != nil { if err != nil {

2
go.mod
View File

@@ -2,6 +2,8 @@ module go.ntppool.org/geoipapi
go 1.23 go 1.23
toolchain go1.24rc1
require ( require (
github.com/oschwald/geoip2-golang v1.11.0 github.com/oschwald/geoip2-golang v1.11.0
go.ntppool.org/common v0.3.0 go.ntppool.org/common v0.3.0