From 731b70ff8f00011335aabf6fd6a67f82bcf66dff Mon Sep 17 00:00:00 2001 From: Matthew Wear Date: Fri, 14 Jul 2023 14:36:56 -0700 Subject: [PATCH 1/4] return error from hostid readFile --- sdk/resource/host_id_readfile.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/resource/host_id_readfile.go b/sdk/resource/host_id_readfile.go index f92c6dad0f9..721e3ca6e7d 100644 --- a/sdk/resource/host_id_readfile.go +++ b/sdk/resource/host_id_readfile.go @@ -21,7 +21,7 @@ import "os" func readFile(filename string) (string, error) { b, err := os.ReadFile(filename) if err != nil { - return "", nil + return "", err } return string(b), nil From 9d89898abf3041da6e7a739b7a86a466034ecb9a Mon Sep 17 00:00:00 2001 From: Matthew Wear Date: Fri, 14 Jul 2023 15:21:42 -0700 Subject: [PATCH 2/4] Add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c653ff02a7e..652247ba41f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Correctly format log messages from the `go.opentelemetry.io/otel/exporters/zipkin` exporter. (#4143) - Log an error for calls to `NewView` in `go.opentelemetry.io/otel/sdk/metric` that have empty criteria. (#4307) +- Fix `readFile` in `go.opentelemetry.io/otel/sdk/resource` to return an error for non-existent files. `resource.WithHostID()` would previously return a resource with an empty `host.id` instead of returning an error. (#4317) ## [1.16.0/0.39.0] 2023-05-18 From b0ab0c552d57ab13b0d1c5008b4e089767866659 Mon Sep 17 00:00:00 2001 From: Matthew Wear Date: Mon, 17 Jul 2023 11:20:29 -0700 Subject: [PATCH 3/4] add tests for host id readFile --- sdk/resource/host_id_readfile_test.go | 53 +++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 sdk/resource/host_id_readfile_test.go diff --git a/sdk/resource/host_id_readfile_test.go b/sdk/resource/host_id_readfile_test.go new file mode 100644 index 00000000000..f071a05cc0c --- /dev/null +++ b/sdk/resource/host_id_readfile_test.go @@ -0,0 +1,53 @@ +// 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. + +//go:build linux || dragonfly || freebsd || netbsd || openbsd || solaris + +package resource + +import ( + "os" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestReadFileExistent(t *testing.T) { + fileContents := "foo" + + f, err := os.CreateTemp("", "readfile_") + require.NoError(t, err) + + defer os.Remove(f.Name()) + + _, err = f.WriteString(fileContents) + require.NoError(t, err) + require.NoError(t, f.Close()) + + result, err := readFile(f.Name()) + require.NoError(t, err) + require.Equal(t, result, fileContents) +} + +func TestReadFileNonExistent(t *testing.T) { + // create unique filename + f, err := os.CreateTemp("", "readfile_") + require.NoError(t, err) + + // make file non-existent + require.NoError(t, os.Remove(f.Name())) + + _, err = readFile(f.Name()) + require.ErrorIs(t, err, os.ErrNotExist) +} From a7369a3e361db1d05f6a428c58fc2d0a2594c7dd Mon Sep 17 00:00:00 2001 From: Matthew Wear Date: Tue, 18 Jul 2023 07:54:27 -0700 Subject: [PATCH 4/4] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Robert PajÄ…k --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eccf93d7c0f..15731d8c22b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,7 +36,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Correctly format log messages from the `go.opentelemetry.io/otel/exporters/zipkin` exporter. (#4143) - Log an error for calls to `NewView` in `go.opentelemetry.io/otel/sdk/metric` that have empty criteria. (#4307) -- Fix `readFile` in `go.opentelemetry.io/otel/sdk/resource` to return an error for non-existent files. `resource.WithHostID()` would previously return a resource with an empty `host.id` instead of returning an error. (#4317) +- Fix `resource.WithHostID()` to not set an empty `host.id`. (#4317) ## [1.16.0/0.39.0] 2023-05-18