This page guides you through the steps of configuring your Spring Boot application to work with Apitally. If you don’t have an account yet, now would be a good time to sign up.

Once you’re done with this guide, you will be able to:

  • Analyze traffic to your application
  • Keep track of errors, response times and payload sizes
  • Understand how individual API consumers use your application
  • Log and inspect each API request and response
  • Monitor your application’s uptime and receive alerts when it’s down
  • Set up custom alert rules and get notified when they’re triggered

Create app

To get started, create a new app in the Apitally dashboard and select as your framework.

Here you can also configure the environments (e.g. prod and dev) for your app, or simply accept the defaults.

Take a note of the client ID displayed after submitting. You will need it in the next step.

Add dependency

Add the Apitally client library for Java to your project’s dependencies.

<dependency>
  <groupId>io.apitally</groupId>
  <artifactId>apitally</artifactId>
  <version>[0.1.0,)</version>
</dependency>

Enable Apitally

Add the @UseApitally annotation to your Spring Boot application class and provide the client ID for your app in the configuration. You’ll find the client ID on the Setup instructions page in the Apitally dashboard, which is displayed immediately after creating the app.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import io.apitally.spring.UseApitally;

@UseApitally
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Deploy your application with these changes, or restart if you're testing locally.

At this point the basic setup for your application is complete and you will start seeing data in the Apitally dashboard. However, you won't be able to analyze API consumers yet.

Identify consumers

In order to analyze API traffic by consumers in Apitally, your application must identify which consumers requests are coming from.

How you identify API consumers depends on your application and use case. If your application uses authentication, it would make sense to use the authenticated identity (e.g. username) as the consumer identifier.

To associate requests with consumers, set the apitallyConsumer attribute on the request. You could do this in a filter or interceptor, for example.

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.filter.OncePerRequestFilter;

public class ConsumerIdentificationFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth != null && auth.isAuthenticated()) {
            request.setAttribute("apitallyConsumer", auth.getName());
        }
        chain.doFilter(request, response);
    }
}

The consumer identifier should be a string (max. 128 characters long) that uniquely identifies the consumer of the API, e.g. customer-123. Once the first request is received from a consumer, it will show up in the Apitally dashboard.

Now the Consumers page in the Apitally dashboard shows you information about all consumers that have made requests to your application. You can also filter insights on other the dashboards by consumer and better understand how each of them use your application.

Name and group consumers

By default, Apitally generates a name for new consumers from the consumer identifier. You can always change it in the Apitally dashboard, and you can assign consumers to groups there too.

If you have many consumers, you may prefer to set a name and group programmatically from your application.

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.filter.OncePerRequestFilter;

import io.apitally.spring.ApitallyConsumer;

public class ConsumerIdentificationFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth != null && auth.isAuthenticated()) {
            User user = (User) auth.getPrincipal();
            request.setAttribute("apitallyConsumer", new ApitallyConsumer(
                user.getUsername(), // identifier
                user.getFullName(), // name
                user.getRole() // group
            ));
        }
        chain.doFilter(request, response);
    }
}

Configure request logging

The request log is an opt-in feature available on our paid plans only.

Logging of individual requests and responses is disabled by default to protect potentially sensitive data. You can enable it by providing a RequestLoggingConfig object, which allows you to configure in detail what parts of the request and response should be logged. There are also options for masking sensitive information and excluding certain requests from logging.

apitally:
  client-id: "your-client-id"
  env: "dev" # or "prod" etc.
  request-logging:
    enabled: true
    query-params-included: true
    request-headers-included: true
    request-body-included: true
    response-headers-included: true
    response-body-included: true