config.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. // WithHostID adds host ID information to the configured resource.
  61. func WithHostID() Option {
  62. return WithDetectors(hostIDDetector{})
  63. }
  64. // WithTelemetrySDK adds TelemetrySDK version info to the configured resource.
  65. func WithTelemetrySDK() Option {
  66. return WithDetectors(telemetrySDK{})
  67. }
  68. // WithSchemaURL sets the schema URL for the configured resource.
  69. func WithSchemaURL(schemaURL string) Option {
  70. return schemaURLOption(schemaURL)
  71. }
  72. type schemaURLOption string
  73. func (o schemaURLOption) apply(cfg config) config {
  74. cfg.schemaURL = string(o)
  75. return cfg
  76. }
  77. // WithOS adds all the OS attributes to the configured Resource.
  78. // See individual WithOS* functions to configure specific attributes.
  79. func WithOS() Option {
  80. return WithDetectors(
  81. osTypeDetector{},
  82. osDescriptionDetector{},
  83. )
  84. }
  85. // WithOSType adds an attribute with the operating system type to the configured Resource.
  86. func WithOSType() Option {
  87. return WithDetectors(osTypeDetector{})
  88. }
  89. // WithOSDescription adds an attribute with the operating system description to the
  90. // configured Resource. The formatted string is equivalent to the output of the
  91. // `uname -snrvm` command.
  92. func WithOSDescription() Option {
  93. return WithDetectors(osDescriptionDetector{})
  94. }
  95. // WithProcess adds all the Process attributes to the configured Resource.
  96. //
  97. // Warning! This option will include process command line arguments. If these
  98. // contain sensitive information it will be included in the exported resource.
  99. //
  100. // This option is equivalent to calling WithProcessPID,
  101. // WithProcessExecutableName, WithProcessExecutablePath,
  102. // WithProcessCommandArgs, WithProcessOwner, WithProcessRuntimeName,
  103. // WithProcessRuntimeVersion, and WithProcessRuntimeDescription. See each
  104. // option function for information about what resource attributes each
  105. // includes.
  106. func WithProcess() Option {
  107. return WithDetectors(
  108. processPIDDetector{},
  109. processExecutableNameDetector{},
  110. processExecutablePathDetector{},
  111. processCommandArgsDetector{},
  112. processOwnerDetector{},
  113. processRuntimeNameDetector{},
  114. processRuntimeVersionDetector{},
  115. processRuntimeDescriptionDetector{},
  116. )
  117. }
  118. // WithProcessPID adds an attribute with the process identifier (PID) to the
  119. // configured Resource.
  120. func WithProcessPID() Option {
  121. return WithDetectors(processPIDDetector{})
  122. }
  123. // WithProcessExecutableName adds an attribute with the name of the process
  124. // executable to the configured Resource.
  125. func WithProcessExecutableName() Option {
  126. return WithDetectors(processExecutableNameDetector{})
  127. }
  128. // WithProcessExecutablePath adds an attribute with the full path to the process
  129. // executable to the configured Resource.
  130. func WithProcessExecutablePath() Option {
  131. return WithDetectors(processExecutablePathDetector{})
  132. }
  133. // WithProcessCommandArgs adds an attribute with all the command arguments (including
  134. // the command/executable itself) as received by the process to the configured
  135. // Resource.
  136. //
  137. // Warning! This option will include process command line arguments. If these
  138. // contain sensitive information it will be included in the exported resource.
  139. func WithProcessCommandArgs() Option {
  140. return WithDetectors(processCommandArgsDetector{})
  141. }
  142. // WithProcessOwner adds an attribute with the username of the user that owns the process
  143. // to the configured Resource.
  144. func WithProcessOwner() Option {
  145. return WithDetectors(processOwnerDetector{})
  146. }
  147. // WithProcessRuntimeName adds an attribute with the name of the runtime of this
  148. // process to the configured Resource.
  149. func WithProcessRuntimeName() Option {
  150. return WithDetectors(processRuntimeNameDetector{})
  151. }
  152. // WithProcessRuntimeVersion adds an attribute with the version of the runtime of
  153. // this process to the configured Resource.
  154. func WithProcessRuntimeVersion() Option {
  155. return WithDetectors(processRuntimeVersionDetector{})
  156. }
  157. // WithProcessRuntimeDescription adds an attribute with an additional description
  158. // about the runtime of the process to the configured Resource.
  159. func WithProcessRuntimeDescription() Option {
  160. return WithDetectors(processRuntimeDescriptionDetector{})
  161. }
  162. // WithContainer adds all the Container attributes to the configured Resource.
  163. // See individual WithContainer* functions to configure specific attributes.
  164. func WithContainer() Option {
  165. return WithDetectors(
  166. cgroupContainerIDDetector{},
  167. )
  168. }
  169. // WithContainerID adds an attribute with the id of the container to the configured Resource.
  170. // Note: WithContainerID will not extract the correct container ID in an ECS environment.
  171. // Please use the ECS resource detector instead (https://pkg.go.dev/go.opentelemetry.io/contrib/detectors/aws/ecs).
  172. func WithContainerID() Option {
  173. return WithDetectors(cgroupContainerIDDetector{})
  174. }