kenshaw / sdhook

Package sdhook provides a Google Stackdriver logging hook for logrus

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

error reporting nil pointer

eraac opened this issue · comments

When i log error log with error reporting configured, code crash due of a nil pointer

Minimum code to reproduce

package main

import (
	"time"

	"github.com/knq/sdhook"
	"github.com/sirupsen/logrus"
)

func main() {
	// create a logger with some fields
	logger := logrus.New()
	logger.WithFields(logrus.Fields{
		"my_field":  115888,
		"my_field2": 898858,
	})

	// create stackdriver hook
	hook, err := sdhook.New(
		sdhook.GoogleServiceAccountCredentialsFile("credentials.json"),
		sdhook.LogName("some_log"),
		sdhook.ErrorReportingService("some_log"),
	)
	if err != nil {
		logger.Fatal(err)
	}

	// add to logrus
	logger.Hooks.Add(hook)

	// log some message
	logger.Errorf("a random message @ %s", time.Now().Format("15:04:05"))

	// wait for the writes to finish
	time.Sleep(10 * time.Second)
}

Error is just here https://github.com/knq/sdhook/blob/master/sdhook.go#L235 sh.errorService is nil

Any idea why ?

Thank you

commented

I ran into the same problem.

Here is a quick patch. Also, you have to add another hook (https://github.com/Gurpartap/logrus-stack) producing strace traces.

diff --git a/vendor/github.com/knq/sdhook/opts.go b/vendor/github.com/knq/sdhook/opts.go          
index 247e16a..add81a4 100644                    
--- a/vendor/github.com/knq/sdhook/opts.go       
+++ b/vendor/github.com/knq/sdhook/opts.go       
@@ -79,7 +79,7 @@ func HTTPClient(client *http.Client) Option {                                   
                if err != nil {                  
                        return err               
                } else {                         
-                       ErrorService(e)          
+                       ErrorService(e)(sh)      
                }                                
                                                 
                return LoggingService(l)(sh)     
diff --git a/vendor/github.com/knq/sdhook/sdhook.go b/vendor/github.com/knq/sdhook/sdhook.go      
index 1100575..48ab4c4 100644                    
--- a/vendor/github.com/knq/sdhook/sdhook.go     
+++ b/vendor/github.com/knq/sdhook/sdhook.go     
@@ -232,7 +232,10 @@ func (sh *StackdriverHook) sendLogMessageViaAgent(entry *logrus.Entry, labels ma
 func (sh *StackdriverHook) sendLogMessageViaAPI(entry *logrus.Entry, labels map[string]string, httpReq *logging.HttpRequest) {
        if sh.errorReportingServiceName != "" && isError(entry) {                                 
                errorEvent := sh.buildErrorReportingEvent(entry, labels, httpReq)                 
-               sh.errorService.Projects.Events.Report(sh.projectID, &errorEvent)                 
+               _, err := sh.errorService.Projects.Events.Report(fmt.Sprintf("projects/%s", sh.projectID), &errorEvent).Do()                                                                         
+               if err != nil {                  
+                       log.Printf("error posting error reporting: %s", err.Error())              
+               }                                
        } else {                                 
                logName := sh.logName            
                if sh.errorReportingLogName != "" && isError(entry) {