You've already forked opentelemetry-go
							
							
				mirror of
				https://github.com/open-telemetry/opentelemetry-go.git
				synced 2025-10-31 00:07:40 +02:00 
			
		
		
		
	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 <MrAlias@users.noreply.github.com> * Update exporters/otlp/otlptrace/version.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> --------- Co-authored-by: Chester Cheung <cheung.zhy.csu@gmail.com> Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
This commit is contained in:
		| @@ -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 | ||||
|  | ||||
|   | ||||
| @@ -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() | ||||
| } | ||||
| @@ -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" | ||||
| @@ -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 { | ||||
|   | ||||
| @@ -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 { | ||||
|   | ||||
							
								
								
									
										20
									
								
								exporters/otlp/otlpmetric/version.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								exporters/otlp/otlpmetric/version.go
									
									
									
									
									
										Normal file
									
								
							| @@ -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" | ||||
| } | ||||
							
								
								
									
										34
									
								
								exporters/otlp/otlpmetric/version_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								exporters/otlp/otlpmetric/version_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -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) | ||||
| } | ||||
							
								
								
									
										25
									
								
								exporters/otlp/otlptrace/internal/header.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								exporters/otlp/otlptrace/internal/header.go
									
									
									
									
									
										Normal file
									
								
							| @@ -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() | ||||
| } | ||||
							
								
								
									
										25
									
								
								exporters/otlp/otlptrace/internal/header_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								exporters/otlp/otlptrace/internal/header_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -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()) | ||||
| } | ||||
| @@ -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 { | ||||
|   | ||||
| @@ -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) | ||||
|   | ||||
							
								
								
									
										20
									
								
								exporters/otlp/otlptrace/version.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								exporters/otlp/otlptrace/version.go
									
									
									
									
									
										Normal file
									
								
							| @@ -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" | ||||
| } | ||||
							
								
								
									
										34
									
								
								exporters/otlp/otlptrace/version_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								exporters/otlp/otlptrace/version_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -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) | ||||
| } | ||||
| @@ -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 { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user