From 1b97d78324434bb65a44c9340b6a124821b9be6f Mon Sep 17 00:00:00 2001 From: Remy Chantenay Date: Mon, 10 Apr 2023 23:19:49 +0200 Subject: [PATCH] Add Version func to otlpmetric and otlptrace (#3956) * Add Version func to otel/exporters/otlp/otlpmetric * Add Version func to otel/exporters/otlp/otlptrace * Remove Version func from otel/exporters/otlp/internal * Update CHANGELOG.md * Add nolint rule to hostid readFile * Move GetUserAgentHeader to internal packages * Update exporters/otlp/otlpmetric/version.go Co-authored-by: Tyler Yahn * Update exporters/otlp/otlptrace/version.go Co-authored-by: Tyler Yahn --------- Co-authored-by: Chester Cheung Co-authored-by: Tyler Yahn --- CHANGELOG.md | 2 ++ .../otlp/{ => otlpmetric}/internal/header.go | 11 +++--- .../{ => otlpmetric}/internal/header_test.go | 3 +- .../otlp/otlpmetric/internal/oconf/options.go | 3 +- .../otlp/otlpmetric/otlpmetrichttp/client.go | 2 +- exporters/otlp/otlpmetric/version.go | 20 +++++++++++ exporters/otlp/otlpmetric/version_test.go | 34 +++++++++++++++++++ exporters/otlp/otlptrace/internal/header.go | 25 ++++++++++++++ .../otlp/otlptrace/internal/header_test.go | 25 ++++++++++++++ .../otlptrace/internal/otlpconfig/options.go | 3 +- .../otlp/otlptrace/otlptracehttp/client.go | 3 +- exporters/otlp/otlptrace/version.go | 20 +++++++++++ exporters/otlp/otlptrace/version_test.go | 34 +++++++++++++++++++ sdk/resource/host_id.go | 1 + 14 files changed, 175 insertions(+), 11 deletions(-) rename exporters/otlp/{ => otlpmetric}/internal/header.go (75%) rename exporters/otlp/{ => otlpmetric}/internal/header_test.go (83%) create mode 100644 exporters/otlp/otlpmetric/version.go create mode 100644 exporters/otlp/otlpmetric/version_test.go create mode 100644 exporters/otlp/otlptrace/internal/header.go create mode 100644 exporters/otlp/otlptrace/internal/header_test.go create mode 100644 exporters/otlp/otlptrace/version.go create mode 100644 exporters/otlp/otlptrace/version_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a75215263e..bfcba5231a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - The `go.opentelemetry.io/otel/metric/embedded` package. (#3916) - The `Version` function to `go.opentelemetry.io/otel/sdk` to return the SDK version. (#3949) - Add a `WithNamespace` option to `go.opentelemetry.io/otel/exporters/prometheus` to allow users to prefix metrics with a namespace. (#3970) +- The `Version` function to `go.opentelemetry.io/otel/exporters/otlp/otlpmetric` to return the OTLP metrics client version. (#3956) +- The `Version` function to `go.opentelemetry.io/otel/exporters/otlp/otlptrace` to return the OTLP trace client version. (#3956) ### Changed diff --git a/exporters/otlp/internal/header.go b/exporters/otlp/otlpmetric/internal/header.go similarity index 75% rename from exporters/otlp/internal/header.go rename to exporters/otlp/otlpmetric/internal/header.go index 9aa62ed9e8e..dde06fa8537 100644 --- a/exporters/otlp/internal/header.go +++ b/exporters/otlp/otlpmetric/internal/header.go @@ -12,13 +12,14 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Package internal contains common functionality for all OTLP exporters. -package internal // import "go.opentelemetry.io/otel/exporters/otlp/internal" +package internal // import "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/internal" -import "go.opentelemetry.io/otel" +import ( + "go.opentelemetry.io/otel/exporters/otlp/otlpmetric" +) -// GetUserAgentHeader return an OTLP header value form "OTel OTLP Exporter Go/{{ .Version }}" +// GetUserAgentHeader returns an OTLP header value form "OTel OTLP Exporter Go/{{ .Version }}" // https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md#user-agent func GetUserAgentHeader() string { - return "OTel OTLP Exporter Go/" + otel.Version() + return "OTel OTLP Exporter Go/" + otlpmetric.Version() } diff --git a/exporters/otlp/internal/header_test.go b/exporters/otlp/otlpmetric/internal/header_test.go similarity index 83% rename from exporters/otlp/internal/header_test.go rename to exporters/otlp/otlpmetric/internal/header_test.go index ecca1a9490e..d93340fc0d6 100644 --- a/exporters/otlp/internal/header_test.go +++ b/exporters/otlp/otlpmetric/internal/header_test.go @@ -12,8 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Package internal contains common functionality for all OTLP exporters. -package internal // import "go.opentelemetry.io/otel/exporters/otlp/internal" +package internal import ( "testing" diff --git a/exporters/otlp/otlpmetric/internal/oconf/options.go b/exporters/otlp/otlpmetric/internal/oconf/options.go index b5ab4e6f315..e696a0f78ba 100644 --- a/exporters/otlp/otlpmetric/internal/oconf/options.go +++ b/exporters/otlp/otlpmetric/internal/oconf/options.go @@ -27,6 +27,7 @@ import ( "go.opentelemetry.io/otel/exporters/otlp/internal" "go.opentelemetry.io/otel/exporters/otlp/internal/retry" + ominternal "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/internal" "go.opentelemetry.io/otel/internal/global" "go.opentelemetry.io/otel/sdk/metric" "go.opentelemetry.io/otel/sdk/metric/aggregation" @@ -116,7 +117,7 @@ func NewGRPCConfig(opts ...GRPCOption) Config { AggregationSelector: metric.DefaultAggregationSelector, }, RetryConfig: retry.DefaultConfig, - DialOptions: []grpc.DialOption{grpc.WithUserAgent(internal.GetUserAgentHeader())}, + DialOptions: []grpc.DialOption{grpc.WithUserAgent(ominternal.GetUserAgentHeader())}, } cfg = ApplyGRPCEnvConfigs(cfg) for _, opt := range opts { diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/client.go b/exporters/otlp/otlpmetric/otlpmetrichttp/client.go index a362b45087e..8b90a090b87 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/client.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/client.go @@ -108,7 +108,7 @@ func newClient(opts ...Option) (ominternal.Client, error) { return nil, err } - req.Header.Set("User-Agent", internal.GetUserAgentHeader()) + req.Header.Set("User-Agent", ominternal.GetUserAgentHeader()) if n := len(cfg.Metrics.Headers); n > 0 { for k, v := range cfg.Metrics.Headers { diff --git a/exporters/otlp/otlpmetric/version.go b/exporters/otlp/otlpmetric/version.go new file mode 100644 index 00000000000..952e6c02226 --- /dev/null +++ b/exporters/otlp/otlpmetric/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 otlpmetric // import "go.opentelemetry.io/otel/exporters/otlp/otlpmetric" + +// Version is the current release version of the OpenTelemetry OTLP metrics exporter in use. +func Version() string { + return "1.15.0-rc.2" +} diff --git a/exporters/otlp/otlpmetric/version_test.go b/exporters/otlp/otlpmetric/version_test.go new file mode 100644 index 00000000000..e0f3a3ef3b5 --- /dev/null +++ b/exporters/otlp/otlpmetric/version_test.go @@ -0,0 +1,34 @@ +// 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 otlpmetric_test + +import ( + "regexp" + "testing" + + "github.com/stretchr/testify/assert" + + "go.opentelemetry.io/otel/exporters/otlp/otlpmetric" +) + +// 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 := otlpmetric.Version() + assert.NotNil(t, versionRegex.FindStringSubmatch(v), "version is not semver: %s", v) +} diff --git a/exporters/otlp/otlptrace/internal/header.go b/exporters/otlp/otlptrace/internal/header.go new file mode 100644 index 00000000000..7fcd89a40fb --- /dev/null +++ b/exporters/otlp/otlptrace/internal/header.go @@ -0,0 +1,25 @@ +// 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 internal // import "go.opentelemetry.io/otel/exporters/otlp/otlptrace/internal" + +import ( + "go.opentelemetry.io/otel/exporters/otlp/otlptrace" +) + +// GetUserAgentHeader returns an OTLP header value form "OTel OTLP Exporter Go/{{ .Version }}" +// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md#user-agent +func GetUserAgentHeader() string { + return "OTel OTLP Exporter Go/" + otlptrace.Version() +} diff --git a/exporters/otlp/otlptrace/internal/header_test.go b/exporters/otlp/otlptrace/internal/header_test.go new file mode 100644 index 00000000000..d93340fc0d6 --- /dev/null +++ b/exporters/otlp/otlptrace/internal/header_test.go @@ -0,0 +1,25 @@ +// 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 internal + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestGetUserAgentHeader(t *testing.T) { + require.Regexp(t, "OTel OTLP Exporter Go/1\\..*", GetUserAgentHeader()) +} diff --git a/exporters/otlp/otlptrace/internal/otlpconfig/options.go b/exporters/otlp/otlptrace/internal/otlpconfig/options.go index c48ffd53081..1a6bb423b30 100644 --- a/exporters/otlp/otlptrace/internal/otlpconfig/options.go +++ b/exporters/otlp/otlptrace/internal/otlpconfig/options.go @@ -27,6 +27,7 @@ import ( "go.opentelemetry.io/otel/exporters/otlp/internal" "go.opentelemetry.io/otel/exporters/otlp/internal/retry" + otinternal "go.opentelemetry.io/otel/exporters/otlp/otlptrace/internal" ) const ( @@ -97,7 +98,7 @@ func NewGRPCConfig(opts ...GRPCOption) Config { Timeout: DefaultTimeout, }, RetryConfig: retry.DefaultConfig, - DialOptions: []grpc.DialOption{grpc.WithUserAgent(internal.GetUserAgentHeader())}, + DialOptions: []grpc.DialOption{grpc.WithUserAgent(otinternal.GetUserAgentHeader())}, } cfg = ApplyGRPCEnvConfigs(cfg) for _, opt := range opts { diff --git a/exporters/otlp/otlptrace/otlptracehttp/client.go b/exporters/otlp/otlptrace/otlptracehttp/client.go index 9fbe861717d..be21724212b 100644 --- a/exporters/otlp/otlptrace/otlptracehttp/client.go +++ b/exporters/otlp/otlptrace/otlptracehttp/client.go @@ -33,6 +33,7 @@ import ( "go.opentelemetry.io/otel/exporters/otlp/internal" "go.opentelemetry.io/otel/exporters/otlp/internal/retry" "go.opentelemetry.io/otel/exporters/otlp/otlptrace" + otinternal "go.opentelemetry.io/otel/exporters/otlp/otlptrace/internal" "go.opentelemetry.io/otel/exporters/otlp/otlptrace/internal/otlpconfig" coltracepb "go.opentelemetry.io/proto/otlp/collector/trace/v1" tracepb "go.opentelemetry.io/proto/otlp/trace/v1" @@ -209,7 +210,7 @@ func (d *client) newRequest(body []byte) (request, error) { return request{Request: r}, err } - r.Header.Set("User-Agent", internal.GetUserAgentHeader()) + r.Header.Set("User-Agent", otinternal.GetUserAgentHeader()) for k, v := range d.cfg.Headers { r.Header.Set(k, v) diff --git a/exporters/otlp/otlptrace/version.go b/exporters/otlp/otlptrace/version.go new file mode 100644 index 00000000000..1a1977e975c --- /dev/null +++ b/exporters/otlp/otlptrace/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 otlptrace // import "go.opentelemetry.io/otel/exporters/otlp/otlptrace" + +// Version is the current release version of the OpenTelemetry OTLP trace exporter in use. +func Version() string { + return "1.15.0-rc.2" +} diff --git a/exporters/otlp/otlptrace/version_test.go b/exporters/otlp/otlptrace/version_test.go new file mode 100644 index 00000000000..62558d99751 --- /dev/null +++ b/exporters/otlp/otlptrace/version_test.go @@ -0,0 +1,34 @@ +// 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 otlptrace_test + +import ( + "regexp" + "testing" + + "github.com/stretchr/testify/assert" + + "go.opentelemetry.io/otel/exporters/otlp/otlptrace" +) + +// 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 := otlptrace.Version() + assert.NotNil(t, versionRegex.FindStringSubmatch(v), "version is not semver: %s", v) +} diff --git a/sdk/resource/host_id.go b/sdk/resource/host_id.go index 32a616d0879..39e35f4f2bf 100644 --- a/sdk/resource/host_id.go +++ b/sdk/resource/host_id.go @@ -38,6 +38,7 @@ type fileReader func(string) (string, error) type commandExecutor func(string, ...string) (string, error) +// nolint: unused // This is used by the hostReaderBSD, gated by build tags. func readFile(filename string) (string, error) { b, err := os.ReadFile(filename) if err != nil {