Skip to content

Commit

Permalink
fix severity bug
Browse files Browse the repository at this point in the history
  • Loading branch information
glassonion1 committed Dec 10, 2020
1 parent af0a2b2 commit 3e9010b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 16 deletions.
15 changes: 7 additions & 8 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ import (
var NowFunc = time.Now

type LogEntry struct {
Severity severity.Severity `json:"severity,string"`
Message string `json:"message"`
Time time.Time `json:"time"`
Trace string `json:"logging.googleapis.com/trace"`
SpanID string `json:"logging.googleapis.com/spanId"`
JSONPayload interface{} `json:"jsonPayload"`
HTTPRequest *HttpRequest `json:"httpRequest,omitempty"`
Severity string `json:"severity"`
Message string `json:"message"`
Time time.Time `json:"time"`
Trace string `json:"logging.googleapis.com/trace"`
SpanID string `json:"logging.googleapis.com/spanId"`
HTTPRequest *HttpRequest `json:"httpRequest,omitempty"`
}

type HttpRequest struct {
Expand All @@ -48,7 +47,7 @@ func (l *Logger) WriteLog(ctx context.Context, severity severity.Severity, forma
trace := fmt.Sprintf("projects/%s/traces/%s", config.ProjectID, sc.TraceID)
msg := fmt.Sprintf(format, a...)
ety := &LogEntry{
Severity: severity,
Severity: severity.String(),
Message: msg,
Time: NowFunc(),
Trace: trace,
Expand Down
17 changes: 17 additions & 0 deletions internal/severity/severity.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,20 @@ const (
Alert // 700
Emergency // 800
)

var severityMap = map[Severity]string{
Default: "DEFAULT",
Debug: "DEBUG",
Info: "INFO",
Notice: "NOTICE",
Warning: "WARNING",
Error: "ERROR",
Critical: "CRITICAL",
Alert: "ALERT",
Emergency: "EMERGENCY",
}

// String returns text representation for the severity
func (s Severity) String() string {
return severityMap[s]
}
13 changes: 6 additions & 7 deletions logz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ import (
Tests logz functions.
The log format is below.
{
"severity":"200",
"severity":"INFO",
"message":"writes info log",
"time":"2020-12-31T23:59:59.999999999+09:00",
"logging.googleapis.com/trace":"projects/test/traces/00000000000000000000000000000000",
"logging.googleapis.com/spanId":"0000000000000000",
"jsonPayload":null
"logging.googleapis.com/spanId":"0000000000000000"
}
*/
func TestLogz(t *testing.T) {
Expand Down Expand Up @@ -56,7 +55,7 @@ func TestLogz(t *testing.T) {
// Gets the log from buffer.
got := strings.TrimRight(buf.String(), "\n")

expected := `{"severity":"200","message":"writes info log","time":"2020-12-31T23:59:59.999999999Z","logging.googleapis.com/trace":"projects/test/traces/00000000000000000000000000000000","logging.googleapis.com/spanId":"0000000000000000","jsonPayload":null}`
expected := `{"severity":"INFO","message":"writes info log","time":"2020-12-31T23:59:59.999999999Z","logging.googleapis.com/trace":"projects/test/traces/00000000000000000000000000000000","logging.googleapis.com/spanId":"0000000000000000"}`

if diff := cmp.Diff(got, expected); diff != "" {
// Restores the stdout
Expand All @@ -83,7 +82,7 @@ func TestLogz(t *testing.T) {
// Gets the log from buffer.
got := strings.TrimRight(buf.String(), "\n")

expected := `{"severity":"400","message":"writes warning log","time":"2020-12-31T23:59:59.999999999Z","logging.googleapis.com/trace":"projects/test/traces/00000000000000000000000000000000","logging.googleapis.com/spanId":"0000000000000000","jsonPayload":null}`
expected := `{"severity":"WARNING","message":"writes warning log","time":"2020-12-31T23:59:59.999999999Z","logging.googleapis.com/trace":"projects/test/traces/00000000000000000000000000000000","logging.googleapis.com/spanId":"0000000000000000"}`

if diff := cmp.Diff(got, expected); diff != "" {
// Restores the stdout
Expand All @@ -110,7 +109,7 @@ func TestLogz(t *testing.T) {
// Gets the log from buffer.
got := strings.TrimRight(buf.String(), "\n")

expected := `{"severity":"500","message":"writes error log","time":"2020-12-31T23:59:59.999999999Z","logging.googleapis.com/trace":"projects/test/traces/00000000000000000000000000000000","logging.googleapis.com/spanId":"0000000000000000","jsonPayload":null}`
expected := `{"severity":"ERROR","message":"writes error log","time":"2020-12-31T23:59:59.999999999Z","logging.googleapis.com/trace":"projects/test/traces/00000000000000000000000000000000","logging.googleapis.com/spanId":"0000000000000000"}`

if diff := cmp.Diff(got, expected); diff != "" {
// Restores the stdout
Expand All @@ -137,7 +136,7 @@ func TestLogz(t *testing.T) {
// Gets the log from buffer.
got := strings.TrimRight(buf.String(), "\n")

expected := `{"severity":"600","message":"writes critical log","time":"2020-12-31T23:59:59.999999999Z","logging.googleapis.com/trace":"projects/test/traces/00000000000000000000000000000000","logging.googleapis.com/spanId":"0000000000000000","jsonPayload":null}`
expected := `{"severity":"CRITICAL","message":"writes critical log","time":"2020-12-31T23:59:59.999999999Z","logging.googleapis.com/trace":"projects/test/traces/00000000000000000000000000000000","logging.googleapis.com/spanId":"0000000000000000"}`

if diff := cmp.Diff(got, expected); diff != "" {
// Restores the stdout
Expand Down
19 changes: 18 additions & 1 deletion middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,29 @@ import (
"net/http"

"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel"
)

// HTTPMiddleware is middleware for HTTP handler
func HTTPMiddleware(label string) func(http.Handler) http.Handler {
func HTTPMiddleware2(label string) func(http.Handler) http.Handler {
return func(h http.Handler) http.Handler {

return otelhttp.NewHandler(h, label)
}
}

func HTTPMiddleware(label string) func(http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
tracer := otel.Tracer("logz")

prop := otel.GetTextMapPropagator()
ctx := prop.Extract(r.Context(), r.Header)

newCtx, span := tracer.Start(ctx, r.URL.String())
defer span.End()

h.ServeHTTP(w, r.WithContext(newCtx))
})
}
}
7 changes: 7 additions & 0 deletions trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ import (
logzpropagation "github.com/glassonion1/logz/propagation"
)

func InitTracer() error {
tp := sdktrace.NewTracerProvider()
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(logzpropagation.HTTPFormat{})
return nil
}

// InitStdoutTracer initializes tracer of OpenTelemetry, that is for stdout
func InitStdoutTracer() error {
// Create stdout exporter to be able to retrieve
Expand Down

0 comments on commit 3e9010b

Please sign in to comment.