agent.go 1016 B

123456789101112131415161718192021222324252627282930313233343536
  1. // Unless explicitly stated otherwise all files in this repository are licensed
  2. // under the Apache License Version 2.0.
  3. // This product includes software developed at Datadog (https://www.datadoghq.com/).
  4. // Copyright 2022 Datadog, Inc.
  5. package internal
  6. import (
  7. "net/url"
  8. "os"
  9. "gopkg.in/DataDog/dd-trace-go.v1/internal/log"
  10. )
  11. // AgentURLFromEnv determines the trace agent URL from environment variable
  12. // DD_TRACE_AGENT_URL. If the determined value is valid and the scheme is
  13. // supported (unix, http or https), it will return an *url.URL. Otherwise,
  14. // it returns nil.
  15. func AgentURLFromEnv() *url.URL {
  16. agentURL := os.Getenv("DD_TRACE_AGENT_URL")
  17. if agentURL == "" {
  18. return nil
  19. }
  20. u, err := url.Parse(agentURL)
  21. if err != nil {
  22. log.Warn("Failed to parse DD_TRACE_AGENT_URL: %v", err)
  23. return nil
  24. }
  25. switch u.Scheme {
  26. case "unix", "http", "https":
  27. return u
  28. default:
  29. log.Warn("Unsupported protocol %q in Agent URL %q. Must be one of: http, https, unix.", u.Scheme, agentURL)
  30. return nil
  31. }
  32. }