attributes.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. "context"
  16. "crypto/sha256"
  17. "encoding/json"
  18. "fmt"
  19. "strings"
  20. "time"
  21. "github.com/docker/compose/v2/pkg/utils"
  22. "github.com/compose-spec/compose-go/v2/types"
  23. moby "github.com/docker/docker/api/types"
  24. "go.opentelemetry.io/otel/attribute"
  25. "go.opentelemetry.io/otel/trace"
  26. )
  27. // SpanOptions is a small helper type to make it easy to share the options helpers between
  28. // downstream functions that accept slices of trace.SpanStartOption and trace.EventOption.
  29. type SpanOptions []trace.SpanStartEventOption
  30. type Metrics struct {
  31. CountExtends int
  32. }
  33. func (s SpanOptions) SpanStartOptions() []trace.SpanStartOption {
  34. out := make([]trace.SpanStartOption, len(s))
  35. for i := range s {
  36. out[i] = s[i]
  37. }
  38. return out
  39. }
  40. func (s SpanOptions) EventOptions() []trace.EventOption {
  41. out := make([]trace.EventOption, len(s))
  42. for i := range s {
  43. out[i] = s[i]
  44. }
  45. return out
  46. }
  47. // ProjectOptions returns common attributes from a Compose project.
  48. //
  49. // For convenience, it's returned as a SpanOptions object to allow it to be
  50. // passed directly to the wrapping helper methods in this package such as
  51. // SpanWrapFunc.
  52. func ProjectOptions(ctx context.Context, proj *types.Project) SpanOptions {
  53. if proj == nil {
  54. return nil
  55. }
  56. capabilities, gpu, tpu := proj.ServicesWithCapabilities()
  57. attrs := []attribute.KeyValue{
  58. attribute.String("project.name", proj.Name),
  59. attribute.String("project.dir", proj.WorkingDir),
  60. attribute.StringSlice("project.compose_files", proj.ComposeFiles),
  61. attribute.StringSlice("project.profiles", proj.Profiles),
  62. attribute.StringSlice("project.volumes", proj.VolumeNames()),
  63. attribute.StringSlice("project.networks", proj.NetworkNames()),
  64. attribute.StringSlice("project.secrets", proj.SecretNames()),
  65. attribute.StringSlice("project.configs", proj.ConfigNames()),
  66. attribute.StringSlice("project.extensions", keys(proj.Extensions)),
  67. attribute.StringSlice("project.includes", flattenIncludeReferences(proj.IncludeReferences)),
  68. attribute.StringSlice("project.services.active", proj.ServiceNames()),
  69. attribute.StringSlice("project.services.disabled", proj.DisabledServiceNames()),
  70. attribute.StringSlice("project.services.build", proj.ServicesWithBuild()),
  71. attribute.StringSlice("project.services.depends_on", proj.ServicesWithDependsOn()),
  72. attribute.StringSlice("project.services.capabilities", capabilities),
  73. attribute.StringSlice("project.services.capabilities.gpu", gpu),
  74. attribute.StringSlice("project.services.capabilities.tpu", tpu),
  75. }
  76. if metrics, ok := ctx.Value(Metrics{}).(Metrics); ok {
  77. attrs = append(attrs, attribute.Int("project.services.extends", metrics.CountExtends))
  78. }
  79. if projHash, ok := projectHash(proj); ok {
  80. attrs = append(attrs, attribute.String("project.hash", projHash))
  81. }
  82. return []trace.SpanStartEventOption{
  83. trace.WithAttributes(attrs...),
  84. }
  85. }
  86. // ServiceOptions returns common attributes from a Compose service.
  87. //
  88. // For convenience, it's returned as a SpanOptions object to allow it to be
  89. // passed directly to the wrapping helper methods in this package such as
  90. // SpanWrapFunc.
  91. func ServiceOptions(service types.ServiceConfig) SpanOptions {
  92. attrs := []attribute.KeyValue{
  93. attribute.String("service.name", service.Name),
  94. attribute.String("service.image", service.Image),
  95. attribute.StringSlice("service.networks", keys(service.Networks)),
  96. }
  97. configNames := make([]string, len(service.Configs))
  98. for i := range service.Configs {
  99. configNames[i] = service.Configs[i].Source
  100. }
  101. attrs = append(attrs, attribute.StringSlice("service.configs", configNames))
  102. secretNames := make([]string, len(service.Secrets))
  103. for i := range service.Secrets {
  104. secretNames[i] = service.Secrets[i].Source
  105. }
  106. attrs = append(attrs, attribute.StringSlice("service.secrets", secretNames))
  107. volNames := make([]string, len(service.Volumes))
  108. for i := range service.Volumes {
  109. volNames[i] = service.Volumes[i].Source
  110. }
  111. attrs = append(attrs, attribute.StringSlice("service.volumes", volNames))
  112. return []trace.SpanStartEventOption{
  113. trace.WithAttributes(attrs...),
  114. }
  115. }
  116. // ContainerOptions returns common attributes from a Moby container.
  117. //
  118. // For convenience, it's returned as a SpanOptions object to allow it to be
  119. // passed directly to the wrapping helper methods in this package such as
  120. // SpanWrapFunc.
  121. func ContainerOptions(container moby.Container) SpanOptions {
  122. attrs := []attribute.KeyValue{
  123. attribute.String("container.id", container.ID),
  124. attribute.String("container.image", container.Image),
  125. unixTimeAttr("container.created_at", container.Created),
  126. }
  127. if len(container.Names) != 0 {
  128. attrs = append(attrs, attribute.String("container.name", strings.TrimPrefix(container.Names[0], "/")))
  129. }
  130. return []trace.SpanStartEventOption{
  131. trace.WithAttributes(attrs...),
  132. }
  133. }
  134. func keys[T any](m map[string]T) []string {
  135. out := make([]string, 0, len(m))
  136. for k := range m {
  137. out = append(out, k)
  138. }
  139. return out
  140. }
  141. func timeAttr(key string, value time.Time) attribute.KeyValue {
  142. return attribute.String(key, value.Format(time.RFC3339))
  143. }
  144. func unixTimeAttr(key string, value int64) attribute.KeyValue {
  145. return timeAttr(key, time.Unix(value, 0).UTC())
  146. }
  147. func flattenIncludeReferences(includeRefs map[string][]types.IncludeConfig) []string {
  148. ret := utils.NewSet[string]()
  149. for _, included := range includeRefs {
  150. for i := range included {
  151. ret.AddAll(included[i].Path...)
  152. }
  153. }
  154. return ret.Elements()
  155. }
  156. // projectHash returns a checksum from the JSON encoding of the project.
  157. func projectHash(p *types.Project) (string, bool) {
  158. if p == nil {
  159. return "", false
  160. }
  161. // disabled services aren't included in the output, so make a copy with
  162. // all the services active for hashing
  163. var err error
  164. p, err = p.WithServicesEnabled(append(p.ServiceNames(), p.DisabledServiceNames()...)...)
  165. if err != nil {
  166. return "", false
  167. }
  168. projData, err := json.Marshal(p)
  169. if err != nil {
  170. return "", false
  171. }
  172. d := sha256.Sum256(projData)
  173. return fmt.Sprintf("%x", d), true
  174. }