config.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright The OpenTelemetry 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 resource // import "go.opentelemetry.io/otel/sdk/resource"
  15. import (
  16. "context"
  17. "go.opentelemetry.io/otel/attribute"
  18. )
  19. // config contains configuration for Resource creation.
  20. type config struct {
  21. // detectors that will be evaluated.
  22. detectors []Detector
  23. // SchemaURL to associate with the Resource.
  24. schemaURL string
  25. }
  26. // Option is the interface that applies a configuration option.
  27. type Option interface {
  28. // apply sets the Option value of a config.
  29. apply(config) config
  30. }
  31. // WithAttributes adds attributes to the configured Resource.
  32. func WithAttributes(attributes ...attribute.KeyValue) Option {
  33. return WithDetectors(detectAttributes{attributes})
  34. }
  35. type detectAttributes struct {
  36. attributes []attribute.KeyValue
  37. }
  38. func (d detectAttributes) Detect(context.Context) (*Resource, error) {
  39. return NewSchemaless(d.attributes...), nil
  40. }
  41. // WithDetectors adds detectors to be evaluated for the configured resource.
  42. func WithDetectors(detectors ...Detector) Option {
  43. return detectorsOption{detectors: detectors}
  44. }
  45. type detectorsOption struct {
  46. detectors []Detector
  47. }
  48. func (o detectorsOption) apply(cfg config) config {
  49. cfg.detectors = append(cfg.detectors, o.detectors...)
  50. return cfg
  51. }
  52. // WithFromEnv adds attributes from environment variables to the configured resource.
  53. func WithFromEnv() Option {
  54. return WithDetectors(fromEnv{})
  55. }
  56. // WithHost adds attributes from the host to the configured resource.
  57. func WithHost() Option {
  58. return WithDetectors(host{})
  59. }
  60. // WithTelemetrySDK adds TelemetrySDK version info to the configured resource.
  61. func WithTelemetrySDK() Option {
  62. return WithDetectors(telemetrySDK{})
  63. }
  64. // WithSchemaURL sets the schema URL for the configured resource.
  65. func WithSchemaURL(schemaURL string) Option {
  66. return schemaURLOption(schemaURL)
  67. }
  68. type schemaURLOption string
  69. func (o schemaURLOption) apply(cfg config) config {
  70. cfg.schemaURL = string(o)
  71. return cfg
  72. }
  73. // WithOS adds all the OS attributes to the configured Resource.
  74. // See individual WithOS* functions to configure specific attributes.
  75. func WithOS() Option {
  76. return WithDetectors(
  77. osTypeDetector{},
  78. osDescriptionDetector{},
  79. )
  80. }
  81. // WithOSType adds an attribute with the operating system type to the configured Resource.
  82. func WithOSType() Option {
  83. return WithDetectors(osTypeDetector{})
  84. }
  85. // WithOSDescription adds an attribute with the operating system description to the
  86. // configured Resource. The formatted string is equivalent to the output of the
  87. // `uname -snrvm` command.
  88. func WithOSDescription() Option {
  89. return WithDetectors(osDescriptionDetector{})
  90. }
  91. // WithProcess adds all the Process attributes to the configured Resource.
  92. //
  93. // Warning! This option will include process command line arguments. If these
  94. // contain sensitive information it will be included in the exported resource.
  95. //
  96. // This option is equivalent to calling WithProcessPID,
  97. // WithProcessExecutableName, WithProcessExecutablePath,
  98. // WithProcessCommandArgs, WithProcessOwner, WithProcessRuntimeName,
  99. // WithProcessRuntimeVersion, and WithProcessRuntimeDescription. See each
  100. // option function for information about what resource attributes each
  101. // includes.
  102. func WithProcess() Option {
  103. return WithDetectors(
  104. processPIDDetector{},
  105. processExecutableNameDetector{},
  106. processExecutablePathDetector{},
  107. processCommandArgsDetector{},
  108. processOwnerDetector{},
  109. processRuntimeNameDetector{},
  110. processRuntimeVersionDetector{},
  111. processRuntimeDescriptionDetector{},
  112. )
  113. }
  114. // WithProcessPID adds an attribute with the process identifier (PID) to the
  115. // configured Resource.
  116. func WithProcessPID() Option {
  117. return WithDetectors(processPIDDetector{})
  118. }
  119. // WithProcessExecutableName adds an attribute with the name of the process
  120. // executable to the configured Resource.
  121. func WithProcessExecutableName() Option {
  122. return WithDetectors(processExecutableNameDetector{})
  123. }
  124. // WithProcessExecutablePath adds an attribute with the full path to the process
  125. // executable to the configured Resource.
  126. func WithProcessExecutablePath() Option {
  127. return WithDetectors(processExecutablePathDetector{})
  128. }
  129. // WithProcessCommandArgs adds an attribute with all the command arguments (including
  130. // the command/executable itself) as received by the process to the configured
  131. // Resource.
  132. //
  133. // Warning! This option will include process command line arguments. If these
  134. // contain sensitive information it will be included in the exported resource.
  135. func WithProcessCommandArgs() Option {
  136. return WithDetectors(processCommandArgsDetector{})
  137. }
  138. // WithProcessOwner adds an attribute with the username of the user that owns the process
  139. // to the configured Resource.
  140. func WithProcessOwner() Option {
  141. return WithDetectors(processOwnerDetector{})
  142. }
  143. // WithProcessRuntimeName adds an attribute with the name of the runtime of this
  144. // process to the configured Resource.
  145. func WithProcessRuntimeName() Option {
  146. return WithDetectors(processRuntimeNameDetector{})
  147. }
  148. // WithProcessRuntimeVersion adds an attribute with the version of the runtime of
  149. // this process to the configured Resource.
  150. func WithProcessRuntimeVersion() Option {
  151. return WithDetectors(processRuntimeVersionDetector{})
  152. }
  153. // WithProcessRuntimeDescription adds an attribute with an additional description
  154. // about the runtime of the process to the configured Resource.
  155. func WithProcessRuntimeDescription() Option {
  156. return WithDetectors(processRuntimeDescriptionDetector{})
  157. }
  158. // WithContainer adds all the Container attributes to the configured Resource.
  159. // See individual WithContainer* functions to configure specific attributes.
  160. func WithContainer() Option {
  161. return WithDetectors(
  162. cgroupContainerIDDetector{},
  163. )
  164. }
  165. // WithContainerID adds an attribute with the id of the container to the configured Resource.
  166. // Note: WithContainerID will not extract the correct container ID in an ECS environment.
  167. // Please use the ECS resource detector instead (https://pkg.go.dev/go.opentelemetry.io/contrib/detectors/aws/ecs).
  168. func WithContainerID() Option {
  169. return WithDetectors(cgroupContainerIDDetector{})
  170. }