Skip to content

Commit

Permalink
Adds log attribute test to HCLogger test
Browse files Browse the repository at this point in the history
  • Loading branch information
gdavison committed Sep 21, 2023
1 parent acba8e2 commit fe84420
Showing 1 changed file with 106 additions and 0 deletions.
106 changes: 106 additions & 0 deletions aws_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3736,6 +3736,112 @@ func TestLogger_HcLog(t *testing.T) {
t.Errorf("GetAwsConfig: line %d: expected module %q, got %q", i+1, e, a)
}
}
var requestLines []map[string]any
var responseLines []map[string]any
for _, line := range lines {
if line["@message"] == "HTTP Request Sent" {
requestLines = append(requestLines, line)
} else if line["@message"] == "HTTP Response Received" {
responseLines = append(responseLines, line)
}
}
if len(requestLines) != 1 {
t.Fatalf("expected 1 request line, got %d", len(requestLines))
}
requestLine := requestLines[0]
maps.DeleteFunc(requestLine, func(k string, _ any) bool {
return strings.HasPrefix(k, "@")
})

for _, k := range []string{
string(semconv.HTTPUserAgentKey),
string(logging.RequestHeaderAttributeKey("Amz-Sdk-Invocation-Id")),
string(logging.RequestHeaderAttributeKey("Authorization")),
string(logging.RequestHeaderAttributeKey("X-Amz-Date")),
} {
_, ok := requestLine[k]
if !ok {
t.Errorf("expected a value for request attribute %q", k)
}
delete(requestLine, k)
}

tsUrl, _ := url.Parse(ts.URL)
if tsUrl.Path == "" {
tsUrl.Path = "/"
}

port, err := strconv.ParseFloat(tsUrl.Port(), 32)
if err != nil {
t.Errorf("error parsing URL port %q: %s", tsUrl.Port(), err)
}

requestBody := "Action=GetCallerIdentity&Version=2011-06-15"

expectedRequest := map[string]any{
// AWS attributes
string(semconv.RPCSystemKey): otelaws.AWSSystemVal,
string(semconv.RPCServiceKey): sts.ServiceID,
string(otelaws.RegionKey): "us-east-1",
string(semconv.RPCMethodKey): "GetCallerIdentity",
// Custom attributes
string(logging.AwsSdkKey): awsSdkGoV2Val,
string(logging.CustomEndpointKey): true,
"http.request.body": requestBody + "\n",
// HTTP attributes
string(semconv.HTTPMethodKey): "POST",
string(logging.RequestHeaderAttributeKey("Amz-Sdk-Request")): "attempt=1; max=3",
string(logging.RequestHeaderAttributeKey("Content-Type")): "application/x-www-form-urlencoded",
string(semconv.HTTPRequestContentLengthKey): float64(len(requestBody)),
string(semconv.HTTPURLKey): tsUrl.String(),
// Net attributes
string(semconv.NetPeerNameKey): tsUrl.Hostname(),
string(semconv.NetPeerPortKey): port,
}

if diff := cmp.Diff(requestLine, expectedRequest); diff != "" {
t.Fatalf("unexpected request attributes: (- got, + expected)\n%s", diff)
}

if len(responseLines) != 1 {
t.Fatalf("expected 1 response line, got %d", len(responseLines))
}
responseLine := responseLines[0]
maps.DeleteFunc(responseLine, func(k string, _ any) bool {
return strings.HasPrefix(k, "@")
})

for _, k := range []string{
string("http.duration"),
string("http.response.body"),
string(logging.ResponseHeaderAttributeKey("Date")),
string(semconv.HTTPResponseContentLengthKey),
string(logging.ResponseHeaderAttributeKey("X-Amzn-Requestid")),
} {
_, ok := responseLine[k]
if !ok {
t.Errorf("expected a value for response attribute %q", k)
}
delete(responseLine, k)
}

expectedResponse := map[string]any{
// AWS attributes
string(semconv.RPCSystemKey): otelaws.AWSSystemVal,
string(semconv.RPCServiceKey): sts.ServiceID,
string(otelaws.RegionKey): "us-east-1",
string(semconv.RPCMethodKey): "GetCallerIdentity",
// Custom attributes
string(logging.AwsSdkKey): awsSdkGoV2Val,
string(logging.CustomEndpointKey): true,
// HTTP attributes
string(semconv.HTTPStatusCodeKey): float64(http.StatusOK),
string(logging.ResponseHeaderAttributeKey("Content-Type")): "text/xml",
}

if diff := cmp.Diff(responseLine, expectedResponse); diff != "" {
t.Fatalf("unexpected response attributes: (- got, + expected)\n%s", diff)
}

_, _, diags = GetAwsAccountIDAndPartition(ctx, awsConfig, config)
if diags.HasError() {
Expand Down

0 comments on commit fe84420

Please sign in to comment.