Search

Dark theme | Light theme

February 20, 2025

Helidon SE Helpings: Serving Observe Endpoints On Different Port

When you enable the /observe endpoints you can configure them to be served on a different port than the application. By default the endpoints are available on the same port as the application. But you can define an extra named socket with another port number in the configuration of the WebServer instance. And in the configuration of the ObserveFeature instance you can define the socket name that should be used for the observe endpoints.

You can use configuration properties or code to configure the extra socket.

Using configuration

In the following example configuration properties file you see how to configure the extra socket and the observe feature to use the extra socket. A new socket with the name observe is defined and port 8081 is used by defining the property prefixed with server.sockets. The value is a list so you have to use a list definition for the property names name and port. Next this socket is used in the configuration of the observe feature by assigning the name to the server.features.observe.sockets property.

# File: application.properties
...
# Configure new socket for observe endpoints.
# Set the name of the new socket to observe.
server.sockets.0.name=observe
# Set the port for the new socket to 8081.
server.sockets.0.port=8081

# Configure the observe feature to use the observe socket.
server.features.observe.sockets.0=observe

# Set port for default socket.
server.port=8080
...

In your application code you can use the configuration to create a WebServer instance:

// File: Main.java
...
import io.helidon.config.Config;
import io.helidon.webserver.WebServer;
...
  public static void main(String[] args) {
    ...
    Config config = Config.create();

    WebServer server =
        WebServer.builder()
            ...
            // Read configuration properties.
            .config(config.get("server"))
            .build()
            .start();
  }
...

Using application code

Alternatively you can write the configuration in your application code. In the following example you can see how to create a WebServer instance using the configuration. To add a new socket definition you can use the WebServer.Builder.putSocket() method. Next you can use the ObserveFeature.Builder.sockets() method to connect the observe feature to the new socket.

// File: Main.java
...
import io.helidon.webserver.observe.ObserveFeature;
import io.helidon.webserver.observe.health.HealthObserver;
import io.helidon.webserver.WebServer;
...
  public static void main(String[] args) {
    ...
    // Create health check observer and enable details.
    HealthObserver healthObserver =
        HealthObserver.builder()
            // Show details in health endpoint.
            .details(true)
            .build();

    WebServer server =
        WebServer.builder()
            ...
            // Set default port.
            .port(8080)
            // Create a new socket with name "observe"
            // to run on port 8081.
            .putSocket("observe", socket -> socket.port(8081))
            .addFeature(
                ObserveFeature.builder()
                    // Set name of socket as defined in the WebServer builder.
                    // This will enable the /observe endpoints on port 8081.
                    .sockets(List.of("observe"))
                    .addObserver(healthObserver)
                    .build())
            .build()
            .start();
  }
...

When you start your application you must use port 8081 to access /observe endpoints.

$ curl -X GET http://localhost:8081/observe/health | jq -r .
{
  "status": "UP",
  "checks": []
}
$

Written with Helidon 4.1.6.