Path: blob/main/component/otelcol/receiver/prometheus/internal/logger_test.go
5406 views
// Copyright The OpenTelemetry Authors1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314package internal1516import (17"fmt"18"net/http"19"testing"2021"github.com/go-kit/log"22"github.com/go-kit/log/level"23"github.com/stretchr/testify/assert"24"github.com/stretchr/testify/require"25"go.uber.org/zap"26"go.uber.org/zap/zapcore"27"go.uber.org/zap/zaptest/observer"28)2930func TestLog(t *testing.T) {31tcs := []struct {32name string33input []interface{}34wantLevel zapcore.Level35wantMessage string36}{37{38name: "Starting provider",39input: []interface{}{40"level",41level.DebugValue(),42"msg",43"Starting provider",44"provider",45"string/0",46"subs",47"[target1]",48},49wantLevel: zapcore.DebugLevel,50wantMessage: "Starting provider",51},52{53name: "Scrape failed",54input: []interface{}{55"level",56level.ErrorValue(),57"scrape_pool",58"target1",59"msg",60"Scrape failed",61"err",62"server returned HTTP status 500 Internal Server Error",63},64wantLevel: zapcore.ErrorLevel,65wantMessage: "Scrape failed",66},67}6869for _, tc := range tcs {70t.Run(tc.name, func(t *testing.T) {71conf := zap.NewProductionConfig()72conf.Level.SetLevel(zapcore.DebugLevel)7374// capture zap log entry75var entry zapcore.Entry76h := func(e zapcore.Entry) error {77entry = e78return nil79}8081logger, err := conf.Build(zap.Hooks(h))82require.NoError(t, err)8384adapter := NewZapToGokitLogAdapter(logger)85err = adapter.Log(tc.input...)86require.NoError(t, err)8788assert.Equal(t, tc.wantLevel, entry.Level)89assert.Equal(t, tc.wantMessage, entry.Message)90})91}92}9394func TestExtractLogData(t *testing.T) {95tcs := []struct {96name string97input []interface{}98wantLevel level.Value99wantMessage string100wantOutput []interface{}101}{102{103name: "nil fields",104input: nil,105wantLevel: level.InfoValue(), // Default106wantMessage: "",107wantOutput: nil,108},109{110name: "empty fields",111input: []interface{}{},112wantLevel: level.InfoValue(), // Default113wantMessage: "",114wantOutput: nil,115},116{117name: "info level",118input: []interface{}{119"level",120level.InfoValue(),121},122wantLevel: level.InfoValue(),123wantMessage: "",124wantOutput: nil,125},126{127name: "warn level",128input: []interface{}{129"level",130level.WarnValue(),131},132wantLevel: level.WarnValue(),133wantMessage: "",134wantOutput: nil,135},136{137name: "error level",138input: []interface{}{139"level",140level.ErrorValue(),141},142wantLevel: level.ErrorValue(),143wantMessage: "",144wantOutput: nil,145},146{147name: "debug level + extra fields",148input: []interface{}{149"timestamp",1501596604719,151"level",152level.DebugValue(),153"msg",154"http client error",155},156wantLevel: level.DebugValue(),157wantMessage: "http client error",158wantOutput: []interface{}{159"timestamp", 1596604719,160},161},162{163name: "missing level field",164input: []interface{}{165"timestamp",1661596604719,167"msg",168"http client error",169},170wantLevel: level.InfoValue(), // Default171wantMessage: "http client error",172wantOutput: []interface{}{173"timestamp", 1596604719,174},175},176{177name: "invalid level type",178input: []interface{}{179"level",180"warn", // String is not recognized181},182wantLevel: level.InfoValue(), // Default183wantOutput: []interface{}{184"level", "warn", // Field is preserved185},186},187}188189for _, tc := range tcs {190t.Run(tc.name, func(t *testing.T) {191ld := extractLogData(tc.input)192assert.Equal(t, tc.wantLevel, ld.level)193assert.Equal(t, tc.wantMessage, ld.msg)194assert.Equal(t, tc.wantOutput, ld.otherFields)195})196}197}198199func TestE2E(t *testing.T) {200logger, observed := observer.New(zap.DebugLevel)201gLogger := NewZapToGokitLogAdapter(zap.New(logger))202203const targetStr = "https://host.docker.internal:5000/prometheus"204205tcs := []struct {206name string207log func() error208wantLevel zapcore.Level209wantMessage string210wantOutput []zapcore.Field211}{212{213name: "debug level",214log: func() error {215return level.Debug(gLogger).Log()216},217wantLevel: zapcore.DebugLevel,218wantMessage: "",219wantOutput: []zapcore.Field{},220},221{222name: "info level",223log: func() error {224return level.Info(gLogger).Log()225},226wantLevel: zapcore.InfoLevel,227wantMessage: "",228wantOutput: []zapcore.Field{},229},230{231name: "warn level",232log: func() error {233return level.Warn(gLogger).Log()234},235wantLevel: zapcore.WarnLevel,236wantMessage: "",237wantOutput: []zapcore.Field{},238},239{240name: "error level",241log: func() error {242return level.Error(gLogger).Log()243},244wantLevel: zapcore.ErrorLevel,245wantMessage: "",246wantOutput: []zapcore.Field{},247},248{249name: "logger with and msg",250log: func() error {251ngLogger := log.With(gLogger, "scrape_pool", "scrape_pool")252ngLogger = log.With(ngLogger, "target", targetStr)253return level.Debug(ngLogger).Log("msg", "http client error", "err", fmt.Errorf("%s %q: dial tcp 192.168.65.2:5000: connect: connection refused", http.MethodGet, targetStr))254},255wantLevel: zapcore.DebugLevel,256wantMessage: "http client error",257wantOutput: []zapcore.Field{258zap.String("scrape_pool", "scrape_pool"),259zap.String("target", "https://host.docker.internal:5000/prometheus"),260zap.Error(fmt.Errorf("%s %q: dial tcp 192.168.65.2:5000: connect: connection refused", http.MethodGet, targetStr)),261},262},263{264name: "missing level",265log: func() error {266ngLogger := log.With(gLogger, "target", "foo")267return ngLogger.Log("msg", "http client error")268},269wantLevel: zapcore.InfoLevel, // Default270wantMessage: "http client error",271wantOutput: []zapcore.Field{272zap.String("target", "foo"),273},274},275{276name: "invalid level type",277log: func() error {278ngLogger := log.With(gLogger, "target", "foo")279return ngLogger.Log("msg", "http client error", "level", "warn")280},281wantLevel: zapcore.InfoLevel, // Default282wantMessage: "http client error",283wantOutput: []zapcore.Field{284zap.String("target", "foo"),285zap.String("level", "warn"), // Field is preserved286},287},288}289290for _, tc := range tcs {291t.Run(tc.name, func(t *testing.T) {292assert.NoError(t, tc.log())293entries := observed.TakeAll()294require.Len(t, entries, 1)295assert.Equal(t, tc.wantLevel, entries[0].Level)296assert.Equal(t, tc.wantMessage, entries[0].Message)297assert.Equal(t, tc.wantOutput, entries[0].Context)298})299}300}301302303