forked from newrelic/newrelic-lambda-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0b44e36
commit 4165628
Showing
2 changed files
with
200 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
package telemetry | ||
|
||
import ( | ||
"bytes" | ||
"crypto/tls" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"regexp" | ||
"strconv" | ||
|
||
"github.com/newrelic/newrelic-lambda-extension/util" | ||
) | ||
|
||
const ( | ||
MetricEndpointEU string = "https://staging-metric-api.eu.newrelic.com/metric/v1" | ||
MetricEndpointUS string = "https://staging-metric-api.newrelic.com/metric/v1" | ||
) | ||
|
||
type Metric struct { | ||
Name string `json:"name"` | ||
Type string `json:"type"` | ||
Value float64 `json:"value"` | ||
Timestamp int64 `json:"timestamp"` | ||
Attributes map[string]string `json:"attributes"` | ||
} | ||
|
||
type MetricPayload struct { | ||
Metrics []Metric `json:"metrics"` | ||
} | ||
|
||
type LambdaMetrics struct { | ||
RequestID string | ||
Duration float64 | ||
BilledDuration float64 | ||
MemorySize int64 | ||
MaxMemoryUsed int64 | ||
InitDuration *float64 | ||
} | ||
|
||
func ParseLambdaLog(logLine string) (*LambdaMetrics, error) { | ||
basicPattern := `RequestId: (\S+)\s+Duration: ([\d.]+) ms\s+Billed Duration: (\d+) ms\s+Memory Size: (\d+) MB\s+Max Memory Used: (\d+) MB` | ||
initPattern := `Init Duration: ([\d.]+) ms` | ||
|
||
basicRe := regexp.MustCompile(basicPattern) | ||
basicMatches := basicRe.FindStringSubmatch(logLine) | ||
if basicMatches == nil { | ||
return nil, fmt.Errorf("invalid log format") | ||
} | ||
|
||
duration, err := strconv.ParseFloat(basicMatches[2], 64) | ||
if err != nil { | ||
return nil, fmt.Errorf("error parsing duration: %v", err) | ||
} | ||
|
||
billedDuration, err := strconv.ParseInt(basicMatches[3], 10, 64) | ||
if err != nil { | ||
return nil, fmt.Errorf("error parsing billed duration: %v", err) | ||
} | ||
|
||
memorySize, err := strconv.ParseInt(basicMatches[4], 10, 64) | ||
if err != nil { | ||
return nil, fmt.Errorf("error parsing memory size: %v", err) | ||
} | ||
|
||
maxMemoryUsed, err := strconv.ParseInt(basicMatches[5], 10, 64) | ||
if err != nil { | ||
return nil, fmt.Errorf("error parsing max memory used: %v", err) | ||
} | ||
|
||
metrics := &LambdaMetrics{ | ||
RequestID: basicMatches[1], | ||
Duration: duration, | ||
BilledDuration: float64(billedDuration), | ||
MemorySize: memorySize, | ||
MaxMemoryUsed: maxMemoryUsed, | ||
InitDuration: nil, // Default to nil for no init duration | ||
} | ||
|
||
// Check for init duration if present | ||
initRe := regexp.MustCompile(initPattern) | ||
initMatches := initRe.FindStringSubmatch(logLine) | ||
if initMatches != nil { | ||
initDuration, err := strconv.ParseFloat(initMatches[1], 64) | ||
if err == nil { // Only set if parsing succeeds | ||
metrics.InitDuration = &initDuration | ||
} | ||
} | ||
|
||
return metrics, nil | ||
} | ||
|
||
// ConvertToMetrics converts LambdaMetrics to a slice of NewRelic metrics | ||
func (lm *LambdaMetrics) ConvertToMetrics(prefix string) []Metric { | ||
timestamp := util.Timestamp() | ||
attributes := map[string]string{ | ||
"requestId": lm.RequestID, | ||
} | ||
|
||
metrics := []Metric{ | ||
{ | ||
Name: prefix + ".duration", | ||
Type: "gauge", | ||
Value: lm.Duration, | ||
Timestamp: timestamp, | ||
Attributes: attributes, | ||
}, | ||
{ | ||
Name: prefix + ".billed_duration", | ||
Type: "gauge", | ||
Value: lm.BilledDuration, | ||
Timestamp: timestamp, | ||
Attributes: attributes, | ||
}, | ||
{ | ||
Name: prefix + ".memory_size", | ||
Type: "gauge", | ||
Value: float64(lm.MemorySize), | ||
Timestamp: timestamp, | ||
Attributes: attributes, | ||
}, | ||
{ | ||
Name: prefix + ".max_memory_used", | ||
Type: "gauge", | ||
Value: float64(lm.MaxMemoryUsed), | ||
Timestamp: timestamp, | ||
Attributes: attributes, | ||
}, | ||
} | ||
|
||
// Add init duration metric only if it exists | ||
if lm.InitDuration != nil { | ||
metrics = append(metrics, Metric{ | ||
Name: prefix + ".init_duration", | ||
Type: "gauge", | ||
Value: *lm.InitDuration, | ||
Timestamp: timestamp, | ||
Attributes: attributes, | ||
}) | ||
} | ||
|
||
return metrics | ||
} | ||
|
||
|
||
func SendMetrics(apiKey string, metrics []Metric, skipTLSVerify bool) (int, string, error) { | ||
payload := []MetricPayload{ | ||
{ | ||
Metrics: metrics, | ||
}, | ||
} | ||
|
||
jsonData, err := json.Marshal(payload) | ||
fmt.Printf("jsonData: %s\n", jsonData) | ||
|
||
if err != nil { | ||
return 0, "", fmt.Errorf("error marshaling JSON: %v", err) | ||
} | ||
|
||
req, err := http.NewRequest("POST", MetricEndpointUS, bytes.NewBuffer(jsonData)) | ||
if err != nil { | ||
return 0, "", fmt.Errorf("error creating request: %v", err) | ||
} | ||
|
||
req.Header.Set("Content-Type", "application/json") | ||
req.Header.Set("Api-Key", apiKey) | ||
|
||
tr := &http.Transport{ | ||
TLSClientConfig: &tls.Config{InsecureSkipVerify: skipTLSVerify}, | ||
} | ||
client := &http.Client{Transport: tr} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return 0, "", fmt.Errorf("error sending request: %v", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return resp.StatusCode, "", fmt.Errorf("error reading response: %v", err) | ||
} | ||
|
||
return resp.StatusCode, string(body), nil | ||
} | ||