attributes.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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/compose-spec/compose-go/types"
  18. moby "github.com/docker/docker/api/types"
  19. "go.opentelemetry.io/otel/attribute"
  20. "go.opentelemetry.io/otel/trace"
  21. )
  22. // SpanOptions is a small helper type to make it easy to share the options helpers between
  23. // downstream functions that accept slices of trace.SpanStartOption and trace.EventOption.
  24. type SpanOptions []trace.SpanStartEventOption
  25. func (s SpanOptions) SpanStartOptions() []trace.SpanStartOption {
  26. out := make([]trace.SpanStartOption, len(s))
  27. for i := range s {
  28. out[i] = s[i]
  29. }
  30. return out
  31. }
  32. func (s SpanOptions) EventOptions() []trace.EventOption {
  33. out := make([]trace.EventOption, len(s))
  34. for i := range s {
  35. out[i] = s[i]
  36. }
  37. return out
  38. }
  39. // ProjectOptions returns common attributes from a Compose project.
  40. //
  41. // For convenience, it's returned as a SpanOptions object to allow it to be
  42. // passed directly to the wrapping helper methods in this package such as
  43. // SpanWrapFunc.
  44. func ProjectOptions(proj *types.Project) SpanOptions {
  45. if proj == nil {
  46. return nil
  47. }
  48. disabledServiceNames := make([]string, len(proj.DisabledServices))
  49. for i := range proj.DisabledServices {
  50. disabledServiceNames[i] = proj.DisabledServices[i].Name
  51. }
  52. attrs := []attribute.KeyValue{
  53. attribute.String("project.name", proj.Name),
  54. attribute.String("project.dir", proj.WorkingDir),
  55. attribute.StringSlice("project.compose_files", proj.ComposeFiles),
  56. attribute.StringSlice("project.services.active", proj.ServiceNames()),
  57. attribute.StringSlice("project.services.disabled", disabledServiceNames),
  58. attribute.StringSlice("project.profiles", proj.Profiles),
  59. attribute.StringSlice("project.volumes", proj.VolumeNames()),
  60. attribute.StringSlice("project.networks", proj.NetworkNames()),
  61. attribute.StringSlice("project.secrets", proj.SecretNames()),
  62. attribute.StringSlice("project.configs", proj.ConfigNames()),
  63. attribute.StringSlice("project.extensions", keys(proj.Extensions)),
  64. }
  65. return []trace.SpanStartEventOption{
  66. trace.WithAttributes(attrs...),
  67. }
  68. }
  69. // ServiceOptions returns common attributes from a Compose service.
  70. //
  71. // For convenience, it's returned as a SpanOptions object to allow it to be
  72. // passed directly to the wrapping helper methods in this package such as
  73. // SpanWrapFunc.
  74. func ServiceOptions(service types.ServiceConfig) SpanOptions {
  75. attrs := []attribute.KeyValue{
  76. attribute.String("service.name", service.Name),
  77. attribute.String("service.image", service.Image),
  78. attribute.StringSlice("service.networks", keys(service.Networks)),
  79. }
  80. configNames := make([]string, len(service.Configs))
  81. for i := range service.Configs {
  82. configNames[i] = service.Configs[i].Source
  83. }
  84. attrs = append(attrs, attribute.StringSlice("service.configs", configNames))
  85. secretNames := make([]string, len(service.Secrets))
  86. for i := range service.Secrets {
  87. secretNames[i] = service.Secrets[i].Source
  88. }
  89. attrs = append(attrs, attribute.StringSlice("service.secrets", secretNames))
  90. volNames := make([]string, len(service.Volumes))
  91. for i := range service.Volumes {
  92. volNames[i] = service.Volumes[i].Source
  93. }
  94. attrs = append(attrs, attribute.StringSlice("service.volumes", volNames))
  95. return []trace.SpanStartEventOption{
  96. trace.WithAttributes(attrs...),
  97. }
  98. }
  99. // ContainerOptions returns common attributes from a Moby container.
  100. //
  101. // For convenience, it's returned as a SpanOptions object to allow it to be
  102. // passed directly to the wrapping helper methods in this package such as
  103. // SpanWrapFunc.
  104. func ContainerOptions(container moby.Container) SpanOptions {
  105. attrs := []attribute.KeyValue{
  106. attribute.String("container.id", container.ID),
  107. attribute.String("container.image", container.Image),
  108. unixTimeAttr("container.created_at", container.Created),
  109. }
  110. if len(container.Names) != 0 {
  111. attrs = append(attrs, attribute.String("container.name", strings.TrimPrefix(container.Names[0], "/")))
  112. }
  113. return []trace.SpanStartEventOption{
  114. trace.WithAttributes(attrs...),
  115. }
  116. }
  117. func keys[T any](m map[string]T) []string {
  118. out := make([]string, 0, len(m))
  119. for k := range m {
  120. out = append(out, k)
  121. }
  122. return out
  123. }
  124. func timeAttr(key string, value time.Time) attribute.KeyValue {
  125. return attribute.String(key, value.Format(time.RFC3339))
  126. }
  127. func unixTimeAttr(key string, value int64) attribute.KeyValue {
  128. return timeAttr(key, time.Unix(value, 0).UTC())
  129. }