Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor logging (Log to Info) #189

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion checks/handler_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (r runtimeConfig) getTrueHandler(h handlerConfigs) string {
}

if h.handlerName != r.wrapperName {
util.Logln("Warning: handler not set to New Relic layer wrapper", r.wrapperName)
util.Infoln("Warning: handler not set to New Relic layer wrapper", r.wrapperName)
return h.handlerName
}

Expand Down
4 changes: 2 additions & 2 deletions checks/startup_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func RunChecks(ctx context.Context, conf *config.Configuration, reg *api.Registr
runtimeConfig, err := checkAndReturnRuntime()
if err != nil {
errLog := fmt.Sprintf("There was an issue querying for the latest agent version: %v", err)
util.Logln(errLog)
util.Infoln(errLog)
}

for _, check := range checks {
Expand All @@ -41,7 +41,7 @@ func runCheck(ctx context.Context, conf *config.Configuration, reg *api.Registra
err := check(ctx, conf, reg, r)
if err != nil {
errLog := fmt.Sprintf("Startup check failed: %v", err)
util.Logln(errLog)
util.Infoln(errLog)

//Send a log line to NR as well
logSender.SendFunctionLogs(ctx, "", []logserver.LogLine{
Expand Down
6 changes: 3 additions & 3 deletions credentials/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,19 @@ func IsSSMParameterConfigured(ctx context.Context, conf *config.Configuration) b
// variable if set.
func GetNewRelicLicenseKey(ctx context.Context, conf *config.Configuration) (string, error) {
if conf.LicenseKey != "" {
util.Logln("Using license key from environment variable")
util.Infoln("Using license key from environment variable")
return conf.LicenseKey, nil
}

secretId := conf.LicenseKeySecretId
if secretId != "" {
util.Logln("Fetching license key from secret id " + secretId)
util.Infoln("Fetching license key from secret id " + secretId)
return tryLicenseKeyFromSecret(ctx, secretId)
}

parameterName := conf.LicenseKeySSMParameterName
if parameterName != "" {
util.Logln("Fetching license key from parameter name " + conf.LicenseKeySSMParameterName)
util.Infoln("Fetching license key from parameter name " + conf.LicenseKeySSMParameterName)
return tryLicenseKeyFromSSMParameter(ctx, parameterName)
}

Expand Down
10 changes: 5 additions & 5 deletions lambda/logserver/logserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ func (ls *LogServer) handler(res http.ResponseWriter, req *http.Request) {

bodyBytes, err := io.ReadAll(req.Body)
if err != nil {
util.Logf("Error processing log request: %v", err)
util.Infof("Error processing log request: %v", err)
}

var logEvents []api.LogEvent
err = json.Unmarshal(bodyBytes, &logEvents)
if err != nil {
util.Logf("Error parsing log payload: %v", err)
util.Infof("Error parsing log payload: %v", err)
}

var functionLogs []LogLine
Expand Down Expand Up @@ -171,7 +171,7 @@ func (ls *LogServer) handler(res http.ResponseWriter, req *http.Request) {
}
ls.platformLogChan <- reportLine
case "platform.logsDropped":
util.Logf("Platform dropped logs: %v", event.Record)
util.Infof("Platform dropped logs: %v", event.Record)
case "function":
record := event.Record.(string)
ls.lastRequestIdLock.Lock()
Expand Down Expand Up @@ -218,8 +218,8 @@ func startInternal(host string) (*LogServer, error) {
server.Handler = mux

go func() {
util.Logln("Starting log server.")
util.Logf("Log server terminating: %v\n", server.Serve(listener))
util.Infoln("Starting log server.")
util.Infof("Log server terminating: %v\n", server.Serve(listener))
}()

return logServer, nil
Expand Down
38 changes: 19 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func main() {
go func() {
s := <-sigs
cancel()
util.Logf("Received %v Exiting", s)
util.Infof("Received %v Exiting", s)
}()

// Allow extension to be interrupted with CTRL-C
Expand All @@ -64,7 +64,7 @@ func main() {
// Optionally enable debug logging, disabled by default
util.ConfigLogger(conf.LogsEnabled, conf.LogLevel == config.DebugLogLevel)

util.Logf("Initializing version %s of the New Relic Lambda Extension...", util.Version)
util.Infof("Initializing version %s of the New Relic Lambda Extension...", util.Version)

// Extensions must register
registrationClient := client.New(http.Client{})
Expand All @@ -80,15 +80,15 @@ func main() {

// If extension disabled, go into no op mode
if !conf.ExtensionEnabled {
util.Logln("Extension telemetry processing disabled")
util.Infoln("Extension telemetry processing disabled")
noopLoop(ctx, invocationClient)
return
}

// Attempt to find the license key for telemetry sending
licenseKey, err := credentials.GetNewRelicLicenseKey(ctx, conf)
if err != nil {
util.Logln("Failed to retrieve New Relic license key", err)
util.Infoln("Failed to retrieve New Relic license key", err)
// We fail open; telemetry will go to CloudWatch instead
noopLoop(ctx, invocationClient)
return
Expand All @@ -102,7 +102,7 @@ func main() {
if err != nil {
err2 := invocationClient.InitError(ctx, "logServer.start", err)
if err2 != nil {
util.Logln(err2)
util.Infoln(err2)
}
util.Panic("Failed to start logs HTTP server", err)
}
Expand All @@ -116,7 +116,7 @@ func main() {
if err != nil {
err2 := invocationClient.InitError(ctx, "logServer.register", err)
if err2 != nil {
util.Logln(err2)
util.Infoln(err2)
}
util.Panic("Failed to register with Logs API", err)
}
Expand All @@ -127,7 +127,7 @@ func main() {
if err != nil {
err2 := invocationClient.InitError(ctx, "telemetryClient.init", err)
if err2 != nil {
util.Logln(err2)
util.Infoln(err2)
}
util.Panic("telemetry pipe init failed: ", err)
}
Expand All @@ -149,12 +149,12 @@ func main() {
// Call next, and process telemetry, until we're shut down
eventCounter := mainLoop(ctx, invocationClient, batch, telemetryChan, logServer, telemetryClient)

util.Logf("New Relic Extension shutting down after %v events\n", eventCounter)
util.Infof("New Relic Extension shutting down after %v events\n", eventCounter)

pollLogServer(logServer, batch)
err = logServer.Close()
if err != nil {
util.Logln("Error shutting down Log API server", err)
util.Infoln("Error shutting down Log API server", err)
}

finalHarvest := batch.Close()
Expand All @@ -165,7 +165,7 @@ func main() {

shutdownAt := time.Now()
ranFor := shutdownAt.Sub(extensionStartup)
util.Logf("Extension shutdown after %vms", ranFor.Milliseconds())
util.Infof("Extension shutdown after %vms", ranFor.Milliseconds())
}

// logShipLoop ships function logs to New Relic as they arrive.
Expand All @@ -178,7 +178,7 @@ func logShipLoop(ctx context.Context, logServer *logserver.LogServer, telemetryC

err := telemetryClient.SendFunctionLogs(ctx, invokedFunctionARN, functionLogs)
if err != nil {
util.Logf("Failed to send %d function logs", len(functionLogs))
util.Infof("Failed to send %d function logs", len(functionLogs))
}
}
}
Expand All @@ -202,10 +202,10 @@ func mainLoop(ctx context.Context, invocationClient *client.InvocationClient, ba
eventStart := time.Now()

if err != nil {
util.Logln(err)
util.Infoln(err)
err = invocationClient.ExitError(ctx, "NextEventError.Main", err)
if err != nil {
util.Logln(err)
util.Infoln(err)
}
continue
}
Expand All @@ -222,7 +222,7 @@ func mainLoop(ctx context.Context, invocationClient *client.InvocationClient, ba
// We received telemetry
util.Debugf("Agent telemetry bytes: %s", base64.URLEncoding.EncodeToString(telemetryBytes))
batch.AddTelemetry(lastRequestId, telemetryBytes)
util.Logf("We suspected a timeout for request %s but got telemetry anyway", lastRequestId)
util.Infof("We suspected a timeout for request %s but got telemetry anyway", lastRequestId)
default:
}
}
Expand Down Expand Up @@ -287,7 +287,7 @@ func mainLoop(ctx context.Context, invocationClient *client.InvocationClient, ba
util.Debugf("Agent telemetry bytes: %s", base64.URLEncoding.EncodeToString(telemetryBytes))
inv := batch.AddTelemetry(lastRequestId, telemetryBytes)
if inv == nil {
util.Logf("Failed to add telemetry for request %v", lastRequestId)
util.Infof("Failed to add telemetry for request %v", lastRequestId)
}

// Opportunity for an aggressive harvest, in which case, we definitely want to wait for the HTTP POST
Expand Down Expand Up @@ -322,13 +322,13 @@ func shipHarvest(ctx context.Context, harvested []*telemetry.Invocation, telemet

err, _ := telemetryClient.SendTelemetry(ctx, invokedFunctionARN, telemetrySlice)
if err != nil {
util.Logf("Failed to send harvested telemetry for %d invocations %s", len(harvested), err)
util.Infof("Failed to send harvested telemetry for %d invocations %s", len(harvested), err)
}
}
}

func noopLoop(ctx context.Context, invocationClient *client.InvocationClient) {
util.Logln("Starting no-op mode, no telemetry will be sent")
util.Infoln("Starting no-op mode, no telemetry will be sent")

for {
select {
Expand All @@ -337,10 +337,10 @@ func noopLoop(ctx context.Context, invocationClient *client.InvocationClient) {
default:
event, err := invocationClient.NextEvent(ctx)
if err != nil {
util.Logln(err)
util.Infoln(err)
errErr := invocationClient.ExitError(ctx, "NextEventError.Noop", err)
if errErr != nil {
util.Logln(errErr)
util.Infoln(errErr)
}
continue
}
Expand Down
6 changes: 3 additions & 3 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func TestMainShutdown(t *testing.T) {
)

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
util.Logln("Path: ", r.URL.Path)
util.Infoln("Path: ", r.URL.Path)
defer util.Close(r.Body)

if r.URL.Path == "/2020-01-01/extension/register" {
Expand Down Expand Up @@ -281,7 +281,7 @@ func TestMainNoLicenseKey(t *testing.T) {
)

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
util.Logln("Path: ", r.URL.Path)
util.Infoln("Path: ", r.URL.Path)
defer util.Close(r.Body)

if r.URL.Path == "/2020-01-01/extension/register" {
Expand Down Expand Up @@ -364,7 +364,7 @@ func TestMainExtensionDisabled(t *testing.T) {
)

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
util.Logln("Path: ", r.URL.Path)
util.Infoln("Path: ", r.URL.Path)
defer util.Close(r.Body)

if r.URL.Path == "/2020-01-01/extension/register" {
Expand Down
8 changes: 4 additions & 4 deletions telemetry/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (c *Client) SendTelemetry(ctx context.Context, invokedFunctionARN string, t
end := time.Now()
totalTime := end.Sub(start)
transmissionTime := end.Sub(transmitStart)
util.Logf(
util.Infof(
"Sent %d/%d New Relic payload batches with %d log events successfully with certainty in %.3fms (%dms to transmit %.1fkB).\n",
successCount,
len(compressedPayloads),
Expand Down Expand Up @@ -168,10 +168,10 @@ func (c *Client) sendPayloads(compressedPayloads []*bytes.Buffer, builder reques
}

if response.Error != nil {
util.Logf("Telemetry client error: %s", response.Error)
util.Infof("Telemetry client error: %s", response.Error)
sentBytes -= p.Len()
} else if response.Response.StatusCode >= 300 {
util.Logf("Telemetry client response: [%s] %s", response.Response.Status, response.ResponseBody)
util.Infof("Telemetry client response: [%s] %s", response.Response.Status, response.ResponseBody)
} else {
successCount += 1
}
Expand Down Expand Up @@ -271,7 +271,7 @@ func (c *Client) SendFunctionLogs(ctx context.Context, invokedFunctionARN string
successCount, sentBytes := c.sendPayloads(compressedPayloads, builder)
totalTime := time.Since(start)
transmissionTime := time.Since(transmitStart)
util.Logf(
util.Infof(
"Sent %d/%d New Relic function log batches successfully with certainty in %.3fms (%dms to transmit %.1fkB).\n",
successCount,
len(compressedPayloads),
Expand Down
2 changes: 1 addition & 1 deletion util/close.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import (
func Close(thing io.Closer) {
err := thing.Close()
if err != nil {
Logln(err)
Infoln(err)
}
}
8 changes: 4 additions & 4 deletions util/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ func (l Logger) Debugln(v ...interface{}) {
}
}

func (l Logger) Logf(format string, v ...interface{}) {
func (l Logger) Infof(format string, v ...interface{}) {
if l.isEnabled {
log.Printf(format, v...)
}
}

func (l Logger) Logln(v ...interface{}) {
func (l Logger) Infoln(v ...interface{}) {
if l.isEnabled {
log.Println(v...)
}
Expand All @@ -59,13 +59,13 @@ func Debugln(v ...interface{}) {
}
}

func Logf(format string, v ...interface{}) {
func Infof(format string, v ...interface{}) {
if logger.isEnabled {
log.Printf(format, v...)
}
}

func Logln(v ...interface{}) {
func Infoln(v ...interface{}) {
if logger.isEnabled {
log.Println(v...)
}
Expand Down