os.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. "strings"
  18. "go.opentelemetry.io/otel/attribute"
  19. semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
  20. )
  21. type osDescriptionProvider func() (string, error)
  22. var defaultOSDescriptionProvider osDescriptionProvider = platformOSDescription
  23. var osDescription = defaultOSDescriptionProvider
  24. func setDefaultOSDescriptionProvider() {
  25. setOSDescriptionProvider(defaultOSDescriptionProvider)
  26. }
  27. func setOSDescriptionProvider(osDescriptionProvider osDescriptionProvider) {
  28. osDescription = osDescriptionProvider
  29. }
  30. type osTypeDetector struct{}
  31. type osDescriptionDetector struct{}
  32. // Detect returns a *Resource that describes the operating system type the
  33. // service is running on.
  34. func (osTypeDetector) Detect(ctx context.Context) (*Resource, error) {
  35. osType := runtimeOS()
  36. osTypeAttribute := mapRuntimeOSToSemconvOSType(osType)
  37. return NewWithAttributes(
  38. semconv.SchemaURL,
  39. osTypeAttribute,
  40. ), nil
  41. }
  42. // Detect returns a *Resource that describes the operating system the
  43. // service is running on.
  44. func (osDescriptionDetector) Detect(ctx context.Context) (*Resource, error) {
  45. description, err := osDescription()
  46. if err != nil {
  47. return nil, err
  48. }
  49. return NewWithAttributes(
  50. semconv.SchemaURL,
  51. semconv.OSDescription(description),
  52. ), nil
  53. }
  54. // mapRuntimeOSToSemconvOSType translates the OS name as provided by the Go runtime
  55. // into an OS type attribute with the corresponding value defined by the semantic
  56. // conventions. In case the provided OS name isn't mapped, it's transformed to lowercase
  57. // and used as the value for the returned OS type attribute.
  58. func mapRuntimeOSToSemconvOSType(osType string) attribute.KeyValue {
  59. // the elements in this map are the intersection between
  60. // available GOOS values and defined semconv OS types
  61. osTypeAttributeMap := map[string]attribute.KeyValue{
  62. "aix": semconv.OSTypeAIX,
  63. "darwin": semconv.OSTypeDarwin,
  64. "dragonfly": semconv.OSTypeDragonflyBSD,
  65. "freebsd": semconv.OSTypeFreeBSD,
  66. "linux": semconv.OSTypeLinux,
  67. "netbsd": semconv.OSTypeNetBSD,
  68. "openbsd": semconv.OSTypeOpenBSD,
  69. "solaris": semconv.OSTypeSolaris,
  70. "windows": semconv.OSTypeWindows,
  71. "zos": semconv.OSTypeZOS,
  72. }
  73. var osTypeAttribute attribute.KeyValue
  74. if attr, ok := osTypeAttributeMap[osType]; ok {
  75. osTypeAttribute = attr
  76. } else {
  77. osTypeAttribute = semconv.OSTypeKey.String(strings.ToLower(osType))
  78. }
  79. return osTypeAttribute
  80. }