Skip to content

Commit 9aaa2ea

Browse files
authored
Logging exporter ignore invalid handle on close (#2994)
* Logging exporter shutdown: ignore invalid handle error. * Separate Windows and non-Windows code * Fix imports
1 parent 33f9a24 commit 9aaa2ea

File tree

3 files changed

+68
-9
lines changed

3 files changed

+68
-9
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// +build !windows
16+
17+
package loggingexporter
18+
19+
import (
20+
"syscall"
21+
)
22+
23+
// knownSyncError returns true if the given error is one of the known
24+
// non-actionable errors returned by Sync on Linux and macOS:
25+
//
26+
// Linux:
27+
// - sync /dev/stdout: invalid argument
28+
//
29+
// macOS:
30+
// - sync /dev/stdout: inappropriate ioctl for device
31+
//
32+
func knownSyncError(err error) bool {
33+
switch err {
34+
case syscall.EINVAL, syscall.ENOTSUP, syscall.ENOTTY:
35+
return true
36+
}
37+
return false
38+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// +build windows
16+
17+
package loggingexporter
18+
19+
import "golang.org/x/sys/windows"
20+
21+
// knownSyncError returns true if the given error is one of the known
22+
// non-actionable errors returned by Sync on Windows:
23+
//
24+
// - sync /dev/stderr: The handle is invalid.
25+
//
26+
func knownSyncError(err error) bool {
27+
return err == windows.ERROR_INVALID_HANDLE
28+
}

exporter/loggingexporter/logging_exporter.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"os"
2121
"strconv"
2222
"strings"
23-
"syscall"
2423

2524
"go.uber.org/zap"
2625

@@ -507,18 +506,12 @@ func (s *loggingExporter) pushLogData(
507506

508507
func loggerSync(logger *zap.Logger) func(context.Context) error {
509508
return func(context.Context) error {
510-
// Currently Sync() on stdout and stderr return errors on Linux and macOS,
511-
// respectively:
512-
//
513-
// - sync /dev/stdout: invalid argument
514-
// - sync /dev/stdout: inappropriate ioctl for device
515-
//
509+
// Currently Sync() return a different error depending on the OS.
516510
// Since these are not actionable ignore them.
517511
err := logger.Sync()
518512
if osErr, ok := err.(*os.PathError); ok {
519513
wrappedErr := osErr.Unwrap()
520-
switch wrappedErr {
521-
case syscall.EINVAL, syscall.ENOTSUP, syscall.ENOTTY:
514+
if knownSyncError(wrappedErr) {
522515
err = nil
523516
}
524517
}

0 commit comments

Comments
 (0)