Search code examples
javarestspring-bootspring-boot-actuator

getting the metrics of an endpoint


I have an endpoint of my API, now I want to track that whenever a request is being made to my endpoint then how many were returning 200 and how many were returning 400 or other HTTP status code please advise how can I achieve the same in spring boot project, I am using spring boot actuators 2.

Suppose my endpoint is:

https://localhost:9090/users

So what I want is:

{  
    "404": 1,
    "200": 6,
    "409": 1
}

Solution

  • You can use /actuator/metrics/ to get all endPoints which are executed/called with their count, exception, outcome, status, total time, etc. For this case count, status is useful

    For that, you have to add a dependency in pom.xml or similar in grade

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
        <version>2.1.3.RELEASE</version>
    </dependency>
    

    Get Dependency


    To get details of all endPoint which are executed/called (see outcome all endPoint)

    localhost:8889/actuator/metrics/http.server.requests
    

    To get details of particular endPoint (see outcome particular endPoint)

    localhost:8889/actuator/metrics/http.server.requests?tag=uri:<endPoint>
    localhost:8889/actuator/metrics/http.server.requests?tag=uri:/users
    

    To get the count of particular endPoint with Status Code (see outcome particular endPoint with Status code)

    localhost:8889/actuator/metrics/http.server.requests?tag=uri:/users&tag=status:200
    

    outcome all endPoint

    {
        "name": "http.server.requests",
        "description": null,
        "baseUnit": "seconds",
        "measurements": [
            {
                "statistic": "COUNT",
                "value": 13
            },
            {
                "statistic": "TOTAL_TIME",
                "value": 0.42338
            },
            {
                "statistic": "MAX",
                "value": 0
            }
        ],
        "availableTags": [
            {
                "tag": "exception",
                "values": [
                    "None"
                ]
            },
            {
                "tag": "method",
                "values": [
                    "GET"
                ]
            },
            {
                "tag": "uri",
                "values": [
                    "/actuator/metrics/{requiredMetricName}",
                    "/getCountByStatus"
                ]
            },
            {
                "tag": "outcome",
                "values": [
                    "CLIENT_ERROR",
                    "SUCCESS"
                ]
            },
            {
                "tag": "status",
                "values": [
                    "404",
                    "200"
                ]
            }
        ]
    }
    

    see outcome particular endPoint

    {
        "name": "http.server.requests",
        "description": null,
        "baseUnit": "seconds",
        "measurements": [
            {
                "statistic": "COUNT",
                "value": 5
            },
            {
                "statistic": "TOTAL_TIME",
                "value": 0.1830878
            },
            {
                "statistic": "MAX",
                "value": 0
            }
        ],
        "availableTags": [
            {
                "tag": "exception",
                "values": [
                    "None"
                ]
            },
            {
                "tag": "method",
                "values": [
                    "GET"
                ]
            },
            {
                "tag": "outcome",
                "values": [
                    "CLIENT_ERROR",
                    "SUCCESS"
                ]
            },
            {
                "tag": "status",
                "values": [
                    "404",
                    "200"
                ]
            }
        ]
    }
    

    outcome particular endPoint with Status code

     {
            "name": "http.server.requests",
            "description": null,
            "baseUnit": "seconds",
            "measurements": [
                {
                    "statistic": "COUNT",
                    "value": 3
                },
                {
                    "statistic": "TOTAL_TIME",
                    "value": 0.034849
                },
                {
                    "statistic": "MAX",
                    "value": 0
                }
            ],
            "availableTags": [
                {
                    "tag": "exception",
                    "values": [
                        "None"
                    ]
                },
                {
                    "tag": "method",
                    "values": [
                        "GET"
                    ]
                },
                {
                    "tag": "outcome",
                    "values": [
                        "SUCCESS"
                    ]
                }
            ]
        }