Skip to content

Commit

Permalink
Fix empty host.id (#4317)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwear committed Jul 18, 2023
1 parent f6a658c commit 9b0c4d2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -36,6 +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 `resource.WithHostID()` to not set an empty `host.id`. (#4317)

## [1.16.0/0.39.0] 2023-05-18

Expand Down
2 changes: 1 addition & 1 deletion sdk/resource/host_id_readfile.go
Expand Up @@ -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
Expand Down
53 changes: 53 additions & 0 deletions 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)
}

0 comments on commit 9b0c4d2

Please sign in to comment.