mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-07-12 14:37:08 +08:00
metrics: add build command duration metric
This adds a build duration metric for the build command with attributes related to the buildx driver, the error type (if any), and which options were used to perform the build from a subset of the options. This also refactors some of the utility methods used by the git tool to determine filepaths into its own separate package so they can be reused in another place. Also adds a test to ensure the resource is initialized correctly and doesn't error. The otel handler logging message is suppressed on buildx invocations so we never see the error if there's a problem with the schema url. It's so easy to mess up the schema url when upgrading OTEL that we need a proper test to make sure we haven't broken the functionality. Signed-off-by: Jonathan A. Sternberg <jonathan.sternberg@docker.com>
This commit is contained in:
33
util/metricutil/resource_test.go
Normal file
33
util/metricutil/resource_test.go
Normal file
@ -0,0 +1,33 @@
|
||||
package metricutil
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"go.opentelemetry.io/otel"
|
||||
)
|
||||
|
||||
func TestResource(t *testing.T) {
|
||||
setErrorHandler(t)
|
||||
|
||||
// Ensure resource creation doesn't result in an error.
|
||||
// This is because the schema urls for the various attributes need to be
|
||||
// the same, but it's really easy to import the wrong package when upgrading
|
||||
// otel to anew version and the buildx CLI swallows any visible errors.
|
||||
res := Resource()
|
||||
|
||||
// Ensure an attribute is present.
|
||||
assert.True(t, res.Set().HasValue("telemetry.sdk.version"), "resource attribute missing")
|
||||
}
|
||||
|
||||
func setErrorHandler(tb testing.TB) {
|
||||
tb.Helper()
|
||||
|
||||
errorHandler := otel.GetErrorHandler()
|
||||
otel.SetErrorHandler(otel.ErrorHandlerFunc(func(err error) {
|
||||
tb.Errorf("otel error: %s", err)
|
||||
}))
|
||||
tb.Cleanup(func() {
|
||||
otel.SetErrorHandler(errorHandler)
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user