Skip to content

Commit

Permalink
Add metric1log wrapped logger (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
advayakrishna authored Mar 30, 2021
1 parent e49278d commit d159caf
Show file tree
Hide file tree
Showing 11 changed files with 437 additions and 4 deletions.
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-130.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Add metric1log wrapped logger
links:
- https://github.com/palantir/witchcraft-go-logging/pull/130
23 changes: 23 additions & 0 deletions wlog-glog/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,29 @@ func TestWrapped1Audit2Log(t *testing.T) {
}
}

func TestWrapped1Metric1Log(t *testing.T) {
os.Args = []string{
os.Args[0],
"-logtostderr=true",
}
flag.Parse()

entityName := "entity"
entityVersion := "version"
for _, tc := range wrapped1logtests.Metric1TestCases(entityName, entityVersion) {
// TODO: test output
logger := wrapped1log.NewFromProvider(
os.Stdout,
wlog.InfoLevel,
wlogglog.LoggerProvider(),
entityName,
entityVersion,
).Metric()

logger.Metric(tc.MetricName, tc.MetricType, tc.Params()...)
}
}

func TestWrapped1Log(t *testing.T) {
os.Args = []string{
os.Args[0],
Expand Down
8 changes: 8 additions & 0 deletions wlog-zap/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ func TestWrapped1LogEvt2Log(t *testing.T) {
})
}

func TestWrapped1Metric1Log(t *testing.T) {
entityName := "entity"
entityVersion := "version"
wrapped1logtests.Metric1LogJSONTestSuite(t, entityName, entityVersion, func(w io.Writer) metric1log.Logger {
return wrapped1log.NewFromProvider(w, wlog.InfoLevel, zapimpl.LoggerProvider(), entityName, entityVersion).Metric()
})
}

func TestWrapped1LogSvc1Log(t *testing.T) {
entityName := "entity"
entityVersion := "version"
Expand Down
8 changes: 8 additions & 0 deletions wlog-zerolog/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ func TestWrapped1Evt2Log(t *testing.T) {
})
}

func TestWrapped1Metric1Log(t *testing.T) {
entityName := "entity"
entityVersion := "version"
wrapped1logtests.Metric1LogJSONTestSuite(t, entityName, entityVersion, func(w io.Writer) metric1log.Logger {
return wrapped1log.NewFromProvider(w, wlog.InfoLevel, wlogzerolog.LoggerProvider(), entityName, entityVersion).Metric()
})
}

func TestWrapped1Log(t *testing.T) {
entityName := "entity"
entityVersion := "version"
Expand Down
4 changes: 2 additions & 2 deletions wlog/metriclog/metric1log/logger_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ type defaultLogger struct {
}

func (l *defaultLogger) Metric(name, typ string, params ...Param) {
l.logger.Log(toParams(name, typ, params)...)
l.logger.Log(ToParams(name, typ, params)...)
}

func toParams(metricName, metricType string, inParams []Param) []wlog.Param {
func ToParams(metricName, metricType string, inParams []Param) []wlog.Param {
outParams := make([]wlog.Param, len(defaultTypeParam)+1+len(inParams))
copy(outParams, defaultTypeParam)
outParams[len(defaultTypeParam)] = wlog.NewParam(metricNameTypeParam(metricName, metricType).apply)
Expand Down
2 changes: 2 additions & 0 deletions wlog/wrappedlog/wrapped1log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/palantir/witchcraft-go-logging/wlog/auditlog/audit2log"
"github.com/palantir/witchcraft-go-logging/wlog/diaglog/diag1log"
"github.com/palantir/witchcraft-go-logging/wlog/evtlog/evt2log"
"github.com/palantir/witchcraft-go-logging/wlog/metriclog/metric1log"
"github.com/palantir/witchcraft-go-logging/wlog/svclog/svc1log"
"github.com/palantir/witchcraft-go-logging/wlog/trclog/trc1log"
)
Expand All @@ -29,6 +30,7 @@ type Logger interface {
Audit() audit2log.Logger
Diagnostic() diag1log.Logger
Event() evt2log.Logger
Metric() metric1log.Logger
Service(params ...svc1log.Param) svc1log.Logger
Trace() trc1log.Logger
}
Expand Down
9 changes: 9 additions & 0 deletions wlog/wrappedlog/wrapped1log/logger_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/palantir/witchcraft-go-logging/wlog/auditlog/audit2log"
"github.com/palantir/witchcraft-go-logging/wlog/diaglog/diag1log"
"github.com/palantir/witchcraft-go-logging/wlog/evtlog/evt2log"
"github.com/palantir/witchcraft-go-logging/wlog/metriclog/metric1log"
"github.com/palantir/witchcraft-go-logging/wlog/svclog/svc1log"
"github.com/palantir/witchcraft-go-logging/wlog/trclog/trc1log"
)
Expand Down Expand Up @@ -54,6 +55,14 @@ func (l *defaultLogger) Event() evt2log.Logger {
}
}

