Skip to content

Commit

Permalink
skaffold trace wrapping of critical functions and skaffold trace expo…
Browse files Browse the repository at this point in the history
…rters (#5854)

What is the problem being solved?
Part of #5756, adding opentelemetry trace information to skaffold commands. Added trace information to specific performance critical skaffold functions (identified in go/cloud-trace-skaffold).  Also added 4 trace exporters - gcp-skaffold, gcp-adc, stdout, and jaeger.  This PR uses env var based enabling/disabling for the trace for simplicity and to hide it from users directly.

Why is this the best approach?
Using opentelemetry tracing is the obvious choice as we use open telemetry libs for metrics and it is becoming the metrics/tracing standard.  Using an env var in this PR and later integrating the flag setup was considered optimal as currently skaffold tracing will be used for benchmarking/bottleneck-identifying for select use cases while the user facing UX w/ jaeger, etc. is still being worked out.

What other approaches did you consider?
There was the possibility of building tracing directly into skaffold events but I think with the current wrapper setup in pkg/skaffold/instrumentation/trace.go (w/ the minimal code required) and the fact that many trace locations will not be event locations  (eg: how long to hash a file, etc.) it makes sense to not integrate them.

What side effects will this approach have?
There shouldn't be any side effects w/ this approach as the default "off" for tracing and the minimal user visibility for now should mean that it used only for select use cases experimentally.  I have done timing tests with the no-op/empty trace (SKAFFOLD_TRACE unset) and it does not change the performance of skaffold.

What future work remains to be done?
Future work includes wiring up a --trace flag through dev, build, deploy, etc. and working on how skaffold might be able to do distributed tracing w/ other tools (minikube, buildpacks, etc.).  Additionally the ability to allow for more sporadic sampling (vs AlwaysSample) should be added.  Some future work mentioned in PR review included:
- OTEL_TRACES_EXPORTER=* support (vs SKAFFOLD_TRACE)
  • Loading branch information
aaron-prindle authored Jun 2, 2021
1 parent 53b0780 commit f95999f
Show file tree
Hide file tree
Showing 34 changed files with 888 additions and 42 deletions.
3 changes: 1 addition & 2 deletions cmd/skaffold/app/cmd/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,12 @@ func createNewRunner(out io.Writer, opts config.SkaffoldOptions) (runner.Runner,
return nil, nil, nil, err
}

instrumentation.InitMeterFromConfig(configs, opts.User)
instrumentation.Init(configs, opts.User)
runner, err := v1.NewForConfig(runCtx)
if err != nil {
event.InititializationFailed(err)
return nil, nil, nil, fmt.Errorf("creating runner: %w", err)
}

return runner, configs, runCtx, nil
}

Expand Down
4 changes: 1 addition & 3 deletions cmd/skaffold/skaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ func main() {
code = exitCode(err)
}
}
if err := instrumentation.ExportMetrics(code); err != nil {
logrus.Debugf("error exporting metrics %v", err)
}
instrumentation.ShutdownAndFlush(context.Background(), code)
os.Exit(code)
}

Expand Down
52 changes: 52 additions & 0 deletions examples/jaeger-skaffold-trace/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
### Example: Skaffold Command Tracing with Jaeger


_**WARNING: Skaffold's trace functionality is experimental and may change without notice.**_

In this example:

* Use Skaffold to deploy a local/remote Jaeger instance
* Enable Skaffold tracing functionality to get trace information about skaffold `dev`, `build`, `deploy`, etc. timings
* Send Skaffold trace information to Jaeger and have that information be visible in the Jaeger UI,

In this example, we'll walk through enabling Skaffold trace information that can be used to explore performance bottlenecks and to get a more in depth view of user's local dev loop.

_**WARNING: If you're running this on a cloud cluster, this example will create a service and expose a webserver.
It's highly recommended that you only run this example on a local, private cluster like minikube or Kubernetes in Docker for Desktop.**_

#### Setting up Jaeger locally

