attributes.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package tracing
  14. import (
  15. "strings"
  16. "time"
  17. "github.com/docker/compose/v2/pkg/utils"
  18. "github.com/compose-spec/compose-go/v2/types"
  19. moby "github.com/docker/docker/api/types"
  20. "go.opentelemetry.io/otel/attribute"
  21. "go.opentelemetry.io/otel/trace"
  22. )
  23. // SpanOptions is a small helper type to make it easy to share the options helpers between
  24. // downstream functions that accept slices of trace.SpanStartOption and trace.EventOption.
  25. type SpanOptions []trace.SpanStartEventOption
  26. func (s SpanOptions) SpanStartOptions() []trace.SpanStartOption {
  27. out := make([]trace.SpanStartOption, len(s))
  28. for i := range s {
  29. out[i] = s[i]
  30. }
  31. return out
  32. }
  33. func (s SpanOptions) EventOptions() []trace.EventOption {
  34. out := make([]trace.EventOption, len(s))
  35. for i := range s {
  36. out[i] = s[i]
  37. }
  38. return out
  39. }
  40. // ProjectOptions returns common attributes from a Compose project.
  41. //
  42. // For convenience, it's returned as a SpanOptions object to allow it to be
  43. // passed directly to the wrapping helper methods in this package such as
  44. // SpanWrapFunc.
  45. func ProjectOptions(proj *types.Project) SpanOptions {
  46. if proj == nil {
  47. return nil
  48. }
  49. attrs := []attribute.KeyValue{
  50. attribute.String("project.name", proj.Name),
  51. attribute.String("project.dir", proj.WorkingDir),
  52. attribute.StringSlice("project.compose_files", proj.ComposeFiles),
  53. attribute.StringSlice("project.services.active", proj.ServiceNames()),
  54. attribute.StringSlice("project.services.disabled", proj.DisabledServiceNames()),
  55. attribute.StringSlice("project.profiles", proj.Profiles),
  56. attribute.StringSlice("project.volumes", proj.VolumeNames()),
  57. attribute.StringSlice("project.networks", proj.NetworkNames()),
  58. attribute.StringSlice("project.secrets", proj.SecretNames()),
  59. attribute.StringSlice("project.configs", proj.ConfigNames()),
  60. attribute.StringSlice("project.extensions", keys(proj.Extensions)),
  61. attribute.StringSlice("project.includes", flattenIncludeReferences(proj.IncludeReferences)),
  62. }
  63. return []trace.SpanStartEventOption{
  64. trace.WithAttributes(attrs...),
  65. }
  66. }
  67. // ServiceOptions returns common attributes from a Compose service.
  68. //
  69. // For convenience, it's returned as a SpanOptions object to allow it to be
  70. // passed directly to the wrapping helper methods in this package such as
  71. // SpanWrapFunc.
  72. func ServiceOptions(service types.ServiceConfig) SpanOptions {
  73. attrs := []attribute.KeyValue{
  74. attribute.String("service.name", service.Name),
  75. attribute.String("service.image", service.Image),
  76. attribute.StringSlice("service.networks", keys(service.Networks)),
  77. }
  78. configNames := make([]string, len(service.Configs))
  79. for i := range service.Configs {
  80. configNames[i] = service.Configs[i].Source
  81. }
  82. attrs = append(attrs, attribute.StringSlice("service.configs", configNames))
  83. secretNames := make([]string, len(service.Secrets))
  84. for i := range service.Secrets {
  85. secretNames[i] = service.Secrets[i].Source
  86. }
  87. attrs = append(attrs, attribute.StringSlice("service.secrets", secretNames))
  88. volNames := make([]string, len(service.Volumes))
  89. for i := range service.Volumes {
  90. volNames[i] = service.Volumes[i].Source
  91. }
  92. attrs = append(attrs, attribute.StringSlice("service.volumes", volNames))
  93. return []trace.SpanStartEventOption{
  94. trace.WithAttributes(attrs...),
  95. }
  96. }
  97. // ContainerOptions returns common attributes from a Moby container.
  98. //
  99. // For convenience, it's returned as a SpanOptions object to allow it to be
  100. // passed directly to the wrapping helper methods in this package such as
  101. // SpanWrapFunc.
  102. func ContainerOptions(container moby.Container) SpanOptions {
  103. attrs := []attribute.KeyValue{
  104. attribute.String("container.id", container.ID),
  105. attribute.String("container.image", container.Image),
  106. unixTimeAttr("container.created_at", container.Created),
  107. }
  108. if len(container.Names) != 0 {
  109. attrs = append(attrs, attribute.String("container.name", strings.TrimPrefix(container.Names[0], "/")))
  110. }
  111. return []trace.SpanStartEventOption{
  112. trace.WithAttributes(attrs...),
  113. }
  114. }
  115. func keys[T any](m map[string]T) []string {
  116. out := make([]string, 0, len(m))
  117. for k := range m {
  118. out = append(out, k)
  119. }
  120. return out
  121. }
  122. func timeAttr(key string, value time.Time) attribute.KeyValue {
  123. return attribute.String(key, value.Format(time.RFC3339))
  124. }
  125. func unixTimeAttr(key string, value int64) attribute.KeyValue {
  126. return timeAttr(key, time.Unix(value, 0).UTC())
  127. }
  128. func flattenIncludeReferences(includeRefs map[string][]types.IncludeConfig) []string {
  129. ret := utils.NewSet[string]()
  130. for _, included := range includeRefs {
  131. for i := range included {
  132. ret.AddAll(included[i].Path...)
  133. }
  134. }
  135. return ret.Elements()
  136. }