3434import com .bolyuba .nexus .plugin .npm .service .internal .MetadataParser ;
3535import com .bolyuba .nexus .plugin .npm .service .internal .PackageRootIterator ;
3636import com .bolyuba .nexus .plugin .npm .service .internal .ProxyMetadataTransport ;
37+ import com .google .common .base .Stopwatch ;
38+ import com .yammer .metrics .Metrics ;
39+ import com .yammer .metrics .core .MetricsRegistry ;
40+ import com .yammer .metrics .core .Timer ;
41+ import com .yammer .metrics .core .TimerContext ;
3742import org .apache .http .HttpResponse ;
3843import org .apache .http .HttpStatus ;
44+ import org .apache .http .StatusLine ;
3945import org .apache .http .client .HttpClient ;
4046import org .apache .http .client .methods .HttpGet ;
47+ import org .apache .http .client .methods .HttpUriRequest ;
4148import org .apache .http .client .protocol .HttpClientContext ;
4249import org .apache .http .util .EntityUtils ;
4350import org .slf4j .Logger ;
@@ -61,12 +68,15 @@ public class HttpProxyMetadataTransport
6168
6269 private final HttpClientManager httpClientManager ;
6370
71+ private final MetricsRegistry metricsRegistry ;
72+
6473 @ Inject
6574 public HttpProxyMetadataTransport (final MetadataParser metadataParser ,
6675 final HttpClientManager httpClientManager )
6776 {
6877 this .metadataParser = checkNotNull (metadataParser );
6978 this .httpClientManager = checkNotNull (httpClientManager );
79+ this .metricsRegistry = Metrics .defaultRegistry ();
7080 }
7181
7282 /**
@@ -82,15 +92,38 @@ public PackageRootIterator fetchRegistryRoot(final NpmProxyRepository npmProxyRe
8292 try {
8393 final HttpGet get = new HttpGet (
8494 buildUri (npmProxyRepository , "-/all" )); // TODO: this in NPM specific, might try both root and NPM api
85- outboundRequestLog .debug ("{} - NPM GET {}" , npmProxyRepository .getId (), get .getURI ());
8695 get .addHeader ("accept" , NpmRepository .JSON_MIME_TYPE );
8796 final HttpClientContext context = new HttpClientContext ();
8897 context .setAttribute (Hc4Provider .HTTP_CTX_KEY_REPOSITORY , npmProxyRepository );
89- final HttpResponse httpResponse = httpClient .execute (get , context );
98+
99+ final Timer timer = timer (get , npmProxyRepository .getRemoteUrl ());
100+ final TimerContext timerContext = timer .time ();
101+ Stopwatch stopwatch = null ;
102+
103+ if (outboundRequestLog .isDebugEnabled ()) {
104+ outboundRequestLog .debug ("[{}] {} {}" , npmProxyRepository .getId (), get .getMethod (), get .getURI ());
105+ stopwatch = Stopwatch .createStarted ();
106+ }
107+
108+ final HttpResponse httpResponse ;
109+ try {
110+ httpResponse = httpClient .execute (get , context );
111+ }
112+ finally {
113+ timerContext .stop ();
114+ if (stopwatch != null ) {
115+ stopwatch .stop ();
116+ }
117+ }
118+
119+ final StatusLine statusLine = httpResponse .getStatusLine ();
120+ if (outboundRequestLog .isDebugEnabled ()) {
121+ outboundRequestLog .debug ("[{}] {} {} -> {}; {}" , npmProxyRepository .getId (), get .getMethod (), get .getURI (),
122+ statusLine , stopwatch );
123+ }
124+
90125 try {
91- outboundRequestLog .debug ("{} - NPM GET {} - {}" , npmProxyRepository .getId (), get .getURI (),
92- httpResponse .getStatusLine ());
93- if (httpResponse .getStatusLine ().getStatusCode () == HttpStatus .SC_OK ) {
126+ if (statusLine .getStatusCode () == HttpStatus .SC_OK ) {
94127 final File tempFile = File
95128 .createTempFile (npmProxyRepository .getId () + "-root" , "temp.json" ,
96129 metadataParser .getTemporaryDirectory ());
@@ -102,7 +135,11 @@ public PackageRootIterator fetchRegistryRoot(final NpmProxyRepository npmProxyRe
102135 final FileContentLocator cl = new FileContentLocator (tempFile , NpmRepository .JSON_MIME_TYPE , true );
103136 return metadataParser .parseRegistryRoot (npmProxyRepository .getId (), cl );
104137 }
105- throw new IOException ("Unexpected response from registry root " + httpResponse .getStatusLine ());
138+ if (log .isDebugEnabled ()) {
139+ log .debug ("[{}] {} {} -> unexpected: {}" , npmProxyRepository .getId (), get .getMethod (), get .getURI (),
140+ statusLine );
141+ }
142+ throw new IOException ("Unexpected response from registry root " + statusLine );
106143 }
107144 finally {
108145 EntityUtils .consumeQuietly (httpResponse .getEntity ());
@@ -126,21 +163,44 @@ public PackageRoot fetchPackageRoot(final NpmProxyRepository npmProxyRepository,
126163 npmProxyRepository .getRemoteStorageContext ());
127164 try {
128165 final HttpGet get = new HttpGet (buildUri (npmProxyRepository , packageName ));
129- outboundRequestLog .debug ("{} - NPM GET {}" , npmProxyRepository .getId (), get .getURI ());
130166 get .addHeader ("accept" , NpmRepository .JSON_MIME_TYPE );
131167 if (expired != null && expired .getProperties ().containsKey (PROP_ETAG )) {
132168 get .addHeader ("if-none-match" , expired .getProperties ().get (PROP_ETAG ));
133169 }
134170 final HttpClientContext context = new HttpClientContext ();
135171 context .setAttribute (Hc4Provider .HTTP_CTX_KEY_REPOSITORY , npmProxyRepository );
136- final HttpResponse httpResponse = httpClient .execute (get , context );
172+
173+ final Timer timer = timer (get , npmProxyRepository .getRemoteUrl ());
174+ final TimerContext timerContext = timer .time ();
175+ Stopwatch stopwatch = null ;
176+
177+ if (outboundRequestLog .isDebugEnabled ()) {
178+ outboundRequestLog .debug ("[{}] {} {}" , npmProxyRepository .getId (), get .getMethod (), get .getURI ());
179+ stopwatch = Stopwatch .createStarted ();
180+ }
181+
182+ final HttpResponse httpResponse ;
183+ try {
184+ httpResponse = httpClient .execute (get , context );
185+ }
186+ finally {
187+ timerContext .stop ();
188+ if (stopwatch != null ) {
189+ stopwatch .stop ();
190+ }
191+ }
192+
193+ final StatusLine statusLine = httpResponse .getStatusLine ();
194+ if (outboundRequestLog .isDebugEnabled ()) {
195+ outboundRequestLog .debug ("[{}] {} {} -> {}; {}" , npmProxyRepository .getId (), get .getMethod (), get .getURI (),
196+ statusLine , stopwatch );
197+ }
198+
137199 try {
138- outboundRequestLog .debug ("{} - NPM GET {} - {}" , npmProxyRepository .getId (), get .getURI (),
139- httpResponse .getStatusLine ());
140- if (httpResponse .getStatusLine ().getStatusCode () == HttpStatus .SC_NOT_MODIFIED ) {
200+ if (statusLine .getStatusCode () == HttpStatus .SC_NOT_MODIFIED ) {
141201 return expired ;
142202 }
143- if (httpResponse . getStatusLine () .getStatusCode () == HttpStatus .SC_OK ) {
203+ if (statusLine .getStatusCode () == HttpStatus .SC_OK ) {
144204 final PreparedContentLocator pcl = new PreparedContentLocator (httpResponse .getEntity ().getContent (),
145205 NpmRepository .JSON_MIME_TYPE , ContentLocator .UNKNOWN_LENGTH );
146206 final PackageRoot fresh = metadataParser .parsePackageRoot (npmProxyRepository .getId (), pcl );
@@ -172,4 +232,8 @@ private String buildUri(final NpmProxyRepository npmProxyRepository, final Strin
172232 return registryUrl + "/" + pathElem ;
173233 }
174234 }
235+
236+ private Timer timer (final HttpUriRequest httpRequest , final String baseUrl ) {
237+ return metricsRegistry .newTimer (HttpProxyMetadataTransport .class , baseUrl , httpRequest .getMethod ());
238+ }
175239}
0 commit comments