globalconfig.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 2016 Datadog, Inc.
  5. // Package globalconfig stores configuration which applies globally to both the tracer
  6. // and integrations.
  7. package globalconfig
  8. import (
  9. "math"
  10. "sync"
  11. "github.com/google/uuid"
  12. )
  13. var cfg = &config{
  14. analyticsRate: math.NaN(),
  15. runtimeID: uuid.New().String(),
  16. }
  17. type config struct {
  18. mu sync.RWMutex
  19. analyticsRate float64
  20. serviceName string
  21. runtimeID string
  22. }
  23. // AnalyticsRate returns the sampling rate at which events should be marked. It uses
  24. // synchronizing mechanisms, meaning that for optimal performance it's best to read it
  25. // once and store it.
  26. func AnalyticsRate() float64 {
  27. cfg.mu.RLock()
  28. defer cfg.mu.RUnlock()
  29. return cfg.analyticsRate
  30. }
  31. // SetAnalyticsRate sets the given event sampling rate globally.
  32. func SetAnalyticsRate(rate float64) {
  33. cfg.mu.Lock()
  34. cfg.analyticsRate = rate
  35. cfg.mu.Unlock()
  36. }
  37. // ServiceName returns the default service name used by non-client integrations such as servers and frameworks.
  38. func ServiceName() string {
  39. cfg.mu.RLock()
  40. defer cfg.mu.RUnlock()
  41. return cfg.serviceName
  42. }
  43. // SetServiceName sets the global service name set for this application.
  44. func SetServiceName(name string) {
  45. cfg.mu.Lock()
  46. defer cfg.mu.Unlock()
  47. cfg.serviceName = name
  48. }
  49. // RuntimeID returns this process's unique runtime id.
  50. func RuntimeID() string {
  51. cfg.mu.RLock()
  52. defer cfg.mu.RUnlock()
  53. return cfg.runtimeID
  54. }