20
20
import java .io .IOException ;
21
21
22
22
import io .micrometer .common .KeyValue ;
23
+ import io .micrometer .common .KeyValues ;
23
24
import io .micrometer .observation .Observation ;
24
25
import org .junit .jupiter .api .Test ;
25
26
26
27
import org .springframework .http .HttpMethod ;
28
+ import org .springframework .http .HttpStatusCode ;
27
29
import org .springframework .http .client .ClientHttpRequest ;
28
30
import org .springframework .http .client .ClientHttpResponse ;
29
31
import org .springframework .web .testfixture .http .client .MockClientHttpRequest ;
@@ -42,6 +44,8 @@ class DefaultClientRequestObservationConventionTests {
42
44
43
45
private final MockClientHttpRequest request = new MockClientHttpRequest (HttpMethod .GET , "/test" );
44
46
47
+ private final static MockClientHttpResponse response = new MockClientHttpResponse ();
48
+
45
49
private final DefaultClientRequestObservationConvention observationConvention = new DefaultClientRequestObservationConvention ();
46
50
47
51
@ Test
@@ -65,7 +69,7 @@ void supportsOnlyClientHttpObservationContext() {
65
69
@ Test
66
70
void addsKeyValuesForRequestWithUriTemplate () {
67
71
ClientRequestObservationContext context = createContext (
68
- new MockClientHttpRequest (HttpMethod .GET , "/resource/{id}" , 42 ), new MockClientHttpResponse () );
72
+ new MockClientHttpRequest (HttpMethod .GET , "/resource/{id}" , 42 ), response );
69
73
context .setUriTemplate ("/resource/{id}" );
70
74
assertThat (this .observationConvention .getLowCardinalityKeyValues (context ))
71
75
.contains (KeyValue .of ("exception" , "none" ), KeyValue .of ("method" , "GET" ), KeyValue .of ("uri" , "/resource/{id}" ),
@@ -76,7 +80,7 @@ void addsKeyValuesForRequestWithUriTemplate() {
76
80
@ Test
77
81
void addsKeyValuesForRequestWithUriTemplateWithHost () {
78
82
ClientRequestObservationContext context = createContext (
79
- new MockClientHttpRequest (HttpMethod .GET , "https://example.org/resource/{id}" , 42 ), new MockClientHttpResponse () );
83
+ new MockClientHttpRequest (HttpMethod .GET , "https://example.org/resource/{id}" , 42 ), response );
80
84
context .setUriTemplate ("https://example.org/resource/{id}" );
81
85
assertThat (this .observationConvention .getLowCardinalityKeyValues (context ))
82
86
.contains (KeyValue .of ("exception" , "none" ), KeyValue .of ("method" , "GET" ), KeyValue .of ("uri" , "/resource/{id}" ),
@@ -88,7 +92,7 @@ void addsKeyValuesForRequestWithUriTemplateWithHost() {
88
92
@ Test
89
93
void addsKeyValuesForRequestWithoutUriTemplate () {
90
94
ClientRequestObservationContext context = createContext (
91
- new MockClientHttpRequest (HttpMethod .GET , "/resource/42" ), new MockClientHttpResponse () );
95
+ new MockClientHttpRequest (HttpMethod .GET , "/resource/42" ), response );
92
96
assertThat (this .observationConvention .getLowCardinalityKeyValues (context ))
93
97
.contains (KeyValue .of ("method" , "GET" ), KeyValue .of ("client.name" , "none" ), KeyValue .of ("uri" , "none" ));
94
98
assertThat (this .observationConvention .getHighCardinalityKeyValues (context )).contains (KeyValue .of ("http.url" , "/resource/42" ));
@@ -98,19 +102,60 @@ void addsKeyValuesForRequestWithoutUriTemplate() {
98
102
void addsClientNameForRequestWithHost () {
99
103
ClientRequestObservationContext context = createContext (
100
104
new MockClientHttpRequest (HttpMethod .GET , "https://localhost:8080/resource/42" ),
101
- new MockClientHttpResponse () );
105
+ response );
102
106
assertThat (this .observationConvention .getLowCardinalityKeyValues (context )).contains (KeyValue .of ("client.name" , "localhost" ));
103
107
}
104
108
105
109
@ Test
106
110
void addsKeyValueForNonResolvableStatus () throws Exception {
107
- ClientRequestObservationContext context = new ClientRequestObservationContext (this .request );
108
111
ClientHttpResponse response = mock ();
109
- context . setResponse ( response );
112
+ ClientRequestObservationContext context = createContext ( this . request , response );
110
113
given (response .getStatusCode ()).willThrow (new IOException ("test error" ));
111
114
assertThat (this .observationConvention .getLowCardinalityKeyValues (context )).contains (KeyValue .of ("status" , "IO_ERROR" ));
112
115
}
113
116
117
+ @ Test
118
+ void addKeyValueForNon200Response () {
119
+ MockClientHttpResponse response = new MockClientHttpResponse (new byte [0 ], HttpStatusCode .valueOf (400 ));
120
+ ClientRequestObservationContext context = createContext (request , response );
121
+ KeyValues lowCardinality = this .observationConvention .getLowCardinalityKeyValues (context );
122
+ assertThat (lowCardinality ).contains (KeyValue .of ("status" , "400" ));
123
+ }
124
+
125
+ @ Test
126
+ void addKeyValuesForUnknownStatusResponse () {
127
+ MockClientHttpResponse response = new MockClientHttpResponse (new byte [0 ], 512 );
128
+ ClientRequestObservationContext context = createContext (request , response );
129
+ KeyValues lowCardinalityKeyValues = this .observationConvention .getLowCardinalityKeyValues (context );
130
+ assertThat (lowCardinalityKeyValues ).contains (KeyValue .of ("status" , "512" ), KeyValue .of ("outcome" , "UNKNOWN" ));
131
+ }
132
+
133
+ @ Test
134
+ void addKeyValuesForRequestNull () {
135
+ ClientRequestObservationContext context = createContext (null , response );
136
+ KeyValues highCardinality = this .observationConvention .getHighCardinalityKeyValues (context );
137
+ assertThat (highCardinality ).contains (KeyValue .of ("http.url" , "none" ));
138
+ KeyValues lowCardinality = this .observationConvention .getLowCardinalityKeyValues (context );
139
+ assertThat (lowCardinality ).contains (KeyValue .of ("method" , "none" ), KeyValue .of ("exception" , "none" ),
140
+ KeyValue .of ("client.name" , "none" ), KeyValue .of ("uri" , "none" )
141
+ );
142
+ }
143
+
144
+ @ Test
145
+ void addKeyValueForResponseNull () {
146
+ ClientRequestObservationContext context = createContext (this .request , null );
147
+ KeyValues lowCardinality = this .observationConvention .getLowCardinalityKeyValues (context );
148
+ assertThat (lowCardinality ).contains (KeyValue .of ("status" , "CLIENT_ERROR" ));
149
+ }
150
+
151
+ @ Test
152
+ void addKeyValueForContextError () {
153
+ ClientRequestObservationContext context = createContext (this .request , response );
154
+ context .setError (new RuntimeException ("error" ));
155
+ KeyValues lowCardinality = this .observationConvention .getLowCardinalityKeyValues (context );
156
+ assertThat (lowCardinality ).contains (KeyValue .of ("exception" , "RuntimeException" ));
157
+ }
158
+
114
159
private ClientRequestObservationContext createContext (ClientHttpRequest request , ClientHttpResponse response ) {
115
160
ClientRequestObservationContext context = new ClientRequestObservationContext (request );
116
161
context .setResponse (response );
0 commit comments