~egtann/sls

824e8f4eeeb1b0ad82b4886a6fafb533a4590a77 — Evan Tann 5 months ago 5e0637b master
fix stackdriver json payload bugs
1 files changed, 12 insertions(+), 5 deletions(-)

M google/stackdriver.go
M google/stackdriver.go => google/stackdriver.go +12 -5
@@ 5,6 5,7 @@ import (
	"encoding/json"
	"fmt"
	"os"
	"strings"
	"time"

	"cloud.google.com/go/logging"


@@ 69,7 70,7 @@ func NewStackdriver(
		return nil, fmt.Errorf("new client: %w", err)
	}
	client.OnError = func(err error) {
		fmt.Println("client error: %v", err)
		fmt.Printf("client error: %v\n", err)
	}
	if err = client.Ping(ctx); err != nil {
		return nil, fmt.Errorf("ping: %w", err)


@@ 84,10 85,16 @@ func NewStackdriver(
}

func (s *Stackdriver) Write(data []byte) (int, error) {
	if s.jsonPayload {
		s.log.Log(logging.Entry{Payload: json.RawMessage(data)})
	} else {
		s.log.Log(logging.Entry{Payload: string(data)})
	if len(data) == 0 {
		return 0, nil
	}
	lines := strings.Split(strings.TrimSpace(string(data)), "\n")
	for _, line := range lines {
		if s.jsonPayload {
			s.log.Log(logging.Entry{Payload: json.RawMessage(line)})
		} else {
			s.log.Log(logging.Entry{Payload: line})
		}
	}
	return len(data), nil
}