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

Adds tags for logs #224

Merged
merged 4 commits into from
Jul 15, 2024
Merged
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
27 changes: 27 additions & 0 deletions telemetry/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"log"
"net"
"net/http"
"os"
"strings"
"time"

Expand Down Expand Up @@ -283,13 +284,39 @@ func (c *Client) SendFunctionLogs(ctx context.Context, invokedFunctionARN string
return nil
}

// getNewRelicTags adds tags to the logs if NR_TAGS has values
func getNewRelicTags(common map[string]interface{}) {
nrTagsStr := os.Getenv("NR_TAGS")
nrDelimiter := os.Getenv("NR_ENV_DELIMITER")
if nrDelimiter == "" {
nrDelimiter = ";"
}

if nrTagsStr != "" {
tags := strings.Split(nrTagsStr, nrDelimiter)
nrTags := make(map[string]string)
for _, tag := range tags {
keyValue := strings.Split(tag, ":")
if len(keyValue) == 2 {
nrTags[keyValue[0]] = keyValue[1]
}
}

for k, v := range nrTags {
common[k] = v
}
}
}

// buildLogPayloads is a helper function that improves readability of the SendFunctionLogs method
func (c *Client) buildLogPayloads(ctx context.Context, invokedFunctionARN string, lines []logserver.LogLine) ([]*bytes.Buffer, requestBuilder, error) {
common := map[string]interface{}{
"plugin": util.Id,
"faas.arn": invokedFunctionARN,
"faas.name": c.functionName,
}

getNewRelicTags(common)

logMessages := make([]FunctionLogMessage, 0, len(lines))
for _, l := range lines {
Expand Down
116 changes: 116 additions & 0 deletions telemetry/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"net/http"
"net/http/httptest"
"os"
"sync/atomic"
"testing"
"time"
Expand Down Expand Up @@ -462,3 +463,118 @@ func TestGetLogEndpointURL(t *testing.T) {
assert.Equal(t, LogEndpointUS, getLogEndpointURL("us mock license key", ""))
assert.Equal(t, LogEndpointEU, getLogEndpointURL("eu mock license key", ""))
}

func TestGetNewRelicTags(t *testing.T) {

tests := []struct {
name string
common map[string]interface{}
expected map[string]interface{}
envTags string
envDelimiter string
}{
{
name: "Add New Relic tags to common",
common: map[string]interface{}{
"plugin": "testPlugin",
"faas.arn": "arn:aws:lambda:us-east-1:123456789012:function:testFunction",
"faas.name": "testFunction",
},
expected: map[string]interface{}{
"plugin": "testPlugin",
"faas.arn": "arn:aws:lambda:us-east-1:123456789012:function:testFunction",
"faas.name": "testFunction",
"env": "prod",
"team": "myTeam",
},
envTags: "env:prod;team:myTeam",
envDelimiter: ";",
},
{
name: "Add New Relic tags to common if no delimiter is set",
common: map[string]interface{}{
"plugin": "testPlugin",
"faas.arn": "arn:aws:lambda:us-east-1:123456789012:function:testFunction",
"faas.name": "testFunction",
},
expected: map[string]interface{}{
"plugin": "testPlugin",
"faas.arn": "arn:aws:lambda:us-east-1:123456789012:function:testFunction",
"faas.name": "testFunction",
"env": "prod",
"team": "myTeam",
},
envTags: "env:prod;team:myTeam",
},
{
name: "No New Relic tags to common if delimiter is incorrect",
common: map[string]interface{}{
"plugin": "testPlugin",
"faas.arn": "arn:aws:lambda:us-east-1:123456789012:function:testFunction",
"faas.name": "testFunction",
},
expected: map[string]interface{}{
"plugin": "testPlugin",
"faas.arn": "arn:aws:lambda:us-east-1:123456789012:function:testFunction",
"faas.name": "testFunction",
},
envTags: "env:prod;team:myTeam",
envDelimiter: ",",
},
{
name: "No New Relic tags to add",
common: map[string]interface{}{
"plugin": "testPlugin",
"faas.arn": "arn:aws:lambda:us-east-1:123456789012:function:testFunction",
"faas.name": "testFunction",
},
expected: map[string]interface{}{
"plugin": "testPlugin",
"faas.arn": "arn:aws:lambda:us-east-1:123456789012:function:testFunction",
"faas.name": "testFunction",
},
envTags: "",
envDelimiter: "",
},
{
name: "No New Relic tags to add when enTags and envDelimiter are undeclared",
common: map[string]interface{}{
"plugin": "testPlugin",
"faas.arn": "arn:aws:lambda:us-east-1:123456789012:function:testFunction",
"faas.name": "testFunction",
},
expected: map[string]interface{}{
"plugin": "testPlugin",
"faas.arn": "arn:aws:lambda:us-east-1:123456789012:function:testFunction",
"faas.name": "testFunction",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_ = os.Setenv("NR_TAGS", tt.envTags)
defer os.Unsetenv("NR_TAGS")
_ = os.Setenv("NR_ENV_DELIMITER", tt.envDelimiter)
defer os.Unsetenv("NR_ENV_DELIMITER")

common := make(map[string]interface{}, len(tt.common))
for k, v := range tt.common {
common[k] = v
}

getNewRelicTags(common)

for k, v := range tt.expected {
if common[k] != v {
t.Errorf("expected common[%q] to be %v, but got %v", k, v, common[k])
}
}
for k := range common {
if _, ok := tt.expected[k]; !ok {
t.Errorf("unexpected key %q in common map", k)
}
}
})
}
}
Loading