Skip to content

Commit

Permalink
Send Extension log events
Browse files Browse the repository at this point in the history
  • Loading branch information
chaudharysaket committed Dec 2, 2024
1 parent 3c9ac93 commit c7bcb60
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ The New Relic Lambda Extension can also send your function's logs to New Relic.
| Environment variable | Default value | Options | Description |
|--------|-----------|-------------|-------------|
| `NEW_RELIC_EXTENSION_SEND_FUNCTION_LOGS` | `false` | `true` , `false` | Send function logs to New Relic. |
| `NEW_RELIC_EXTENSION_SEND_EXTENSION_LOGS` | `false` | `true` , `false` | Send extension logs in addition to the function logs to New Relic. |
| `NEW_RELIC_EXTENSION_LOGS_ENABLED` | `true` | `true` , `false` | Enable or disable `[NR_EXT]` log lines |
| `NR_TAGS` | | | Specify tags to be added to all log events. **Optional**. Each tag is composed of a colon-delimited key and value. Multiple key-value pairs are semicolon-delimited; for example, env:prod;team:myTeam. |
| `NR_ENV_DELIMITER` | | | Some users in UTF-8 environments might face difficulty in defining strings of `NR_TAGS` delimited by the semicolon `;` character. Use `NR_ENV_DELIMITER`, to set custom delimiter for `NR_TAGS`. |
Expand Down
6 changes: 6 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Configuration struct {
IgnoreExtensionChecks map[string]bool
LogsEnabled bool
SendFunctionLogs bool
SendExtensionLogs bool
CollectTraceID bool
RipeMillis uint32
RotMillis uint32
Expand Down Expand Up @@ -87,6 +88,7 @@ func ConfigurationFromEnvironment() *Configuration {
logLevelStr, logLevelOverride := os.LookupEnv("NEW_RELIC_EXTENSION_LOG_LEVEL")
logsEnabledStr, logsEnabledOverride := os.LookupEnv("NEW_RELIC_EXTENSION_LOGS_ENABLED")
sendFunctionLogsStr, sendFunctionLogsOverride := os.LookupEnv("NEW_RELIC_EXTENSION_SEND_FUNCTION_LOGS")
sendExtensionLogsStr, sendExtensionLogsOverride := os.LookupEnv("NEW_RELIC_EXTENSION_SEND_EXTENSION_LOGS")
logServerHostStr, logServerHostOverride := os.LookupEnv("NEW_RELIC_LOG_SERVER_HOST")
collectTraceIDStr, collectTraceIDOverride := os.LookupEnv("NEW_RELIC_COLLECT_TRACE_ID")

Expand Down Expand Up @@ -181,6 +183,10 @@ func ConfigurationFromEnvironment() *Configuration {
ret.SendFunctionLogs = true
}

if sendExtensionLogsOverride && strings.ToLower(sendExtensionLogsStr) == "true" {
ret.SendExtensionLogs = true
}

if collectTraceIDOverride && collectTraceIDStr == "true" {
ret.CollectTraceID = true
}
Expand Down
9 changes: 9 additions & 0 deletions lambda/logserver/logserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ func (ls *LogServer) handler(res http.ResponseWriter, req *http.Request) {

for _, event := range logEvents {
switch event.Type {
case "extension":
record := event.Record.(string)
ls.lastRequestIdLock.Lock()
functionLogs = append(functionLogs, LogLine{
Time: event.Time,
RequestID: ls.lastRequestId,
Content: []byte(record),
})
ls.lastRequestIdLock.Unlock()
case "platform.start":
ls.lastRequestIdLock.Lock()
switch event.Record.(type) {
Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ func main() {
if conf.SendFunctionLogs {
eventTypes = append(eventTypes, api.Function)
}
if conf.SendExtensionLogs {
eventTypes = append(eventTypes, api.Extension)
}
subscriptionRequest := api.DefaultLogSubscription(eventTypes, logServer.Port())
err = invocationClient.LogRegister(ctx, subscriptionRequest)
if err != nil {
Expand Down

0 comments on commit c7bcb60

Please sign in to comment.