github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/log/error_hook.go (about)

     1  /*
     2   * Copyright 2020 The Compass Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package log
    18  
    19  import (
    20  	"fmt"
    21  	"strings"
    22  
    23  	"github.com/pkg/errors"
    24  	"github.com/sirupsen/logrus"
    25  )
    26  
    27  const (
    28  	errorSourceField            = "error_source"
    29  	errorSourceFieldUnavailable = "check component log field"
    30  )
    31  
    32  // ErrorLocationHook provides an implementation of the sirupsen/logrus/Hook interface.
    33  // Attaches error location information to log entries if an error is being logged and it has stack-trace information
    34  // (i.e. if it originates from or is wrapped by github.com/pkg/errors).
    35  type ErrorLocationHook struct {
    36  }
    37  
    38  // Levels missing godoc
    39  func (h *ErrorLocationHook) Levels() []logrus.Level {
    40  	return logrus.AllLevels
    41  }
    42  
    43  // Fire missing godoc
    44  func (h *ErrorLocationHook) Fire(entry *logrus.Entry) error {
    45  	var (
    46  		errObj interface{}
    47  		exists bool
    48  	)
    49  
    50  	if errObj, exists = entry.Data[logrus.ErrorKey]; !exists {
    51  		return nil
    52  	}
    53  
    54  	err, ok := errObj.(error)
    55  	if !ok {
    56  		return errors.New("object logged as error does not satisfy error interface")
    57  	}
    58  
    59  	stackErr := getInnermostStackTrace(err)
    60  
    61  	if stackErr != nil {
    62  		stackTrace := stackErr.StackTrace()
    63  		bndl := getPkgName(stackTrace)
    64  		errSource := fmt.Sprintf("%s/%s:%d:%n", bndl, stackTrace[0], stackTrace[0], stackTrace[0])
    65  		entry.Data[errorSourceField] = errSource
    66  	} else {
    67  		entry.Data[errorSourceField] = errorSourceFieldUnavailable
    68  	}
    69  
    70  	return nil
    71  }
    72  
    73  func getPkgName(trace errors.StackTrace) string {
    74  	formattedTrace := fmt.Sprintf("%+s", trace[0])
    75  	split := strings.Split(formattedTrace, "/") // Even on Windows this should be "/"
    76  
    77  	return split[len(split)-2]
    78  }
    79  
    80  type stackTracer interface {
    81  	error
    82  	StackTrace() errors.StackTrace
    83  }
    84  
    85  type causer interface {
    86  	Cause() error
    87  }
    88  
    89  type unwrapper interface {
    90  	Unwrap() error
    91  }
    92  
    93  // getInnermostStackTrace drills down into all possible error wrappings looking for the inner-most error that is annotated with a stack trace
    94  func getInnermostStackTrace(err error) stackTracer {
    95  	var tracer stackTracer
    96  
    97  	for {
    98  		t, isTracer := err.(stackTracer)
    99  		if isTracer {
   100  			tracer = t
   101  		}
   102  
   103  		c, isCauser := err.(causer)
   104  		if isCauser {
   105  			err = c.Cause()
   106  			continue
   107  		}
   108  
   109  		u, inUnwrappable := err.(unwrapper)
   110  		if inUnwrappable {
   111  			err = u.Unwrap()
   112  			continue
   113  		}
   114  
   115  		return tracer
   116  	}
   117  }