| 1 |
// Code generated by gotmpl. DO NOT MODIFY. |
| 2 |
// source: internal/shared/semconv/client_test.go.tmpl |
| 3 |
|
| 4 |
// Copyright The OpenTelemetry Authors |
| 5 |
// SPDX-License-Identifier: Apache-2.0 |
| 6 |
|
| 7 |
package semconv |
| 8 |
|
| 9 |
import ( |
| 10 |
"net/http" |
| 11 |
"strconv" |
| 12 |
"testing" |
| 13 |
|
| 14 |
"github.com/stretchr/testify/assert" |
| 15 |
"github.com/stretchr/testify/require" |
| 16 |
|
| 17 |
"go.opentelemetry.io/otel/attribute" |
| 18 |
"go.opentelemetry.io/otel/codes" |
| 19 |
) |
| 20 |
|
| 21 |
func TestHTTPClientStatus(t *testing.T) { |
| 22 |
tests := []struct { |
| 23 |
code int |
| 24 |
stat codes.Code |
| 25 |
msg bool |
| 26 |
}{ |
| 27 |
{0, codes.Error, true}, |
| 28 |
{http.StatusContinue, codes.Unset, false}, |
| 29 |
{http.StatusSwitchingProtocols, codes.Unset, false}, |
| 30 |
{http.StatusProcessing, codes.Unset, false}, |
| 31 |
{http.StatusEarlyHints, codes.Unset, false}, |
| 32 |
{http.StatusOK, codes.Unset, false}, |
| 33 |
{http.StatusCreated, codes.Unset, false}, |
| 34 |
{http.StatusAccepted, codes.Unset, false}, |
| 35 |
{http.StatusNonAuthoritativeInfo, codes.Unset, false}, |
| 36 |
{http.StatusNoContent, codes.Unset, false}, |
| 37 |
{http.StatusResetContent, codes.Unset, false}, |
| 38 |
{http.StatusPartialContent, codes.Unset, false}, |
| 39 |
{http.StatusMultiStatus, codes.Unset, false}, |
| 40 |
{http.StatusAlreadyReported, codes.Unset, false}, |
| 41 |
{http.StatusIMUsed, codes.Unset, false}, |
| 42 |
{http.StatusMultipleChoices, codes.Unset, false}, |
| 43 |
{http.StatusMovedPermanently, codes.Unset, false}, |
| 44 |
{http.StatusFound, codes.Unset, false}, |
| 45 |
{http.StatusSeeOther, codes.Unset, false}, |
| 46 |
{http.StatusNotModified, codes.Unset, false}, |
| 47 |
{http.StatusUseProxy, codes.Unset, false}, |
| 48 |
{306, codes.Unset, false}, |
| 49 |
{http.StatusTemporaryRedirect, codes.Unset, false}, |
| 50 |
{http.StatusPermanentRedirect, codes.Unset, false}, |
| 51 |
{http.StatusBadRequest, codes.Error, false}, |
| 52 |
{http.StatusUnauthorized, codes.Error, false}, |
| 53 |
{http.StatusPaymentRequired, codes.Error, false}, |
| 54 |
{http.StatusForbidden, codes.Error, false}, |
| 55 |
{http.StatusNotFound, codes.Error, false}, |
| 56 |
{http.StatusMethodNotAllowed, codes.Error, false}, |
| 57 |
{http.StatusNotAcceptable, codes.Error, false}, |
| 58 |
{http.StatusProxyAuthRequired, codes.Error, false}, |
| 59 |
{http.StatusRequestTimeout, codes.Error, false}, |
| 60 |
{http.StatusConflict, codes.Error, false}, |
| 61 |
{http.StatusGone, codes.Error, false}, |
| 62 |
{http.StatusLengthRequired, codes.Error, false}, |
| 63 |
{http.StatusPreconditionFailed, codes.Error, false}, |
| 64 |
{http.StatusRequestEntityTooLarge, codes.Error, false}, |
| 65 |
{http.StatusRequestURITooLong, codes.Error, false}, |
| 66 |
{http.StatusUnsupportedMediaType, codes.Error, false}, |
| 67 |
{http.StatusRequestedRangeNotSatisfiable, codes.Error, false}, |
| 68 |
{http.StatusExpectationFailed, codes.Error, false}, |
| 69 |
{http.StatusTeapot, codes.Error, false}, |
| 70 |
{http.StatusMisdirectedRequest, codes.Error, false}, |
| 71 |
{http.StatusUnprocessableEntity, codes.Error, false}, |
| 72 |
{http.StatusLocked, codes.Error, false}, |
| 73 |
{http.StatusFailedDependency, codes.Error, false}, |
| 74 |
{http.StatusTooEarly, codes.Error, false}, |
| 75 |
{http.StatusUpgradeRequired, codes.Error, false}, |
| 76 |
{http.StatusPreconditionRequired, codes.Error, false}, |
| 77 |
{http.StatusTooManyRequests, codes.Error, false}, |
| 78 |
{http.StatusRequestHeaderFieldsTooLarge, codes.Error, false}, |
| 79 |
{http.StatusUnavailableForLegalReasons, codes.Error, false}, |
| 80 |
{499, codes.Error, false}, |
| 81 |
{http.StatusInternalServerError, codes.Error, false}, |
| 82 |
{http.StatusNotImplemented, codes.Error, false}, |
| 83 |
{http.StatusBadGateway, codes.Error, false}, |
| 84 |
{http.StatusServiceUnavailable, codes.Error, false}, |
| 85 |
{http.StatusGatewayTimeout, codes.Error, false}, |
| 86 |
{http.StatusHTTPVersionNotSupported, codes.Error, false}, |
| 87 |
{http.StatusVariantAlsoNegotiates, codes.Error, false}, |
| 88 |
{http.StatusInsufficientStorage, codes.Error, false}, |
| 89 |
{http.StatusLoopDetected, codes.Error, false}, |
| 90 |
{http.StatusNotExtended, codes.Error, false}, |
| 91 |
{http.StatusNetworkAuthenticationRequired, codes.Error, false}, |
| 92 |
{600, codes.Error, true}, |
| 93 |
} |
| 94 |
|
| 95 |
for _, test := range tests { |
| 96 |
t.Run(strconv.Itoa(test.code), func(t *testing.T) { |
| 97 |
c, msg := HTTPClient{}.Status(test.code) |
| 98 |
assert.Equal(t, test.stat, c) |
| 99 |
if test.msg && msg == "" { |
| 100 |
t.Errorf("expected non-empty message for %d", test.code) |
| 101 |
} else if !test.msg && msg != "" { |
| 102 |
t.Errorf("expected empty message for %d, got: %s", test.code, msg) |
| 103 |
} |
| 104 |
}) |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
func TestHTTPClient_MetricAttributes(t *testing.T) { |
| 109 |
defaultRequest, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "http://example.com/path?query=test", http.NoBody) |
| 110 |
require.NoError(t, err) |
| 111 |
httpsRequest, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "https://example.com/path?query=test", http.NoBody) |
| 112 |
require.NoError(t, err) |
| 113 |
|
| 114 |
tests := []struct { |
| 115 |
name string |
| 116 |
server string |
| 117 |
req *http.Request |
| 118 |
statusCode int |
| 119 |
additionalAttributes []attribute.KeyValue |
| 120 |
wantFunc func(t *testing.T, attrs []attribute.KeyValue) |
| 121 |
}{ |
| 122 |
{ |
| 123 |
name: "routine testing", |
| 124 |
req: defaultRequest, |
| 125 |
statusCode: 200, |
| 126 |
additionalAttributes: []attribute.KeyValue{attribute.String("test", "test")}, |
| 127 |
wantFunc: func(t *testing.T, attrs []attribute.KeyValue) { |
| 128 |
require.Len(t, attrs, 7) |
| 129 |
assert.ElementsMatch(t, []attribute.KeyValue{ |
| 130 |
attribute.String("http.request.method", "GET"), |
| 131 |
attribute.String("server.address", "example.com"), |
| 132 |
attribute.String("url.scheme", "http"), |
| 133 |
attribute.String("network.protocol.name", "http"), |
| 134 |
attribute.String("network.protocol.version", "1.1"), |
| 135 |
attribute.Int64("http.response.status_code", 200), |
| 136 |
attribute.String("test", "test"), |
| 137 |
}, attrs) |
| 138 |
}, |
| 139 |
}, |
| 140 |
{ |
| 141 |
name: "use server address", |
| 142 |
req: defaultRequest, |
| 143 |
statusCode: 200, |
| 144 |
additionalAttributes: nil, |
| 145 |
wantFunc: func(t *testing.T, attrs []attribute.KeyValue) { |
| 146 |
require.Len(t, attrs, 6) |
| 147 |
assert.ElementsMatch(t, []attribute.KeyValue{ |
| 148 |
attribute.String("http.request.method", "GET"), |
| 149 |
attribute.String("server.address", "example.com"), |
| 150 |
attribute.String("url.scheme", "http"), |
| 151 |
attribute.String("network.protocol.name", "http"), |
| 152 |
attribute.String("network.protocol.version", "1.1"), |
| 153 |
attribute.Int64("http.response.status_code", 200), |
| 154 |
}, attrs) |
| 155 |
}, |
| 156 |
}, |
| 157 |
{ |
| 158 |
name: "https scheme", |
| 159 |
req: httpsRequest, |
| 160 |
statusCode: 200, |
| 161 |
additionalAttributes: nil, |
| 162 |
wantFunc: func(t *testing.T, attrs []attribute.KeyValue) { |
| 163 |
require.Len(t, attrs, 6) |
| 164 |
assert.ElementsMatch(t, []attribute.KeyValue{ |
| 165 |
attribute.String("http.request.method", "GET"), |
| 166 |
attribute.String("server.address", "example.com"), |
| 167 |
attribute.String("url.scheme", "https"), |
| 168 |
attribute.String("network.protocol.name", "http"), |
| 169 |
attribute.String("network.protocol.version", "1.1"), |
| 170 |
attribute.Int64("http.response.status_code", 200), |
| 171 |
}, attrs) |
| 172 |
}, |
| 173 |
}, |
| 174 |
} |
| 175 |
|
| 176 |
for _, tt := range tests { |
| 177 |
t.Run(tt.name, func(t *testing.T) { |
| 178 |
got := HTTPClient{}.MetricAttributes(tt.req, tt.statusCode, tt.additionalAttributes) |
| 179 |
tt.wantFunc(t, got) |
| 180 |
}) |
| 181 |
} |
| 182 |
} |