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