config.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package newrelic
  2. import (
  3. "errors"
  4. "time"
  5. "github.com/imgproxy/imgproxy/v3/ensure"
  6. "github.com/imgproxy/imgproxy/v3/env"
  7. )
  8. var (
  9. IMGPROXY_NEW_RELIC_APP_NAME = env.Describe("IMGPROXY_NEW_RELIC_APP_NAME", "string")
  10. IMGPROXY_NEW_RELIC_KEY = env.Describe("IMGPROXY_NEW_RELIC_KEY", "string")
  11. IMGPROXY_NEW_RELIC_LABELS = env.Describe("IMGPROXY_NEW_RELIC_LABELS", "semicolon-separated list of key=value pairs")
  12. )
  13. // Config holds the configuration for New Relic monitoring
  14. type Config struct {
  15. AppName string // New Relic application name
  16. Key string // New Relic license key (non-empty value enables New Relic)
  17. Labels map[string]string // New Relic labels/tags
  18. MetricsInterval time.Duration // Interval for sending metrics to New Relic
  19. }
  20. // NewDefaultConfig returns a new default configuration for New Relic monitoring
  21. func NewDefaultConfig() Config {
  22. return Config{
  23. AppName: "imgproxy",
  24. Key: "",
  25. Labels: make(map[string]string),
  26. MetricsInterval: 10 * time.Second,
  27. }
  28. }
  29. // LoadConfigFromEnv loads configuration from environment variables
  30. func LoadConfigFromEnv(c *Config) (*Config, error) {
  31. c = ensure.Ensure(c, NewDefaultConfig)
  32. err := errors.Join(
  33. env.String(&c.AppName, IMGPROXY_NEW_RELIC_APP_NAME),
  34. env.String(&c.Key, IMGPROXY_NEW_RELIC_KEY),
  35. env.StringMap(&c.Labels, IMGPROXY_NEW_RELIC_LABELS),
  36. )
  37. return c, err
  38. }
  39. // Enabled returns true if New Relic is enabled
  40. func (c *Config) Enabled() bool {
  41. return len(c.Key) > 0
  42. }
  43. // Validate checks the configuration for errors
  44. func (c *Config) Validate() error {
  45. // If Key is empty, New Relic is disabled, so no need to validate further
  46. if !c.Enabled() {
  47. return nil
  48. }
  49. // AppName should not be empty if New Relic is enabled
  50. if len(c.AppName) == 0 {
  51. return IMGPROXY_NEW_RELIC_APP_NAME.ErrorEmpty()
  52. }
  53. return nil
  54. }