func (l *defaultLogger) Metric() metric1log.Logger {
return &wrappedMetric1Logger{
name: l.name,
version: l.version,
logger: l.logger,
}
}

func (l *defaultLogger) Service(params ...svc1log.Param) svc1log.Logger {
return &wrappedSvc1Logger{
params: params,
Expand Down
39 changes: 39 additions & 0 deletions wlog/wrappedlog/wrapped1log/logger_metric.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2021 Palantir Technologies. All rights reserved.
//
// 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://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.

package wrapped1log

import (
"github.com/palantir/witchcraft-go-logging/wlog"
"github.com/palantir/witchcraft-go-logging/wlog/metriclog/metric1log"
)

type wrappedMetric1Logger struct {
name string
version string

logger wlog.Logger
}

func (l *wrappedMetric1Logger) Metric(name, typ string, params ...metric1log.Param) {
l.logger.Log(l.toMetricParams(name, typ, params)...)
}

func (l *wrappedMetric1Logger) toMetricParams(metricName, metricType string, inParams []metric1log.Param) []wlog.Param {
outParams := make([]wlog.Param, len(defaultTypeParam)+2)
copy(outParams, defaultTypeParam)
outParams[len(defaultTypeParam)] = wlog.NewParam(wrappedTypeParams(l.name, l.version).apply)
outParams[len(defaultTypeParam)+1] = wlog.NewParam(metric1PayloadParams(metricName, metricType, inParams).apply)
return outParams
}
13 changes: 13 additions & 0 deletions wlog/wrappedlog/wrapped1log/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/palantir/witchcraft-go-logging/wlog/auditlog/audit2log"
"github.com/palantir/witchcraft-go-logging/wlog/diaglog/diag1log"
"github.com/palantir/witchcraft-go-logging/wlog/evtlog/evt2log"
"github.com/palantir/witchcraft-go-logging/wlog/metriclog/metric1log"
"github.com/palantir/witchcraft-go-logging/wlog/svclog/svc1log"
"github.com/palantir/witchcraft-go-logging/wlog/trclog/trc1log"
"github.com/palantir/witchcraft-go-tracing/wtracing"
Expand Down Expand Up @@ -95,6 +96,18 @@ func evt2PayloadParams(name string, params []evt2log.Param) Param {
})
}

func metric1PayloadParams(metricName, metricType string, params []metric1log.Param) Param {
return paramFunc(func(entry wlog.LogEntry) {
metric1Log := wlog.NewMapLogEntry()
wlog.ApplyParams(metric1Log, metric1log.ToParams(metricName, metricType, params))
payload := wlog.NewMapLogEntry()
payload.StringValue(PayloadTypeKey, PayloadMetricLogV1)
payload.AnyMapValue(PayloadMetricLogV1, metric1Log.AllValues())

entry.AnyMapValue(PayloadKey, payload.AllValues())
})
}

func svc1PayloadParams(message string, level wlog.Param, params []svc1log.Param) Param {
return paramFunc(func(entry wlog.LogEntry) {
svc1Log := wlog.NewMapLogEntry()
Expand Down
4 changes: 2 additions & 2 deletions wlog/wrappedlog/wrapped1log/wrapped1logtests/evt2log_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func Evt2TestCases(entityName, entityVersion string) []Evt2TestCase {

func Evt2LogJSONTestSuite(t *testing.T, entityName, entityVersion string, loggerProvider func(w io.Writer) evt2log.Logger) {
evt2LogJSONOutputTests(t, entityName, entityVersion, loggerProvider)
valueIsntOverwrittenByValues(t, entityName, entityVersion, loggerProvider)
evt2LogValueIsntOverwrittenByValues(t, entityName, entityVersion, loggerProvider)
extraValuesIndependentAcrossCalls(t, entityName, entityVersion, loggerProvider)
}

Expand All @@ -117,7 +117,7 @@ func evt2LogJSONOutputTests(t *testing.T, entityName, entityVersion string, logg

// Verifies that if different parameters are specified using Value and Values params, all of the values are present in
// the final output (that is, these parameters should be additive).
func valueIsntOverwrittenByValues(t *testing.T, entityName, entityVersion string, loggerProvider func(w io.Writer) evt2log.Logger) {
func evt2LogValueIsntOverwrittenByValues(t *testing.T, entityName, entityVersion string, loggerProvider func(w io.Writer) evt2log.Logger) {
t.Run("Value and Values params are additive", func(t *testing.T) {
var buf bytes.Buffer
logger := loggerProvider(&buf)
Expand Down
Loading

0 comments on commit d159caf

Please sign in to comment.