From 1ac95db53ae5aad4a6479984bd0071ae90383a4f Mon Sep 17 00:00:00 2001 From: Robb Kidd Date: Fri, 21 Apr 2023 10:51:04 +0000 Subject: [PATCH 01/10] customize the user-agent of the OLTP exporter Instead of the Go autoinst agent appearing to telemetry data receivers as only the version of grpc-go against which it was built, this adds an identifier to the User-Agent header via the dial options parameter support within otlptracegrpc. To help troubleshooting data transmission, runtime operating system and CPU architecture is included in the user-agent within a parenthetical comment in accordance with the header's specification.[1] This replaces the low-level build up of a gRPC client connection with a reuse of the OTel Go SDK's grpc trace client constructor. A happy byproduct of using the SDK's constructor is that all of the OTEL_EXPORTER_OTLP_* environment variables related to exporting traces ought to be supported now, obviating the need to maintain code here to retrieve, interpret, and handle them. [1] RFC 7231 https://www.rfc-editor.org/rfc/rfc7231#section-5.5.3 though more readable documentation about this header spec is available at MDN Web Docs: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent --- go.mod | 2 +- pkg/opentelemetry/controller.go | 36 ++++++++++++++++----------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/go.mod b/go.mod index dc840c67c..5d9e94d98 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/pkg/errors v0.9.1 github.com/prometheus/procfs v0.9.0 go.opentelemetry.io/otel v1.14.0 + go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.14.0 go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.14.0 go.opentelemetry.io/otel/sdk v1.14.0 go.opentelemetry.io/otel/trace v1.14.0 @@ -25,7 +26,6 @@ require ( github.com/golang/protobuf v1.5.2 // indirect github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.14.0 // indirect - go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.14.0 // indirect go.opentelemetry.io/proto/otlp v0.19.0 // indirect go.uber.org/atomic v1.7.0 // indirect go.uber.org/multierr v1.6.0 // indirect diff --git a/pkg/opentelemetry/controller.go b/pkg/opentelemetry/controller.go index e196fb0d2..79935cde9 100644 --- a/pkg/opentelemetry/controller.go +++ b/pkg/opentelemetry/controller.go @@ -19,11 +19,13 @@ import ( "fmt" "os" "runtime" + "strings" "time" "github.com/prometheus/procfs" "go.opentelemetry.io/auto/pkg/instrumentors/events" "go.opentelemetry.io/auto/pkg/log" + "go.opentelemetry.io/otel/exporters/otlp/otlptrace" "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" "go.opentelemetry.io/otel/sdk/resource" sdktrace "go.opentelemetry.io/otel/sdk/trace" @@ -34,10 +36,19 @@ import ( ) const ( - otelEndpointEnvVar = "OTEL_EXPORTER_OTLP_ENDPOINT" otelServiceNameEnvVar = "OTEL_SERVICE_NAME" ) +var ( + // releaseVersion = ??? // TODO: reference version of the build to include in outward comms + // start of this autoinstrumentation's exporter User-Agent header, e.g. ""OTel-Go-Auto-Instrumentation/1.2.3" + baseUserAgent = fmt.Sprintf("OTel-Go-Auto-Instrumentation") // TODO: include the releaseVersion + // Information about the runtime environment for inclusion in User-Agent + runtimeInfo = fmt.Sprintf("%s (%s/%s)", strings.Replace(runtime.Version(), "go", "go/", 1), runtime.GOOS, runtime.GOARCH) + // The default User-Agent when no additions have been given + autoinstUserAgent = fmt.Sprintf("%s %s", baseUserAgent, runtimeInfo) +) + type Controller struct { tracerProvider trace.TracerProvider tracersMap map[string]trace.Tracer @@ -83,11 +94,6 @@ func (c *Controller) convertTime(t int64) time.Time { } func NewController() (*Controller, error) { - endpoint, exists := os.LookupEnv(otelEndpointEnvVar) - if !exists { - return nil, fmt.Errorf("%s env var must be set", otelEndpointEnvVar) - } - serviceName, exists := os.LookupEnv(otelServiceNameEnvVar) if !exists { return nil, fmt.Errorf("%s env var must be set", otelServiceNameEnvVar) @@ -98,26 +104,20 @@ func NewController() (*Controller, error) { resource.WithAttributes( semconv.ServiceNameKey.String(serviceName), semconv.TelemetrySDKLanguageGo, + // TODO: semconv.TelemetryAutoVersionKey.String(releaseVersion), ), ) if err != nil { return nil, err } - log.Logger.V(0).Info("Establishing connection to OpenTelemetry collector ...") - timeoutContext, cancel := context.WithTimeout(ctx, time.Second*10) - defer cancel() - conn, err := grpc.DialContext(timeoutContext, endpoint, grpc.WithInsecure(), grpc.WithBlock()) - if err != nil { - log.Logger.Error(err, "unable to connect to OpenTelemetry collector", "addr", endpoint) - return nil, err - } - - traceExporter, err := otlptracegrpc.New(ctx, - otlptracegrpc.WithGRPCConn(conn), + log.Logger.V(0).Info("Establishing connection to OTLP receiver ...") + otlpTraceClient := otlptracegrpc.NewClient( + otlptracegrpc.WithDialOption(grpc.WithUserAgent(autoinstUserAgent)), ) - + traceExporter, err := otlptrace.New(ctx, otlpTraceClient) if err != nil { + log.Logger.Error(err, "unable to connect to OTLP endpoint") return nil, err } From 42800c648007e301013dbfb5e48bd5dd9db3197b Mon Sep 17 00:00:00 2001 From: Robb Kidd Date: Mon, 24 Apr 2023 23:23:14 +0000 Subject: [PATCH 02/10] add release version to user-agent and Auto attribute --- pkg/opentelemetry/controller.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/opentelemetry/controller.go b/pkg/opentelemetry/controller.go index 79935cde9..a16ff23e3 100644 --- a/pkg/opentelemetry/controller.go +++ b/pkg/opentelemetry/controller.go @@ -40,9 +40,9 @@ const ( ) var ( - // releaseVersion = ??? // TODO: reference version of the build to include in outward comms + releaseVersion = "v0.1.0-alpha" // TODO: reference something instead of hard-coding // start of this autoinstrumentation's exporter User-Agent header, e.g. ""OTel-Go-Auto-Instrumentation/1.2.3" - baseUserAgent = fmt.Sprintf("OTel-Go-Auto-Instrumentation") // TODO: include the releaseVersion + baseUserAgent = fmt.Sprintf("OTel-Go-Auto-Instrumentation/%s", releaseVersion) // Information about the runtime environment for inclusion in User-Agent runtimeInfo = fmt.Sprintf("%s (%s/%s)", strings.Replace(runtime.Version(), "go", "go/", 1), runtime.GOOS, runtime.GOARCH) // The default User-Agent when no additions have been given @@ -104,7 +104,7 @@ func NewController() (*Controller, error) { resource.WithAttributes( semconv.ServiceNameKey.String(serviceName), semconv.TelemetrySDKLanguageGo, - // TODO: semconv.TelemetryAutoVersionKey.String(releaseVersion), + semconv.TelemetryAutoVersionKey.String(releaseVersion), ), ) if err != nil { From 3e6e16366ab327056947136603ac1ff7956f0446 Mon Sep 17 00:00:00 2001 From: Robb Kidd Date: Mon, 24 Apr 2023 23:30:03 +0000 Subject: [PATCH 03/10] an attempt at adding unit tests This is an expendable commit if folks don't like it. --- .github/workflows/build.yaml | 9 ++++++ Makefile | 12 ++++++++ go.mod | 4 +++ go.sum | 7 +++++ pkg/opentelemetry/controller_test.go | 43 ++++++++++++++++++++++++++++ 5 files changed, 75 insertions(+) create mode 100644 pkg/opentelemetry/controller_test.go diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index bfee4c0b6..febc4422e 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -43,3 +43,12 @@ jobs: - name: Check diff run: | git diff --exit-code || (echo 'offsets diff detected, run "make offsets"' && exit 1) + unit-tests: + runs-on: ubuntu-latest + steps: + - name: Checkout Repo + uses: actions/checkout@v3 + - name: Run unit tests + run: | + make test + diff --git a/Makefile b/Makefile index 454e77e2f..8f9a6885f 100644 --- a/Makefile +++ b/Makefile @@ -22,6 +22,18 @@ $(TOOLS)/go-licenses: PACKAGE=github.com/google/go-licenses .PHONY: tools tools: $(GOLICENSES) +.PHONY: test +#: run the tests! +test: +ifeq (, $(shell which gotestsum)) + @echo " ***" + @echo "Running with standard go test because gotestsum was not found on PATH. Consider installing gotestsum for friendlier test output!" + @echo " ***" + go test -race -v ./... +else + gotestsum --junitfile unit-tests.xml --format testname -- -race ./... +endif + .PHONY: generate generate: export CFLAGS := $(BPF_INCLUDE) generate: diff --git a/go.mod b/go.mod index 5d9e94d98..28ea4250b 100644 --- a/go.mod +++ b/go.mod @@ -18,13 +18,17 @@ require ( golang.org/x/arch v0.3.0 golang.org/x/sys v0.7.0 google.golang.org/grpc v1.54.0 + gopkg.in/yaml.v3 v3.0.1 ) require ( github.com/cenkalti/backoff/v4 v4.2.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/testify v1.8.2 go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.14.0 // indirect go.opentelemetry.io/proto/otlp v0.19.0 // indirect go.uber.org/atomic v1.7.0 // indirect diff --git a/go.sum b/go.sum index ba0e6f6ab..56f922ee8 100644 --- a/go.sum +++ b/go.sum @@ -161,11 +161,16 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= +github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= @@ -454,6 +459,7 @@ google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= @@ -462,6 +468,7 @@ gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/pkg/opentelemetry/controller_test.go b/pkg/opentelemetry/controller_test.go new file mode 100644 index 000000000..e5344f4d9 --- /dev/null +++ b/pkg/opentelemetry/controller_test.go @@ -0,0 +1,43 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package opentelemetry + +import ( + "io/ioutil" + "testing" + + "github.com/stretchr/testify/assert" + "gopkg.in/yaml.v3" +) + +func TestReleaseVersion(t *testing.T) { + versionYaml, err := ioutil.ReadFile("../../versions.yaml") + if err != nil { + t.Fatalf("Couldn't read versions.yaml file: %e", err) + return + } + + var versionInfo map[string]interface{} + + err = yaml.Unmarshal(versionYaml, &versionInfo) + if err != nil { + t.Fatalf("Couldn't parse version.yaml: %e", err) + return + } + + // incredibad, but it's where the intended version is declared at the moment + expectedVersion := versionInfo["module-sets"].(map[string]interface{})["alpha"].(map[string]interface{})["version"] + assert.Equal(t, expectedVersion, releaseVersion, "Controller release version should match versions.yaml so that it can report the version in use.") +} From 0028b4252590e0bb3aa6566b3c467a4dd9f6bbd5 Mon Sep 17 00:00:00 2001 From: Robb Kidd Date: Mon, 24 Apr 2023 23:32:48 +0000 Subject: [PATCH 04/10] tack tests onto end of generate instead? --- .github/workflows/build.yaml | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index febc4422e..1e832d264 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -6,7 +6,7 @@ on: pull_request: jobs: - generate: + generate-and-test: runs-on: ubuntu-latest steps: - name: Checkout Repo @@ -24,6 +24,9 @@ jobs: - name: verify output run: | git diff --exit-code || (echo 'generated diff, please run "make generate"' && exit 1) + - name: Run unit tests + run: | + make test docker-build: runs-on: ubuntu-latest steps: @@ -43,12 +46,3 @@ jobs: - name: Check diff run: | git diff --exit-code || (echo 'offsets diff detected, run "make offsets"' && exit 1) - unit-tests: - runs-on: ubuntu-latest - steps: - - name: Checkout Repo - uses: actions/checkout@v3 - - name: Run unit tests - run: | - make test - From c13d9a1cfb1d86b3b0e77a026c11eaaec1a62fa8 Mon Sep 17 00:00:00 2001 From: Robb Kidd Date: Tue, 25 Apr 2023 17:03:21 -0400 Subject: [PATCH 05/10] include scheme for endpoint Spec and therefore SDK require the URL scheme in ENDPOINT values. --- .github/workflows/e2e/k8s/sample-job.yml | 2 +- docs/getting-started/emojivoto-instrumented.yaml | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/e2e/k8s/sample-job.yml b/.github/workflows/e2e/k8s/sample-job.yml index 99a94ebfb..6414ef806 100644 --- a/.github/workflows/e2e/k8s/sample-job.yml +++ b/.github/workflows/e2e/k8s/sample-job.yml @@ -30,7 +30,7 @@ spec: - name: OTEL_GO_AUTO_TARGET_EXE value: /sample-app/main - name: OTEL_EXPORTER_OTLP_ENDPOINT - value: "test-opentelemetry-collector:4317" + value: "http://test-opentelemetry-collector:4317" - name: OTEL_SERVICE_NAME value: "sample-app" - name: OTEL_PROPAGATORS diff --git a/docs/getting-started/emojivoto-instrumented.yaml b/docs/getting-started/emojivoto-instrumented.yaml index f19a50b58..d59c7ea6b 100644 --- a/docs/getting-started/emojivoto-instrumented.yaml +++ b/docs/getting-started/emojivoto-instrumented.yaml @@ -61,7 +61,7 @@ spec: - name: OTEL_GO_AUTO_TARGET_EXE value: /usr/local/bin/emojivoto-emoji-svc - name: OTEL_EXPORTER_OTLP_ENDPOINT - value: "jaeger:4317" + value: "http://jaeger:4317" - name: OTEL_SERVICE_NAME value: "emojivoto-emoji" securityContext: @@ -143,7 +143,7 @@ spec: - name: OTEL_GO_AUTO_TARGET_EXE value: /usr/local/bin/emojivoto-voting-svc - name: OTEL_EXPORTER_OTLP_ENDPOINT - value: "jaeger:4317" + value: "http://jaeger:4317" - name: OTEL_SERVICE_NAME value: "emojivoto-voting" securityContext: @@ -227,7 +227,7 @@ spec: - name: OTEL_GO_AUTO_TARGET_EXE value: /usr/local/bin/emojivoto-web - name: OTEL_EXPORTER_OTLP_ENDPOINT - value: "jaeger:4317" + value: "http://jaeger:4317" - name: OTEL_SERVICE_NAME value: "emojivoto-web" securityContext: @@ -244,4 +244,4 @@ spec: emptyDir: {} - name: kernel-debug hostPath: - path: /sys/kernel/debug \ No newline at end of file + path: /sys/kernel/debug From 6c7f3d61052a650b14f597cff2276c99af815519 Mon Sep 17 00:00:00 2001 From: Robb Kidd Date: Tue, 25 Apr 2023 18:39:18 -0400 Subject: [PATCH 06/10] update e2e expectations This Go auto-inst adds its version to resource attributes now. --- test/e2e/gorillamux/traces.json | 6 ++++++ test/e2e/nethttp/traces.json | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/test/e2e/gorillamux/traces.json b/test/e2e/gorillamux/traces.json index 0b0617cff..ba6adfbc6 100644 --- a/test/e2e/gorillamux/traces.json +++ b/test/e2e/gorillamux/traces.json @@ -9,6 +9,12 @@ "stringValue": "sample-app" } }, + { + "key": "telemetry.auto.version", + "value": { + "stringValue": "v0.1.0-alpha" + } + }, { "key": "telemetry.sdk.language", "value": { diff --git a/test/e2e/nethttp/traces.json b/test/e2e/nethttp/traces.json index ad20ef277..a11b52ea0 100644 --- a/test/e2e/nethttp/traces.json +++ b/test/e2e/nethttp/traces.json @@ -9,6 +9,12 @@ "stringValue": "sample-app" } }, + { + "key": "telemetry.auto.version", + "value": { + "stringValue": "v0.1.0-alpha" + } + }, { "key": "telemetry.sdk.language", "value": { From 074d8c9188f2ee120e12bb04b9f3448f86a40fbd Mon Sep 17 00:00:00 2001 From: Robb Kidd Date: Wed, 26 Apr 2023 22:47:12 +0000 Subject: [PATCH 07/10] new version.go for the auto package The version declared here will get bumped when using the multimod releaser utility. The agent's controller uses this version to include in the User-Agent header and in a resource attribute for outgoing telemetry. --- go.mod | 2 +- pkg/opentelemetry/controller.go | 10 +++--- pkg/opentelemetry/controller_test.go | 24 +++---------- version.go | 20 +++++++++++ version_test.go | 54 ++++++++++++++++++++++++++++ 5 files changed, 85 insertions(+), 25 deletions(-) create mode 100644 version.go create mode 100644 version_test.go diff --git a/go.mod b/go.mod index 28ea4250b..1066cf238 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/hashicorp/go-version v1.6.0 github.com/pkg/errors v0.9.1 github.com/prometheus/procfs v0.9.0 + github.com/stretchr/testify v1.8.2 go.opentelemetry.io/otel v1.14.0 go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.14.0 go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.14.0 @@ -28,7 +29,6 @@ require ( github.com/golang/protobuf v1.5.2 // indirect github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/stretchr/testify v1.8.2 go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.14.0 // indirect go.opentelemetry.io/proto/otlp v0.19.0 // indirect go.uber.org/atomic v1.7.0 // indirect diff --git a/pkg/opentelemetry/controller.go b/pkg/opentelemetry/controller.go index a16ff23e3..d0d1dbfb9 100644 --- a/pkg/opentelemetry/controller.go +++ b/pkg/opentelemetry/controller.go @@ -23,6 +23,7 @@ import ( "time" "github.com/prometheus/procfs" + "go.opentelemetry.io/auto" "go.opentelemetry.io/auto/pkg/instrumentors/events" "go.opentelemetry.io/auto/pkg/log" "go.opentelemetry.io/otel/exporters/otlp/otlptrace" @@ -40,12 +41,13 @@ const ( ) var ( - releaseVersion = "v0.1.0-alpha" // TODO: reference something instead of hard-coding - // start of this autoinstrumentation's exporter User-Agent header, e.g. ""OTel-Go-Auto-Instrumentation/1.2.3" + // Controller-local reference to the auto-instrumentation release version. + releaseVersion = auto.Version() + // Start of this auto-instrumentation's exporter User-Agent header, e.g. ""OTel-Go-Auto-Instrumentation/1.2.3" baseUserAgent = fmt.Sprintf("OTel-Go-Auto-Instrumentation/%s", releaseVersion) - // Information about the runtime environment for inclusion in User-Agent + // Information about the runtime environment for inclusion in User-Agent, e.g. "go/1.18.2 (linux/amd64)" runtimeInfo = fmt.Sprintf("%s (%s/%s)", strings.Replace(runtime.Version(), "go", "go/", 1), runtime.GOOS, runtime.GOARCH) - // The default User-Agent when no additions have been given + // Combined User-Agent identifying this auto-instrumentation and its runtime environment, see RFC7231 for format considerations. autoinstUserAgent = fmt.Sprintf("%s %s", baseUserAgent, runtimeInfo) ) diff --git a/pkg/opentelemetry/controller_test.go b/pkg/opentelemetry/controller_test.go index e5344f4d9..e13b0d9c8 100644 --- a/pkg/opentelemetry/controller_test.go +++ b/pkg/opentelemetry/controller_test.go @@ -15,29 +15,13 @@ package opentelemetry import ( - "io/ioutil" + "fmt" "testing" "github.com/stretchr/testify/assert" - "gopkg.in/yaml.v3" + "go.opentelemetry.io/auto" ) -func TestReleaseVersion(t *testing.T) { - versionYaml, err := ioutil.ReadFile("../../versions.yaml") - if err != nil { - t.Fatalf("Couldn't read versions.yaml file: %e", err) - return - } - - var versionInfo map[string]interface{} - - err = yaml.Unmarshal(versionYaml, &versionInfo) - if err != nil { - t.Fatalf("Couldn't parse version.yaml: %e", err) - return - } - - // incredibad, but it's where the intended version is declared at the moment - expectedVersion := versionInfo["module-sets"].(map[string]interface{})["alpha"].(map[string]interface{})["version"] - assert.Equal(t, expectedVersion, releaseVersion, "Controller release version should match versions.yaml so that it can report the version in use.") +func TestUserAgent(t *testing.T) { + assert.Contains(t, autoinstUserAgent, fmt.Sprintf("OTel-Go-Auto-Instrumentation/%s", auto.Version())) } diff --git a/version.go b/version.go new file mode 100644 index 000000000..2fe8f0c6b --- /dev/null +++ b/version.go @@ -0,0 +1,20 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package auto + +// Go autoinstrumentation release version +func Version() string { + return "v0.1.0-alpha" +} diff --git a/version_test.go b/version_test.go new file mode 100644 index 000000000..903f700ea --- /dev/null +++ b/version_test.go @@ -0,0 +1,54 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package auto + +import ( + "io/ioutil" + "regexp" + "testing" + + "github.com/stretchr/testify/assert" + "gopkg.in/yaml.v3" +) + +// regex taken from https://github.com/Masterminds/semver/tree/v3.1.1 +var versionRegex = regexp.MustCompile(`^v?([0-9]+)(\.[0-9]+)?(\.[0-9]+)?` + + `(-([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?` + + `(\+([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?$`) + +func TestVersionSemver(t *testing.T) { + v := Version() + assert.NotNil(t, versionRegex.FindStringSubmatch(v), "version is not semver: %s", v) +} + +func TestVersionMatchesYaml(t *testing.T) { + versionYaml, err := ioutil.ReadFile("versions.yaml") + if err != nil { + t.Fatalf("Couldn't read versions.yaml file: %e", err) + return + } + + var versionInfo map[string]interface{} + + err = yaml.Unmarshal(versionYaml, &versionInfo) + if err != nil { + t.Fatalf("Couldn't parse version.yaml: %e", err) + return + } + + // incredibad, but it's where the intended version is declared at the moment + expectedVersion := versionInfo["module-sets"].(map[string]interface{})["alpha"].(map[string]interface{})["version"] + assert.Equal(t, expectedVersion, Version(), "Build version should match versions.yaml.") +} From 335f20d7c7d0747dddf0fbe824a396ba685fa11c Mon Sep 17 00:00:00 2001 From: Robb Kidd Date: Thu, 27 Apr 2023 02:13:13 +0000 Subject: [PATCH 08/10] test potentially testable modules Modeled after the test targets in OpenTelemetry Go. Removes the "maybe use gotestsum if you've got it!" option. --- Makefile | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 01e15b25b..4efb7f8e0 100644 --- a/Makefile +++ b/Makefile @@ -27,17 +27,13 @@ $(TOOLS)/go-licenses: PACKAGE=github.com/google/go-licenses .PHONY: tools tools: $(GOLICENSES) -.PHONY: test -#: run the tests! -test: -ifeq (, $(shell which gotestsum)) - @echo " ***" - @echo "Running with standard go test because gotestsum was not found on PATH. Consider installing gotestsum for friendlier test output!" - @echo " ***" - go test -race -v ./... -else - gotestsum --junitfile unit-tests.xml --format testname -- -race ./... -endif +ALL_GO_MODS := $(shell find . -type f -name 'go.mod' ! -path '$(TOOLS_MOD_DIR)/*' ! -path './LICENSES/*' | sort) +GO_MODS_TO_TEST := $(ALL_GO_MODS:%=test/%) + +test: $(GO_MODS_TO_TEST) +test/%: GO_MOD=$* +test/%: + cd $(shell dirname $(GO_MOD)) && go test -v ./... .PHONY: generate generate: export CFLAGS := $(BPF_INCLUDE) From 4a4cd13bcacfc59d6f2dcf4b7c8b6cccf67373d6 Mon Sep 17 00:00:00 2001 From: Robb Kidd Date: Thu, 27 Apr 2023 11:22:33 -0400 Subject: [PATCH 09/10] improve code comment for auto.Version() Co-authored-by: Tyler Yahn --- version.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.go b/version.go index 2fe8f0c6b..5596caae1 100644 --- a/version.go +++ b/version.go @@ -14,7 +14,7 @@ package auto -// Go autoinstrumentation release version +// Version is the current release version of OpenTelemetry Go auto-instrumentation in use. func Version() string { return "v0.1.0-alpha" } From dbf3f0e0fcf0ee80609941467852fffe7fd709c2 Mon Sep 17 00:00:00 2001 From: Robb Kidd Date: Thu, 27 Apr 2023 11:30:43 -0400 Subject: [PATCH 10/10] test is PHONY Always Be Testing. Co-authored-by: Tyler Yahn --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index 4efb7f8e0..f97439274 100644 --- a/Makefile +++ b/Makefile @@ -30,6 +30,7 @@ tools: $(GOLICENSES) ALL_GO_MODS := $(shell find . -type f -name 'go.mod' ! -path '$(TOOLS_MOD_DIR)/*' ! -path './LICENSES/*' | sort) GO_MODS_TO_TEST := $(ALL_GO_MODS:%=test/%) +.PHONY: test test: $(GO_MODS_TO_TEST) test/%: GO_MOD=$* test/%: