config.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2018, OpenCensus Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package trace
  15. import (
  16. "sync"
  17. "go.opencensus.io/trace/internal"
  18. )
  19. // Config represents the global tracing configuration.
  20. type Config struct {
  21. // DefaultSampler is the default sampler used when creating new spans.
  22. DefaultSampler Sampler
  23. // IDGenerator is for internal use only.
  24. IDGenerator internal.IDGenerator
  25. // MaxAnnotationEventsPerSpan is max number of annotation events per span
  26. MaxAnnotationEventsPerSpan int
  27. // MaxMessageEventsPerSpan is max number of message events per span
  28. MaxMessageEventsPerSpan int
  29. // MaxAnnotationEventsPerSpan is max number of attributes per span
  30. MaxAttributesPerSpan int
  31. // MaxLinksPerSpan is max number of links per span
  32. MaxLinksPerSpan int
  33. }
  34. var configWriteMu sync.Mutex
  35. const (
  36. // DefaultMaxAnnotationEventsPerSpan is default max number of annotation events per span
  37. DefaultMaxAnnotationEventsPerSpan = 32
  38. // DefaultMaxMessageEventsPerSpan is default max number of message events per span
  39. DefaultMaxMessageEventsPerSpan = 128
  40. // DefaultMaxAttributesPerSpan is default max number of attributes per span
  41. DefaultMaxAttributesPerSpan = 32
  42. // DefaultMaxLinksPerSpan is default max number of links per span
  43. DefaultMaxLinksPerSpan = 32
  44. )
  45. // ApplyConfig applies changes to the global tracing configuration.
  46. //
  47. // Fields not provided in the given config are going to be preserved.
  48. func ApplyConfig(cfg Config) {
  49. configWriteMu.Lock()
  50. defer configWriteMu.Unlock()
  51. c := *config.Load().(*Config)
  52. if cfg.DefaultSampler != nil {
  53. c.DefaultSampler = cfg.DefaultSampler
  54. }
  55. if cfg.IDGenerator != nil {
  56. c.IDGenerator = cfg.IDGenerator
  57. }
  58. if cfg.MaxAnnotationEventsPerSpan > 0 {
  59. c.MaxAnnotationEventsPerSpan = cfg.MaxAnnotationEventsPerSpan
  60. }
  61. if cfg.MaxMessageEventsPerSpan > 0 {
  62. c.MaxMessageEventsPerSpan = cfg.MaxMessageEventsPerSpan
  63. }
  64. if cfg.MaxAttributesPerSpan > 0 {
  65. c.MaxAttributesPerSpan = cfg.MaxAttributesPerSpan
  66. }
  67. if cfg.MaxLinksPerSpan > 0 {
  68. c.MaxLinksPerSpan = cfg.MaxLinksPerSpan
  69. }
  70. config.Store(&c)
  71. }