21
21
import io .micrometer .common .KeyValue ;
22
22
import io .micrometer .common .KeyValues ;
23
23
24
+ import org .springframework .http .HttpStatus ;
25
+ import org .springframework .http .HttpStatusCode ;
24
26
import org .springframework .http .client .ClientHttpResponse ;
25
- import org .springframework .http .observation .HttpOutcome ;
26
27
import org .springframework .util .StringUtils ;
27
28
28
29
/**
29
- * Default implementation for a {@link ClientHttpObservationConvention },
30
- * extracting information from the {@link ClientHttpObservationContext }.
30
+ * Default implementation for a {@link ClientRequestObservationConvention },
31
+ * extracting information from the {@link ClientRequestObservationContext }.
31
32
*
32
33
* @author Brian Clozel
33
34
* @since 6.0
34
35
*/
35
- public class DefaultClientHttpObservationConvention implements ClientHttpObservationConvention {
36
+ public class DefaultClientRequestObservationConvention implements ClientRequestObservationConvention {
36
37
37
38
private static final String DEFAULT_NAME = "http.client.requests" ;
38
39
@@ -44,26 +45,31 @@ public class DefaultClientHttpObservationConvention implements ClientHttpObserva
44
45
45
46
private static final KeyValue STATUS_CLIENT_ERROR = KeyValue .of (ClientHttpObservationDocumentation .LowCardinalityKeyNames .STATUS , "CLIENT_ERROR" );
46
47
48
+ private static final KeyValue HTTP_OUTCOME_SUCCESS = KeyValue .of (ClientHttpObservationDocumentation .LowCardinalityKeyNames .OUTCOME , "SUCCESS" );
49
+
50
+ private static final KeyValue HTTP_OUTCOME_UNKNOWN = KeyValue .of (ClientHttpObservationDocumentation .LowCardinalityKeyNames .OUTCOME , "UNKNOWN" );
51
+
47
52
private static final KeyValue EXCEPTION_NONE = KeyValue .of (ClientHttpObservationDocumentation .LowCardinalityKeyNames .EXCEPTION , "none" );
48
53
49
54
private static final KeyValue HTTP_URL_NONE = KeyValue .of (ClientHttpObservationDocumentation .HighCardinalityKeyNames .HTTP_URL , "none" );
50
55
51
56
private static final KeyValue CLIENT_NAME_NONE = KeyValue .of (ClientHttpObservationDocumentation .HighCardinalityKeyNames .CLIENT_NAME , "none" );
52
57
58
+
53
59
private final String name ;
54
60
55
61
/**
56
62
* Create a convention with the default name {@code "http.client.requests"}.
57
63
*/
58
- public DefaultClientHttpObservationConvention () {
64
+ public DefaultClientRequestObservationConvention () {
59
65
this (DEFAULT_NAME );
60
66
}
61
67
62
68
/**
63
69
* Create a convention with a custom name.
64
70
* @param name the observation name
65
71
*/
66
- public DefaultClientHttpObservationConvention (String name ) {
72
+ public DefaultClientRequestObservationConvention (String name ) {
67
73
this .name = name ;
68
74
}
69
75
@@ -73,23 +79,23 @@ public String getName() {
73
79
}
74
80
75
81
@ Override
76
- public String getContextualName (ClientHttpObservationContext context ) {
82
+ public String getContextualName (ClientRequestObservationContext context ) {
77
83
return "http " + context .getCarrier ().getMethod ().name ().toLowerCase ();
78
84
}
79
85
80
86
@ Override
81
- public KeyValues getLowCardinalityKeyValues (ClientHttpObservationContext context ) {
87
+ public KeyValues getLowCardinalityKeyValues (ClientRequestObservationContext context ) {
82
88
return KeyValues .of (uri (context ), method (context ), status (context ), exception (context ), outcome (context ));
83
89
}
84
90
85
- protected KeyValue uri (ClientHttpObservationContext context ) {
91
+ protected KeyValue uri (ClientRequestObservationContext context ) {
86
92
if (context .getUriTemplate () != null ) {
87
93
return KeyValue .of (ClientHttpObservationDocumentation .LowCardinalityKeyNames .URI , context .getUriTemplate ());
88
94
}
89
95
return URI_NONE ;
90
96
}
91
97
92
- protected KeyValue method (ClientHttpObservationContext context ) {
98
+ protected KeyValue method (ClientRequestObservationContext context ) {
93
99
if (context .getCarrier () != null ) {
94
100
return KeyValue .of (ClientHttpObservationDocumentation .LowCardinalityKeyNames .METHOD , context .getCarrier ().getMethod ().name ());
95
101
}
@@ -98,7 +104,7 @@ protected KeyValue method(ClientHttpObservationContext context) {
98
104
}
99
105
}
100
106
101
- protected KeyValue status (ClientHttpObservationContext context ) {
107
+ protected KeyValue status (ClientRequestObservationContext context ) {
102
108
ClientHttpResponse response = context .getResponse ();
103
109
if (response == null ) {
104
110
return STATUS_CLIENT_ERROR ;
@@ -111,7 +117,7 @@ protected KeyValue status(ClientHttpObservationContext context) {
111
117
}
112
118
}
113
119
114
- protected KeyValue exception (ClientHttpObservationContext context ) {
120
+ protected KeyValue exception (ClientRequestObservationContext context ) {
115
121
Throwable error = context .getError ();
116
122
if (error != null ) {
117
123
String simpleName = error .getClass ().getSimpleName ();
@@ -121,36 +127,51 @@ protected KeyValue exception(ClientHttpObservationContext context) {
121
127
return EXCEPTION_NONE ;
122
128
}
123
129
124
- protected static KeyValue outcome (ClientHttpObservationContext context ) {
130
+ protected static KeyValue outcome (ClientRequestObservationContext context ) {
125
131
if (context .getResponse () != null ) {
126
132
try {
127
- HttpOutcome httpOutcome = HttpOutcome .forStatus (context .getResponse ().getStatusCode ());
128
- return httpOutcome .asKeyValue ();
133
+ return HttpOutcome .forStatus (context .getResponse ().getStatusCode ());
129
134
}
130
135
catch (IOException ex ) {
131
136
// Continue
132
137
}
133
138
}
134
- return HttpOutcome . UNKNOWN . asKeyValue () ;
139
+ return HTTP_OUTCOME_UNKNOWN ;
135
140
}
136
141
137
142
@ Override
138
- public KeyValues getHighCardinalityKeyValues (ClientHttpObservationContext context ) {
143
+ public KeyValues getHighCardinalityKeyValues (ClientRequestObservationContext context ) {
139
144
return KeyValues .of (requestUri (context ), clientName (context ));
140
145
}
141
146
142
- protected KeyValue requestUri (ClientHttpObservationContext context ) {
147
+ protected KeyValue requestUri (ClientRequestObservationContext context ) {
143
148
if (context .getCarrier () != null ) {
144
149
return KeyValue .of (ClientHttpObservationDocumentation .HighCardinalityKeyNames .HTTP_URL , context .getCarrier ().getURI ().toASCIIString ());
145
150
}
146
151
return HTTP_URL_NONE ;
147
152
}
148
153
149
- protected KeyValue clientName (ClientHttpObservationContext context ) {
154
+ protected KeyValue clientName (ClientRequestObservationContext context ) {
150
155
if (context .getCarrier () != null && context .getCarrier ().getURI ().getHost () != null ) {
151
156
return KeyValue .of (ClientHttpObservationDocumentation .HighCardinalityKeyNames .CLIENT_NAME , context .getCarrier ().getURI ().getHost ());
152
157
}
153
158
return CLIENT_NAME_NONE ;
154
159
}
155
160
161
+ static class HttpOutcome {
162
+
163
+ static KeyValue forStatus (HttpStatusCode statusCode ) {
164
+ if (statusCode .is2xxSuccessful ()) {
165
+ return HTTP_OUTCOME_SUCCESS ;
166
+ }
167
+ else if (statusCode instanceof HttpStatus status ){
168
+ return KeyValue .of ("outcome" , status .series ().name ());
169
+ }
170
+ else {
171
+ return HTTP_OUTCOME_UNKNOWN ;
172
+ }
173
+ }
174
+
175
+ }
176
+
156
177
}
0 commit comments