From fe84420804769d1b4716d3c9ba8f13a63f8fba51 Mon Sep 17 00:00:00 2001 From: Graham Davison Date: Wed, 20 Sep 2023 17:11:15 -0700 Subject: [PATCH] Adds log attribute test to HCLogger test --- aws_config_test.go | 106 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/aws_config_test.go b/aws_config_test.go index 05cca67c..9b74804d 100644 --- a/aws_config_test.go +++ b/aws_config_test.go @@ -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() {