Add additional attributes to span
@@ -5,6 +5,7 @@ import (
"net/http"
"net/http/httputil"
"net/url"
+ "os"
"strings"
"github.com/go-chi/chi"
@@ -17,6 +18,7 @@ import (
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"go.opencensus.io/tag"
+ "go.opencensus.io/trace"
)
const (
@@ -106,11 +108,37 @@ func createDirector(proxyDefinition *Definition, balancer balancer.Balancer, sta
}).Info("Proxying request to the following upstream")
statsClient.TrackMetric(statsSection, bucket.MetricOperation{req.Host})
+
+ // Add additional trace attributes
+ addTraceAttributes(req)
+
+ // Insert additional tags
ctx, _ := tag.New(req.Context(), tag.Insert(obs.KeyUpstreamPath, upstream.Target))
*req = *req.WithContext(ctx)
}
}
+func addTraceAttributes(req *http.Request) {
+ ctx := req.Context()
+ span := trace.FromContext(ctx)
+ if span == nil {
+ return
+ }
+
+ host, err := os.Hostname()
+ if host == "" || err != nil {
+ log.WithError(err).Debug("Failed to get host name")
+ host = "unknown"
+ }
+
+ span.AddAttributes(
+ trace.StringAttribute("http.host", host),
+ trace.StringAttribute("http.referrer", req.Referer()),
+ trace.StringAttribute("http.remote_address", req.RemoteAddr),
+ trace.StringAttribute("request.id", middleware.RequestIDFromContext(ctx)),
+ )
+}
+
func applyParameters(req *http.Request, path string, paramNames []string) (string, error) {
for _, paramName := range paramNames {
paramValue := chi.URLParam(req, paramName)
