dapper.go 942 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package service
  2. import (
  3. "net/http"
  4. "net/http/httputil"
  5. "net/url"
  6. "strings"
  7. "github.com/pkg/errors"
  8. )
  9. const (
  10. _apmDapperPrefix = "/x/admin/apm/dapper"
  11. _dapperPrxfix = "/x/internal/dapper"
  12. )
  13. type dapperProxy struct {
  14. *httputil.ReverseProxy
  15. }
  16. func newDapperProxy(host string) (*dapperProxy, error) {
  17. if !strings.HasPrefix(host, "http") {
  18. host = "http://" + host
  19. }
  20. target, err := url.Parse(host)
  21. if err != nil {
  22. return nil, errors.Wrapf(err, "invalid dapper host %s", host)
  23. }
  24. proxy := httputil.NewSingleHostReverseProxy(target)
  25. proxy.Director = func(r *http.Request) {
  26. r.Host = target.Host
  27. r.URL.Host = target.Host
  28. r.URL.Scheme = target.Scheme
  29. r.URL.Path = strings.Replace(r.URL.Path, _apmDapperPrefix, _dapperPrxfix, 1)
  30. }
  31. return &dapperProxy{ReverseProxy: proxy}, nil
  32. }
  33. // DapperProxy dapper proxy.
  34. func (s *Service) DapperProxy(rw http.ResponseWriter, r *http.Request) {
  35. s.dapperProxy.ServeHTTP(rw, r)
  36. }