Skip to content

Commit

Permalink
Use Lumber Jack Logger In Otel (#944)
Browse files Browse the repository at this point in the history
  • Loading branch information
sethAmazon authored Nov 8, 2023
1 parent 996d191 commit d7c57d2
Show file tree
Hide file tree
Showing 6 changed files with 402 additions and 38 deletions.
15 changes: 10 additions & 5 deletions cmd/amazon-cloudwatch-agent/amazon-cloudwatch-agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"errors"
"flag"
"fmt"
"io"
"log"
"net/http"
_ "net/http/pprof" // Comment this line to disable pprof endpoint.
Expand Down Expand Up @@ -36,6 +37,7 @@ import (
"github.com/aws/amazon-cloudwatch-agent/cmd/amazon-cloudwatch-agent/internal"
"github.com/aws/amazon-cloudwatch-agent/extension/agenthealth/handler/useragent"
"github.com/aws/amazon-cloudwatch-agent/internal/version"
cwaLogger "github.com/aws/amazon-cloudwatch-agent/logger"
"github.com/aws/amazon-cloudwatch-agent/logs"
_ "github.com/aws/amazon-cloudwatch-agent/plugins"
"github.com/aws/amazon-cloudwatch-agent/profiler"
Expand Down Expand Up @@ -158,6 +160,7 @@ func reloadLoop(
if err := wlog.SetLevelFromName(logLevel); err != nil {
log.Printf("E! Unable to set log level: %v\n", err)
}
cwaLogger.SetLevel(cwaLogger.ConvertToAtomicLevel(wlog.LogLevel()))
// Set AWS SDK logging
sdkLogLevel := os.Getenv(envconfig.AWS_SDK_LOG_LEVEL)
configaws.SetSDKLogLevel(sdkLogLevel)
Expand Down Expand Up @@ -257,7 +260,7 @@ func runAgent(ctx context.Context,
LogWithTimezone: "",
}

logger.SetupLogging(logConfig)
writer := logger.NewLogWriter(logConfig)

log.Printf("I! Starting AmazonCloudWatchAgent %s\n", version.Full())
// Need to set SDK log level before plugins get loaded.
Expand Down Expand Up @@ -331,7 +334,7 @@ func runAgent(ctx context.Context,

useragent.Get().SetComponents(cfg, c)

params := getCollectorParams(factories, provider)
params := getCollectorParams(factories, provider, writer)

cmd := otelcol.NewCommand(params)

Expand All @@ -344,8 +347,10 @@ func runAgent(ctx context.Context,
return cmd.Execute()
}

func getCollectorParams(factories otelcol.Factories, provider otelcol.ConfigProvider) otelcol.CollectorSettings {
params := otelcol.CollectorSettings{
func getCollectorParams(factories otelcol.Factories, provider otelcol.ConfigProvider, writer io.Writer) otelcol.CollectorSettings {
level := cwaLogger.ConvertToAtomicLevel(wlog.LogLevel())
loggingOptions := cwaLogger.NewLoggerOptions(writer, level)
return otelcol.CollectorSettings{
Factories: factories,
ConfigProvider: provider,
// build info is essential for populating the user agent string in otel contrib upstream exporters, like the EMF exporter
Expand All @@ -354,8 +359,8 @@ func getCollectorParams(factories otelcol.Factories, provider otelcol.ConfigProv
Description: "CloudWatch Agent",
Version: version.Number(),
},
LoggingOptions: loggingOptions,
}
return params
}

func components(telegrafConfig *config.Config) (otelcol.Factories, error) {
Expand Down
14 changes: 12 additions & 2 deletions cmd/amazon-cloudwatch-agent/amazon-cloudwatch-agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@
package main

import (
"reflect"
"os"
"testing"

"github.com/go-test/deep"
"github.com/influxdata/wlog"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/otelcol"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"

"github.com/aws/amazon-cloudwatch-agent/logger"
)

func Test_getCollectorParams(t *testing.T) {
Expand All @@ -35,12 +41,16 @@ func Test_getCollectorParams(t *testing.T) {
Description: "CloudWatch Agent",
Version: "Unknown",
},
LoggingOptions: logger.NewLoggerOptions(os.Stderr, zap.NewAtomicLevelAt(zapcore.InfoLevel)),
},
},
}
for _, tt := range tests {
logger.SetLevel(zap.NewAtomicLevelAt(zapcore.InfoLevel))
wlog.SetLevel(wlog.INFO)
t.Run(tt.name, func(t *testing.T) {
if got := getCollectorParams(tt.args.factories, tt.args.provider); !reflect.DeepEqual(got, tt.want) {
got := getCollectorParams(tt.args.factories, tt.args.provider, os.Stderr)
if deep.Equal(got, tt.want) != nil {
t.Errorf("getCollectorParams() = %v, want %v", got, tt.want)
}
})
Expand Down
10 changes: 3 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/aws/amazon-cloudwatch-agent

go 1.20

replace github.com/influxdata/telegraf => github.com/aws/telegraf v0.10.2-0.20220502160831-c20ebe67c5ef
replace github.com/influxdata/telegraf => github.com/aws/telegraf v0.10.2-0.20231103153700-d2a4b9e20b87

// Replace with https://github.com/amazon-contributing/opentelemetry-collector-contrib, there are no requirements for all receivers/processors/exporters
// to be all replaced since there are some changes that will always be from upstream
Expand Down Expand Up @@ -133,7 +133,7 @@ require (
github.com/prometheus/client_golang v1.16.0
github.com/prometheus/common v0.44.0
github.com/prometheus/prometheus v1.8.2-0.20210430082741-2a4b8e12bbf23
github.com/shirou/gopsutil v3.21.5+incompatible
github.com/shirou/gopsutil v3.21.11+incompatible
github.com/shirou/gopsutil/v3 v3.23.8
github.com/stretchr/testify v1.8.4
github.com/xeipuuv/gojsonschema v1.2.0
Expand Down Expand Up @@ -168,6 +168,7 @@ require (
k8s.io/apimachinery v0.28.1
k8s.io/client-go v0.28.1
k8s.io/klog/v2 v2.100.1
github.com/go-test/deep v1.0.2-0.20181118220953-042da051cf31
)

require (
Expand All @@ -187,7 +188,6 @@ require (
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.19.1 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/Showmax/go-fqdn v1.0.0 // indirect
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/alecthomas/participle v0.4.1 // indirect
github.com/alecthomas/participle/v2 v2.0.0 // indirect
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
Expand All @@ -197,7 +197,6 @@ require (
github.com/antchfx/xpath v1.2.0 // indirect
github.com/antonmedv/expr v1.15.0 // indirect
github.com/apache/arrow/go/v12 v12.0.1 // indirect
github.com/apache/thrift v0.16.0 // indirect
github.com/armon/go-metrics v0.4.1 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.13.24 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.41 // indirect
Expand Down Expand Up @@ -425,9 +424,6 @@ require (
k8s.io/klog v1.0.0 // indirect
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect
k8s.io/utils v0.0.0-20230711102312-30195339c3c7 // indirect
modernc.org/ccgo/v3 v3.16.13 // indirect
modernc.org/libc v1.22.2 // indirect
modernc.org/sqlite v1.18.2 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.3.0 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
Expand Down
Loading

0 comments on commit d7c57d2

Please sign in to comment.