Use docker to start a local jaeger instance using the Jaeger project's [all-in-one docker setup](https://www.jaegertracing.io/docs/getting-started/#all-in-one):
```bash
docker run -d --name jaeger \
-e COLLECTOR_ZIPKIN_HOST_PORT=:9411 \
-p 5775:5775/udp \
-p 6831:6831/udp \
-p 6832:6832/udp \
-p 5778:5778 \
-p 16686:16686 \
-p 14268:14268 \
-p 14250:14250 \
-p 9411:9411 \
jaegertracing/all-in-one:1.22
```

Now, in a different terminal, go to another Skaffold example (eg: microservices), enable SKAFFOLD_TRACE with Jaeger and start dev session there:
```bash
cd ../microservices
export SKAFFOLD_TRACE=jaeger
skaffold dev
```

Now go to the Jaeger UI that Skaffold will port-forward to localhost at http://127.0.0.1:16686/

Select service:`skaffold-trace` in the left bar and then click `Find Traces` on the bottom of the left bar.

From here you should be able to view all of the relevant traces

#### Cleaning up
To cleanup Jaeger all-in-one setup, run the following:
```
docker kill jaeger # stops the running jaeger container
docker rm jaeger #removes the container image
```
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ require (
cloud.google.com/go/storage v1.10.0
github.com/AlecAivazis/survey/v2 v2.2.7
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.20.0
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace v0.20.0
github.com/aws/aws-sdk-go v1.36.30 // indirect
github.com/blang/semver v3.5.1+incompatible
github.com/bmatcuk/doublestar v1.2.4
github.com/buildpacks/imgutil v0.0.0-20210209163614-30601e371ce3
github.com/buildpacks/lifecycle v0.10.2
github.com/buildpacks/pack v0.18.1
github.com/cenkalti/backoff/v4 v4.0.2
github.com/daixiang0/gci v0.2.8 // indirect
github.com/docker/cli v20.10.0-beta1.0.20201117192004-5cc239616494+incompatible
github.com/docker/distribution v2.7.1+incompatible
github.com/docker/docker v20.10.0-beta1.0.20201110211921-af34b94a78a1+incompatible
Expand Down Expand Up @@ -66,8 +66,10 @@ require (
github.com/spf13/pflag v1.0.5
github.com/tektoncd/pipeline v0.5.1-0.20190731183258-9d7e37e85bf8
github.com/xeipuuv/gojsonschema v1.2.0
go.opencensus.io v0.22.5
go.opentelemetry.io/otel v0.20.0
go.opentelemetry.io/otel/exporters/stdout v0.20.0
go.opentelemetry.io/otel/exporters/trace/jaeger v0.20.0 // indirect
go.opentelemetry.io/otel/metric v0.20.0
go.opentelemetry.io/otel/sdk v0.20.0 // indirect
go.opentelemetry.io/otel/sdk/metric v0.20.0
Expand Down
17 changes: 14 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ cloud.google.com/go v0.53.0/go.mod h1:fp/UouUEsRkN6ryDKNW/Upv/JBKnv6WDthjR6+vze6
cloud.google.com/go v0.54.0/go.mod h1:1rq2OEkV3YMf6n/9ZvGWI3GWw0VoqH/1x2nd8Is/bPc=
cloud.google.com/go v0.56.0/go.mod h1:jr7tqZxxKOVYizybht9+26Z/gUq7tiRzu+ACVAMbKVk=
cloud.google.com/go v0.57.0/go.mod h1:oXiQ6Rzq3RAkkY7N6t3TcE6jE+CIBBbA36lwQ1JyzZs=
cloud.google.com/go v0.61.0/go.mod h1:XukKJg4Y7QsUu0Hxg3qQKUWR4VuWivmyMK2+rUyxAqw=
cloud.google.com/go v0.62.0/go.mod h1:jmCYTdRCQuc1PHIIJ/maLInMho30T/Y0M4hTdTShOYc=
cloud.google.com/go v0.65.0/go.mod h1:O5N8zS7uWy9vkA9vayVHs65eM1ubvY4h553ofrNHObY=
cloud.google.com/go v0.72.0 h1:eWRCuwubtDrCJG0oSUMgnsbD4CmPFQF2ei4OFbXvwww=
Expand Down Expand Up @@ -132,6 +133,8 @@ github.com/GoogleCloudPlatform/k8s-cloud-provider v0.0.0-20190822182118-27a4ced3
github.com/GoogleCloudPlatform/k8s-cloud-provider v0.0.0-20200415212048-7901bc822317/go.mod h1:DF8FZRxMHMGv/vP2lQP6h+dYzzjpuRn24VeRiYn3qjQ=
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.20.0 h1:H4Hs0jLf0IcQ96dgx3gPFRke2zyjrzpYMecVsrWKrAc=
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.20.0/go.mod h1:3DfYfW/GJ2p+Yd1vGImcFO1jKaqvZmAMMIdWk8BJsjw=
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace v0.20.0 h1:ExGRyJwOUijAPv/RzCJ3p1CNUxBQGzVO238m1lFjLS4=
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace v0.20.0/go.mod h1:f4BFp2+kV6s/OKj3IP/34keB/OE7tTTaZZQyX/mQ7Ng=
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/MakeNowJust/heredoc v0.0.0-20170808103936-bb23615498cd h1:sjQovDkwrZp8u+gxLtPgKGjk5hCxuy2hrRejBTA9xFU=
github.com/MakeNowJust/heredoc v0.0.0-20170808103936-bb23615498cd/go.mod h1:64YHyfSL2R96J44Nlwm39UHepQbyR5q10x7iYa1ks2E=
Expand Down Expand Up @@ -355,8 +358,6 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/creack/pty v1.1.11 h1:07n33Z8lZxZ2qwegKbObQohDhXDQxiMMz1NOUGYlesw=
github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/cyphar/filepath-securejoin v0.2.2/go.mod h1:FpkQEhXnPnOthhzymB7CGsFk2G9VLXONKD9G7QGMM+4=
github.com/daixiang0/gci v0.2.8 h1:1mrIGMBQsBu0P7j7m1M8Lb+ZeZxsZL+jyGX4YoMJJpg=
github.com/daixiang0/gci v0.2.8/go.mod h1:+4dZ7TISfSmqfAGv59ePaHfNzgGtIkHAhhdKggP1JAc=
github.com/davecgh/go-spew v0.0.0-20151105211317-5215b55f46b2/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down Expand Up @@ -435,6 +436,8 @@ github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d/go.mod h1:ZZM
github.com/fatih/camelcase v1.0.0/go.mod h1:yN2Sb0lFhZJUdVvtELVWefmrXpuZESvPmqwoZc+/fpc=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ=
github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BHsljHzVlRcyQhjrss6TZTdY2VfCqZPbv5k3iBFa2ZQ=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/form3tech-oss/jwt-go v3.2.2+incompatible h1:TcekIExNqud5crz4xD2pavyTgWiPvpYe4Xau31I0PRk=
Expand Down Expand Up @@ -1351,10 +1354,16 @@ go.opencensus.io v0.22.4-0.20200608061201-1901b56b9515/go.mod h1:yxeiOL68Rb0Xd1d
go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.5 h1:dntmOdLpSpHlVqbW5Eay97DelsZHe+55D+xC6i0dDS0=
go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk=
go.opentelemetry.io/contrib v0.20.0 h1:ubFQUn0VCZ0gPwIoJfBJVpeBlyRMxu8Mm/huKWYd9p0=
go.opentelemetry.io/contrib v0.20.0/go.mod h1:G/EtFaa6qaN7+LxqfIAT3GiZa7Wv5DTBUzl5H4LY0Kc=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.20.0 h1:Q3C9yzW6I9jqEc8sawxzxZmY48fs9u220KXq6d5s3XU=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.20.0/go.mod h1:2AboqHi0CiIZU0qwhtUfCYD1GeUzvvIXWNkhDt7ZMG4=
go.opentelemetry.io/otel v0.20.0 h1:eaP0Fqu7SXHwvjiqDq83zImeehOHX8doTvU9AwXON8g=
go.opentelemetry.io/otel v0.20.0/go.mod h1:Y3ugLH2oa81t5QO+Lty+zXf8zC9L26ax4Nzoxm/dooo=
go.opentelemetry.io/otel/exporters/stdout v0.20.0 h1:NXKkOWV7Np9myYrQE0wqRS3SbwzbupHu07rDONKubMo=
go.opentelemetry.io/otel/exporters/stdout v0.20.0/go.mod h1:t9LUU3JvYlmoPA61abhvsXxKh58xdyi3nMtI6JiR8v0=
go.opentelemetry.io/otel/exporters/trace/jaeger v0.20.0 h1:FoclOadJNul1vUiKnZU0sKFWOZtZQq3jUzSbrX2jwNM=
go.opentelemetry.io/otel/exporters/trace/jaeger v0.20.0/go.mod h1:10qwvAmKpvwRO5lL3KQ8EWznPp89uGfhcbK152LFWsQ=
go.opentelemetry.io/otel/metric v0.20.0 h1:4kzhXFP+btKm4jwxpjIqjs41A7MakRFUS86bqLHTIw8=
go.opentelemetry.io/otel/metric v0.20.0/go.mod h1:598I5tYlH1vzBjn+BTuhzTCSb/9debfNp6R3s7Pr1eU=
go.opentelemetry.io/otel/oteltest v0.20.0 h1:HiITxCawalo5vQzdHfKeZurV8x7ljcqAgiWzF6Vaeaw=
Expand Down Expand Up @@ -1732,13 +1741,13 @@ golang.org/x/tools v0.0.0-20200515010526-7d3b6ebf133d/go.mod h1:EkVYQZoAsY45+roY
golang.org/x/tools v0.0.0-20200616133436-c1934b75d054/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200618134242-20370b0cb4b2/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200701151220-7cb253f4c4f8/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200713011307-fd294ab11aed/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20200729194436-6467de6f59a7/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20200904185747-39188db58858/go.mod h1:Cj7w3i3Rnn0Xh82ur9kSqwfTHTeVxaDqrfMjpcNT6bE=
golang.org/x/tools v0.0.0-20200916195026-c9a70fc28ce3/go.mod h1:z6u4i615ZeAfBE4XtMziQW1fSVJXACjjbWkB/mvPzlU=
golang.org/x/tools v0.0.0-20201110124207-079ba7bd75cd/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.0.0-20201118003311-bd56c0adb394/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.0 h1:po9/4sTYwZU9lPhi1tOrb4hCv3qrhiQ77LZfGa2OjwY=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
Expand Down Expand Up @@ -1835,6 +1844,8 @@ google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEY
google.golang.org/genproto v0.0.0-20200527145253-8367513e4ece/go.mod h1:jDfRM7FcilCzHH/e9qn6dsT145K34l5v+OpcnNgKAAA=
google.golang.org/genproto v0.0.0-20200605102947-12044bf5ea91/go.mod h1:jDfRM7FcilCzHH/e9qn6dsT145K34l5v+OpcnNgKAAA=
google.golang.org/genproto v0.0.0-20200618031413-b414f8b61790/go.mod h1:jDfRM7FcilCzHH/e9qn6dsT145K34l5v+OpcnNgKAAA=
google.golang.org/genproto v0.0.0-20200711021454-869866162049/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
google.golang.org/genproto v0.0.0-20200715011427-11fb19a81f2c/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
google.golang.org/genproto v0.0.0-20200729003335-053ba62fc06f/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
google.golang.org/genproto v0.0.0-20200804131852-c06518451d9c/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
google.golang.org/genproto v0.0.0-20200825200019-8632dd797987/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
Expand Down
52 changes: 52 additions & 0 deletions integration/examples/jaeger-skaffold-trace/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
### Example: Skaffold Command Tracing with Jaeger


_**WARNING: Skaffold's trace functionality is experimental and may change without notice.**_

In this example:

* Use Skaffold to deploy a local/remote Jaeger instance
* Enable Skaffold tracing functionality to get trace information about skaffold `dev`, `build`, `deploy`, etc. timings
* Send Skaffold trace information to Jaeger and have that information be visible in the Jaeger UI,

In this example, we'll walk through enabling Skaffold trace information that can be used to explore performance bottlenecks and to get a more in depth view of user's local dev loop.

_**WARNING: If you're running this on a cloud cluster, this example will create a service and expose a webserver.
It's highly recommended that you only run this example on a local, private cluster like minikube or Kubernetes in Docker for Desktop.**_

#### Setting up Jaeger locally

Use docker to start a local jaeger instance using the Jaeger project's [all-in-one docker setup](https://www.jaegertracing.io/docs/getting-started/#all-in-one):
```bash
docker run -d --name jaeger \
-e COLLECTOR_ZIPKIN_HOST_PORT=:9411 \
-p 5775:5775/udp \
-p 6831:6831/udp \
-p 6832:6832/udp \
-p 5778:5778 \
-p 16686:16686 \
-p 14268:14268 \
-p 14250:14250 \
-p 9411:9411 \
jaegertracing/all-in-one:1.22
```

Now, in a different terminal, go to another Skaffold example (eg: microservices), enable SKAFFOLD_TRACE with Jaeger and start dev session there:
```bash
cd ../microservices
export SKAFFOLD_TRACE=jaeger
skaffold dev
```

Now go to the Jaeger UI that Skaffold will port-forward to localhost at http://127.0.0.1:16686/

Select service:`skaffold-trace` in the left bar and then click `Find Traces` on the bottom of the left bar.

From here you should be able to view all of the relevant traces

#### Cleaning up
To cleanup Jaeger all-in-one setup, run the following:
```
docker kill jaeger # stops the running jaeger container
docker rm jaeger #removes the container image
```
8 changes: 8 additions & 0 deletions pkg/skaffold/build/cache/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/graph"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/instrumentation"
latestV1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
)
Expand Down Expand Up @@ -66,14 +67,21 @@ func newArtifactHasher(artifacts graph.ArtifactGraph, lister DependencyLister, m
}

func (h *artifactHasherImpl) hash(ctx context.Context, a *latestV1.Artifact) (string, error) {
ctx, endTrace := instrumentation.StartTrace(ctx, "hash_GenerateHashOneArtifact", map[string]string{
"ImageName": instrumentation.PII(a.ImageName),
})
defer endTrace()

hash, err := h.safeHash(ctx, a)
if err != nil {
endTrace(instrumentation.TraceEndError(err))
return "", err
}
hashes := []string{hash}
for _, dep := range sortedDependencies(a, h.artifacts) {
depHash, err := h.hash(ctx, dep)
if err != nil {
endTrace(instrumentation.TraceEndError(err))
return "", err
}
hashes = append(hashes, depHash)
Expand Down
9 changes: 9 additions & 0 deletions pkg/skaffold/build/cache/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/sirupsen/logrus"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/instrumentation"
latestV1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/tag"
)
Expand All @@ -33,6 +34,9 @@ func (c *cache) lookupArtifacts(ctx context.Context, tags tag.ImageTags, artifac
details := make([]cacheDetails, len(artifacts))
// Create a new `artifactHasher` on every new dev loop.
// This way every artifact hash is calculated at most once in a single dev loop, and recalculated on every dev loop.

ctx, endTrace := instrumentation.StartTrace(ctx, "lookupArtifacts_CacheLookupArtifacts")
defer endTrace()
h := newArtifactHasherFunc(c.artifactGraph, c.lister, c.cfg.Mode())
var wg sync.WaitGroup
for i := range artifacts {
Expand All @@ -50,6 +54,11 @@ func (c *cache) lookupArtifacts(ctx context.Context, tags tag.ImageTags, artifac
}

func (c *cache) lookup(ctx context.Context, a *latestV1.Artifact, tag string, h artifactHasher) cacheDetails {
ctx, endTrace := instrumentation.StartTrace(ctx, "lookup_CacheLookupOneArtifact", map[string]string{
"ImageName": instrumentation.PII(a.ImageName),
})
defer endTrace()

hash, err := h.hash(ctx, a)
if err != nil {
return failed{err: fmt.Errorf("getting hash for artifact %q: %s", a.ImageName, err)}
Expand Down
11 changes: 11 additions & 0 deletions pkg/skaffold/build/cache/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
sErrors "github.com/GoogleContainerTools/skaffold/pkg/skaffold/errors"
eventV2 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/event/v2"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/graph"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/instrumentation"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/output"
latestV1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/tag"
Expand All @@ -42,6 +43,8 @@ func (c *cache) Build(ctx context.Context, out io.Writer, tags tag.ImageTags, ar
start := time.Now()

output.Default.Fprintln(out, "Checking cache...")
ctx, endTrace := instrumentation.StartTrace(ctx, "Build_CheckBuildCache")
defer endTrace()

lookup := make(chan []cacheDetails)
go func() { lookup <- c.lookupArtifacts(ctx, tags, artifacts) }()
Expand All @@ -64,6 +67,7 @@ func (c *cache) Build(ctx context.Context, out io.Writer, tags tag.ImageTags, ar
switch result := result.(type) {
case failed:
output.Red.Fprintln(out, "Error checking cache.")
endTrace(instrumentation.TraceEndError(result.err))
return nil, result.err

case needsBuilding:
Expand All @@ -77,20 +81,24 @@ func (c *cache) Build(ctx context.Context, out io.Writer, tags tag.ImageTags, ar
eventV2.CacheCheckHit(artifact.ImageName)
output.Green.Fprintln(out, "Found. Tagging")
if err := result.Tag(ctx, c); err != nil {
endTrace(instrumentation.TraceEndError(err))
return nil, fmt.Errorf("tagging image: %w", err)
}

case needsPushing:
eventV2.CacheCheckHit(artifact.ImageName)
output.Green.Fprintln(out, "Found. Pushing")
if err := result.Push(ctx, out, c); err != nil {
endTrace(instrumentation.TraceEndError(err))

return nil, fmt.Errorf("%s: %w", sErrors.PushImageErr, err)
}

default:
eventV2.CacheCheckHit(artifact.ImageName)
isLocal, err := c.isLocalImage(artifact.ImageName)
if err != nil {
endTrace(instrumentation.TraceEndError(err))
return nil, err
}
if isLocal {
Expand All @@ -109,12 +117,14 @@ func (c *cache) Build(ctx context.Context, out io.Writer, tags tag.ImageTags, ar
var uniqueTag string
isLocal, err := c.isLocalImage(artifact.ImageName)
if err != nil {
endTrace(instrumentation.TraceEndError(err))
return nil, err
}
if isLocal {
var err error
uniqueTag, err = build.TagWithImageID(ctx, tag, entry.ID, c.client)
if err != nil {
endTrace(instrumentation.TraceEndError(err))
return nil, err
}
} else {
Expand All @@ -131,6 +141,7 @@ func (c *cache) Build(ctx context.Context, out io.Writer, tags tag.ImageTags, ar

bRes, err := buildAndTest(ctx, out, tags, needToBuild)
if err != nil {
endTrace(instrumentation.TraceEndError(err))
return nil, err
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/skaffold/build/custom/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ import (
"io"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/instrumentation"
latestV1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1"
)

// Build builds an artifact using a custom script
func (b *Builder) Build(ctx context.Context, out io.Writer, artifact *latestV1.Artifact, tag string) (string, error) {
instrumentation.AddAttributesToCurrentSpanFromContext(ctx, map[string]string{
"BuildType": "custom",
"Context": instrumentation.PII(artifact.Workspace),
"Destination": instrumentation.PII(tag),
})
if err := b.runBuildScript(ctx, out, artifact, tag); err != nil {
return "", fmt.Errorf("building custom artifact: %w", err)
}
Expand Down
Loading

0 comments on commit f95999f

Please sign in to comment.