Skip to content

Commit

Permalink
Logging exporter ignore invalid handle on close (open-telemetry#2994)
Browse files Browse the repository at this point in the history
* Logging exporter shutdown: ignore invalid handle error.

* Separate Windows and non-Windows code

* Fix imports
  • Loading branch information
pjanotti committed Apr 22, 2021
1 parent 33f9a24 commit 9aaa2ea
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 9 deletions.
38 changes: 38 additions & 0 deletions exporter/loggingexporter/known_sync_error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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:https://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.

// +build !windows

package loggingexporter

import (
"syscall"
)

// knownSyncError returns true if the given error is one of the known
// non-actionable errors returned by Sync on Linux and macOS:
//
// Linux:
// - sync /dev/stdout: invalid argument
//
// macOS:
// - sync /dev/stdout: inappropriate ioctl for device
//
func knownSyncError(err error) bool {
switch err {
case syscall.EINVAL, syscall.ENOTSUP, syscall.ENOTTY:
return true
}
return false
}
28 changes: 28 additions & 0 deletions exporter/loggingexporter/known_sync_error_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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:https://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.

// +build windows

package loggingexporter

import "golang.org/x/sys/windows"

// knownSyncError returns true if the given error is one of the known
// non-actionable errors returned by Sync on Windows:
//
// - sync /dev/stderr: The handle is invalid.
//
func knownSyncError(err error) bool {
return err == windows.ERROR_INVALID_HANDLE
}
11 changes: 2 additions & 9 deletions exporter/loggingexporter/logging_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"os"
"strconv"
"strings"
"syscall"

"go.uber.org/zap"

Expand Down Expand Up @@ -507,18 +506,12 @@ func (s *loggingExporter) pushLogData(

func loggerSync(logger *zap.Logger) func(context.Context) error {
return func(context.Context) error {
// Currently Sync() on stdout and stderr return errors on Linux and macOS,
// respectively:
//
// - sync /dev/stdout: invalid argument
// - sync /dev/stdout: inappropriate ioctl for device
//
// Currently Sync() return a different error depending on the OS.
// Since these are not actionable ignore them.
err := logger.Sync()
if osErr, ok := err.(*os.PathError); ok {
wrappedErr := osErr.Unwrap()
switch wrappedErr {
case syscall.EINVAL, syscall.ENOTSUP, syscall.ENOTTY:
if knownSyncError(wrappedErr) {
err = nil
}
}
Expand Down

0 comments on commit 9aaa2ea

Please sign in to comment.