attributes.go 6.3 KB

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