Project

General

Profile

« Previous | Next » 

Revision 56601

Adding metrics with micrometer

View differences:

SearchRequestController.java
1 1
package eu.dnetlib.data.search.web.api;
2 2

  
3 3
import com.google.common.collect.Iterables;
4
import eu.dnetlib.api.data.SearchService;
4 5
import eu.dnetlib.api.data.SearchServiceException;
5
import eu.dnetlib.data.search.app.SearchServiceImpl;
6 6
import eu.dnetlib.data.search.utils.cql.ParameterQueryEnhancer;
7 7
import eu.dnetlib.data.search.utils.vocabulary.VocabularyManager;
8 8
import eu.dnetlib.domain.data.FormattedSearchResult;
9
import io.micrometer.core.annotation.Timed;
10
import io.micrometer.prometheus.PrometheusMeterRegistry;
9 11
import org.apache.commons.collections.ListUtils;
10
import org.apache.commons.io.IOUtils;
11 12
import org.apache.commons.lang.IncompleteArgumentException;
12 13
import org.apache.commons.lang.StringEscapeUtils;
13 14
import org.apache.log4j.Logger;
14 15
import org.springframework.beans.factory.annotation.Autowired;
15
import org.springframework.http.*;
16
import org.springframework.context.annotation.EnableAspectJAutoProxy;
17
import org.springframework.http.HttpStatus;
18
import org.springframework.http.MediaType;
19
import org.springframework.http.ResponseEntity;
16 20
import org.springframework.stereotype.Controller;
17 21
import org.springframework.ui.ModelMap;
18 22
import org.springframework.web.bind.annotation.ExceptionHandler;
......
23 27
import javax.annotation.Resource;
24 28
import javax.servlet.http.HttpServletRequest;
25 29
import javax.servlet.http.HttpServletResponse;
26
import java.io.IOException;
27 30
import java.io.PrintWriter;
28 31
import java.text.SimpleDateFormat;
29 32
import java.util.*;
30 33

  
31 34
@Controller
35
@EnableAspectJAutoProxy
36
@Timed
32 37
public class SearchRequestController {
33 38

  
34 39
    @Autowired
35
    private SearchServiceImpl searchService = null;
40
    private SearchService searchService = null;
36 41
    @Autowired
37 42
    private VocabularyManager vocabularyManager = null;
38 43

  
......
42 47
    @Resource
43 48
    private String maxSize = null;
44 49

  
50
    @Autowired
51
    private PrometheusMeterRegistry registry;
52

  
45 53
    private static Logger logger = Logger.getLogger(SearchRequestController.class);
46 54

  
47 55
    private static final String XML_CONTENT_TYPE = "application/xml;charset=UTF-8";
......
77 85

  
78 86
    //TODO this is for joomla - to be removed soon
79 87
    @RequestMapping(value = "/search", method = RequestMethod.GET)
88
    @Timed(value = "search.joomla.requests", longTask = false)
80 89
    public void search(HttpServletRequest request, HttpServletResponse response) {
81 90
        PrintWriter writer = null;
82 91
        FormattedSearchResult formattedSearchResult = null;
......
88 97

  
89 98
        String locale = request.getParameter("locale");
90 99

  
91
        int page= 0;
100
        int page = 0;
92 101
        int size = 0;
93 102
        //TODO check paging
94 103
        if (!action.equals("refine")) {
......
190 199
    }
191 200

  
192 201
    @RequestMapping(value = "/api/publications", method = RequestMethod.GET, produces = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE})
202
    @Timed(value = "http.server.request.duration", extraTags = {"referer", "api", "uri", "/api/publications"}, longTask = false)
193 203
    public void searchPublications(HttpServletRequest request, HttpServletResponse response) throws Exception {
194 204

  
195 205
        long time = System.currentTimeMillis();
......
239 249

  
240 250
        PrintWriter writer = response.getWriter();
241 251
        writer.append(formattedSearchResult.getFormattedResult());
252

  
242 253
        writer.close();
243 254

  
244 255
        time = System.currentTimeMillis() - time;
245 256
        logger.debug("Answer old time " + time);
246 257
    }
247 258

  
259

  
248 260
    @RequestMapping(value = "/api/datasets", method = RequestMethod.GET, produces = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE})
