attributes.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. "github.com/docker/docker/api/types/container"
  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.models", proj.ModelNames()),
  69. attribute.StringSlice("project.extensions", keys(proj.Extensions)),
  70. attribute.StringSlice("project.services.active", proj.ServiceNames()),
  71. attribute.StringSlice("project.services.disabled", proj.DisabledServiceNames()),
  72. attribute.StringSlice("project.services.build", proj.ServicesWithBuild()),
  73. attribute.StringSlice("project.services.depends_on", proj.ServicesWithDependsOn()),
  74. attribute.StringSlice("project.services.models", proj.ServicesWithModels()),
  75. attribute.StringSlice("project.services.capabilities", capabilities),
  76. attribute.StringSlice("project.services.capabilities.gpu", gpu),
  77. attribute.StringSlice("project.services.capabilities.tpu", tpu),
  78. }
  79. if metrics, ok := ctx.Value(MetricsKey{}).(Metrics); ok {
  80. attrs = append(attrs, attribute.Int("project.services.extends", metrics.CountExtends))
  81. attrs = append(attrs, attribute.Int("project.includes.local", metrics.CountIncludesLocal))
  82. attrs = append(attrs, attribute.Int("project.includes.remote", metrics.CountIncludesRemote))
  83. }
  84. if projHash, ok := projectHash(proj); ok {
  85. attrs = append(attrs, attribute.String("project.hash", projHash))
  86. }
  87. return []trace.SpanStartEventOption{
  88. trace.WithAttributes(attrs...),
  89. }
  90. }
  91. // ServiceOptions returns common attributes from a Compose service.
  92. //
  93. // For convenience, it's returned as a SpanOptions object to allow it to be
  94. // passed directly to the wrapping helper methods in this package such as
  95. // SpanWrapFunc.
  96. func ServiceOptions(service types.ServiceConfig) SpanOptions {
  97. attrs := []attribute.KeyValue{
  98. attribute.String("service.name", service.Name),
  99. attribute.String("service.image", service.Image),
  100. attribute.StringSlice("service.networks", keys(service.Networks)),
  101. attribute.StringSlice("service.models", keys(service.Models)),
  102. }
  103. configNames := make([]string, len(service.Configs))
  104. for i := range service.Configs {
  105. configNames[i] = service.Configs[i].Source
  106. }
  107. attrs = append(attrs, attribute.StringSlice("service.configs", configNames))
  108. secretNames := make([]string, len(service.Secrets))
  109. for i := range service.Secrets {
  110. secretNames[i] = service.Secrets[i].Source
  111. }
  112. attrs = append(attrs, attribute.StringSlice("service.secrets", secretNames))
  113. volNames := make([]string, len(service.Volumes))
  114. for i := range service.Volumes {
  115. volNames[i] = service.Volumes[i].Source
  116. }
  117. attrs = append(attrs, attribute.StringSlice("service.volumes", volNames))
  118. return []trace.SpanStartEventOption{
  119. trace.WithAttributes(attrs...),
  120. }
  121. }
  122. // ContainerOptions returns common attributes from a Moby container.
  123. //
  124. // For convenience, it's returned as a SpanOptions object to allow it to be
  125. // passed directly to the wrapping helper methods in this package such as
  126. // SpanWrapFunc.
  127. func ContainerOptions(ctr container.Summary) SpanOptions {
  128. attrs := []attribute.KeyValue{
  129. attribute.String("container.id", ctr.ID),
  130. attribute.String("container.image", ctr.Image),
  131. unixTimeAttr("container.created_at", ctr.Created),
  132. }
  133. if len(ctr.Names) != 0 {
  134. attrs = append(attrs, attribute.String("container.name", strings.TrimPrefix(ctr.Names[0], "/")))
  135. }
  136. return []trace.SpanStartEventOption{
  137. trace.WithAttributes(attrs...),
  138. }
  139. }
  140. func keys[T any](m map[string]T) []string {
  141. out := make([]string, 0, len(m))
  142. for k := range m {
  143. out = append(out, k)
  144. }
  145. return out
  146. }
  147. func timeAttr(key string, value time.Time) attribute.KeyValue {
  148. return attribute.String(key, value.Format(time.RFC3339))
  149. }
  150. func unixTimeAttr(key string, value int64) attribute.KeyValue {
  151. return timeAttr(key, time.Unix(value, 0).UTC())
  152. }
  153. // projectHash returns a checksum from the JSON encoding of the project.
  154. func projectHash(p *types.Project) (string, bool) {
  155. if p == nil {
  156. return "", false
  157. }
  158. // disabled services aren't included in the output, so make a copy with
  159. // all the services active for hashing
  160. var err error
  161. p, err = p.WithServicesEnabled(append(p.ServiceNames(), p.DisabledServiceNames()...)...)
  162. if err != nil {
  163. return "", false
  164. }
  165. projData, err := json.Marshal(p)
  166. if err != nil {
  167. return "", false
  168. }
  169. d := sha256.Sum256(projData)
  170. return fmt.Sprintf("%x", d), true
  171. }