Skip to content

Commit 5159058

Browse files
authored
Merge pull request docker#10831 from milas/instrument-up
trace: instrument `compose up` at a high-level
2 parents 3b2f3cd + 1ae191a commit 5159058

File tree

5 files changed

+295
-28
lines changed

5 files changed

+295
-28
lines changed

internal/tracing/attributes.go

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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+
}

internal/tracing/wrap.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
"context"
21+
22+
"go.opentelemetry.io/otel/codes"
23+
semconv "go.opentelemetry.io/otel/semconv/v1.18.0"
24+
"go.opentelemetry.io/otel/trace"
25+
)
26+
27+
// SpanWrapFunc wraps a function that takes a context with a trace.Span, marking the status as codes.Error if the
28+
// wrapped function returns an error.
29+
//
30+
// The context passed to the function is created from the span to ensure correct propagation.
31+
//
32+
// NOTE: This function is nearly identical to SpanWrapFuncForErrGroup, except the latter is designed specially for
33+
// convenience with errgroup.Group due to its prevalence throughout the codebase. The code is duplicated to avoid
34+
// adding even more levels of function wrapping/indirection.
35+
func SpanWrapFunc(spanName string, opts SpanOptions, fn func(ctx context.Context) error) func(context.Context) error {
36+
return func(ctx context.Context) error {
37+
ctx, span := Tracer.Start(ctx, spanName, opts.SpanStartOptions()...)
38+
defer span.End()
39+
40+
if err := fn(ctx); err != nil {
41+
span.SetStatus(codes.Error, err.Error())
42+
return err
43+
}
44+
45+
span.SetStatus(codes.Ok, "")
46+
return nil
47+
}
48+
}
49+
50+
// SpanWrapFuncForErrGroup wraps a function that takes a context with a trace.Span, marking the status as codes.Error
51+
// if the wrapped function returns an error.
52+
//
53+
// The context passed to the function is created from the span to ensure correct propagation.
54+
//
55+
// NOTE: This function is nearly identical to SpanWrapFunc, except this function is designed specially for
56+
// convenience with errgroup.Group due to its prevalence throughout the codebase. The code is duplicated to avoid
57+
// adding even more levels of function wrapping/indirection.
58+
func SpanWrapFuncForErrGroup(ctx context.Context, spanName string, opts SpanOptions, fn func(ctx context.Context) error) func() error {
59+
return func() error {
60+
ctx, span := Tracer.Start(ctx, spanName, opts.SpanStartOptions()...)
61+
defer span.End()
62+
63+
if err := fn(ctx); err != nil {
64+
span.SetStatus(codes.Error, err.Error())
65+
return err
66+
}
67+
68+
span.SetStatus(codes.Ok, "")
69+
return nil
70+
}
71+
}
72+
73+
// EventWrapFuncForErrGroup invokes a function and records an event, optionally including the returned
74+
// error as the "exception message" on the event.
75+
//
76+
// This is intended for lightweight usage to wrap errgroup.Group calls where a full span is not desired.
77+
func EventWrapFuncForErrGroup(ctx context.Context, eventName string, opts SpanOptions, fn func(ctx context.Context) error) func() error {
78+
return func() error {
79+
span := trace.SpanFromContext(ctx)
80+
eventOpts := opts.EventOptions()
81+
82+
err := fn(ctx)
83+
84+
if err != nil {
85+
eventOpts = append(eventOpts, trace.WithAttributes(semconv.ExceptionMessage(err.Error())))
86+
}
87+
span.AddEvent(eventName, eventOpts...)
88+
89+
return err
90+
}
91+
}

pkg/compose/build.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"os"
2323
"path/filepath"
2424

25+
"github.com/docker/compose/v2/internal/tracing"
26+
2527
"github.com/docker/buildx/controller/pb"
2628

2729
"github.com/compose-spec/compose-go/types"
@@ -170,7 +172,11 @@ func (s *composeService) ensureImagesExists(ctx context.Context, project *types.
170172
return err
171173
}
172174

173-
err = s.pullRequiredImages(ctx, project, images, quietPull)
175+
err = tracing.SpanWrapFunc("project/pull", tracing.ProjectOptions(project),
176+
func(ctx context.Context) error {
177+
return s.pullRequiredImages(ctx, project, images, quietPull)
178+
},
179+
)(ctx)
174180
if err != nil {
175181
return err
176182
}
@@ -186,16 +192,24 @@ func (s *composeService) ensureImagesExists(ctx context.Context, project *types.
186192
}
187193

188194
if buildRequired {
189-
builtImages, err := s.build(ctx, project, api.BuildOptions{
190-
Progress: mode,
191-
})
195+
err = tracing.SpanWrapFunc("project/build", tracing.ProjectOptions(project),
196+
func(ctx context.Context) error {
197+
builtImages, err := s.build(ctx, project, api.BuildOptions{
198+
Progress: mode,
199+
})
200+
if err != nil {
201+
return err
202+
}
203+
204+
for name, digest := range builtImages {
205+
images[name] = digest
206+
}
207+
return nil
208+
},
209+
)(ctx)
192210
if err != nil {
193211
return err
194212
}
195-
196-
for name, digest := range builtImages {
197-
images[name] = digest
198-
}
199213
}
200214

201215
// set digest as com.docker.compose.image label so we can detect outdated containers

0 commit comments

Comments
 (0)