261
    @Timed(value = "http.server.request.duration", extraTags = {"referer", "api", "uri", "/api/datasets"}, longTask = false)
249 262
    public void searchData(HttpServletRequest request, HttpServletResponse response) throws Exception {
250 263

  
251 264
        checkParameters(ListUtils.union(PUB_N_DATA_COMMON_PARAMETERS, DATA_PARAMETERS),request.getParameterMap());
......
298 311
    }
299 312

  
300 313
    @RequestMapping(value = "/api/software", method = RequestMethod.GET, produces = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE})
314
    @Timed(value = "http.server.request.duration", extraTags = {"referer", "api", "uri", "/api/software"}, longTask = false)
301 315
    public void searchSoftware(HttpServletRequest request, HttpServletResponse response) throws Exception {
302 316

  
303 317
        checkParameters(ListUtils.union(PUB_N_DATA_COMMON_PARAMETERS, SOFTWARE_PARAMETERS),request.getParameterMap());
......
351 365
    }
352 366

  
353 367
    @RequestMapping(value = "/api/other", method = RequestMethod.GET, produces = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE})
368
    @Timed(value = "http.server.request.duration", extraTags = {"referer", "api", "uri", "/api/other"}, longTask = false)
354 369
    public void searchOther(HttpServletRequest request, HttpServletResponse response) throws Exception {
355 370

  
356 371
        checkParameters(ListUtils.union(PUB_N_DATA_COMMON_PARAMETERS, OTHER_PARAMETERS),request.getParameterMap());
......
390 405

  
391 406
        FormattedSearchResult formattedSearchResult = null;
392 407

  
408
        //long start = System.currentTimeMillis();
393 409
        try {
410
            //Timer.Sample sample = Timer.start(metrics.getRegistry());
394 411
            formattedSearchResult =  searchService.search(queryBuilder.toString(),sTransformer, (newFormat!=null)?newFormat:format, locale, page, size);
395 412

  
396 413
        } catch (Exception e) {
......
400 417

  
401 418
        PrintWriter writer = response.getWriter();
402 419
        writer.append(formattedSearchResult.getFormattedResult());
403

  
404 420
    }
405 421

  
406 422
    @RequestMapping(value = "/api/projects", method = RequestMethod.GET, produces = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE})
423
    @Timed(value = "http.server.request.duration", extraTags = {"referer", "api", "uri", "/api/projects"}, longTask = false)
407 424
    public void searchProjects(HttpServletRequest request, HttpServletResponse response) throws Exception {
408 425

  
409 426
        checkParameters(PROJECT_PARAMETERS, request.getParameterMap());
......
439 456
            formattedSearchResult = searchService.search(queryBuilder.toString(),sTransformer, format, locale, page, size);
440 457

  
441 458
        } catch (SearchServiceException e) {
442
            throw new Exception("Fail to execute search");
459
            throw new Exception("Fail to execute search", e);
443 460
        }
444 461

  
445 462
        PrintWriter writer = response.getWriter();
......
458 475

  
459 476
        String format = (request.getParameter("format") == null)? "xml": request.getParameter("format").toLowerCase();
460 477

  
478
        registry.counter("http.status.400", "400", "uri").increment();
479

  
461 480
        return new ResponseEntity<Error>(response, HttpStatus.BAD_REQUEST);
462 481
    }
463 482

  
......
793 812
        time = System.currentTimeMillis() - time;
794 813
        System.out.println("Answer time " + time);
795 814
    }*/
815

  
796 816
}

Also available in: Unified diff