Create a telemetry backend

Create a Quarkus-based telemetry backend that extends the Che telemetry client and implements custom event handling logic.

For fast feedback when developing, develop inside a DevWorkspace. This way, you can run the application in a cluster and receive events from the front-end telemetry plugin.

Prerequisites
  • You have a running instance of Eclipse Che.

  • You have a telemetry server deployed to receive events. See Create a telemetry server.

Procedure
  1. Create a Maven Quarkus project:

    mvn io.quarkus:quarkus-maven-plugin:2.7.1.Final:create \
        -DprojectGroupId=mygroup -DprojectArtifactId=devworkspace-telemetry-example-plugin \
    -DprojectVersion=1.0.0-SNAPSHOT
  2. Remove the files under src/main/java/mygroup and src/test/java/mygroup.

  3. Consult the GitHub packages for the latest version of backend-base and add the following dependencies to your pom.xml:

    Example 1. pom.xml
    <!-- Required -->
    <dependency>
        <groupId>org.eclipse.che.incubator.workspace-telemetry</groupId>
        <artifactId>backend-base</artifactId>
        <version>LATEST VERSION FROM PREVIOUS STEP</version>
    </dependency>
    
    
    <!-- Used to make http requests to the telemetry server -->
    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-rest-client</artifactId>
    </dependency>
    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-rest-client-jackson</artifactId>
    </dependency>
  4. Create a personal access token with read:packages permissions from GitHub packages and add your GitHub username, the token, and che-incubator repository details in your ~/.m2/settings.xml file:

    Example 2. settings.xml
    <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
    http://maven.apache.org/xsd/settings-1.0.0.xsd">
       <servers>
          <server>
             <id>che-incubator</id>
             <username>YOUR GITHUB USERNAME</username>
             <password>YOUR GITHUB TOKEN</password>
          </server>
       </servers>
    
       <profiles>
          <profile>
             <id>github</id>
             <activation>
                <activeByDefault>true</activeByDefault>
             </activation>
             <repositories>
                <repository>
                   <id>central</id>
                   <url>https://repo1.maven.org/maven2</url>
                   <releases><enabled>true</enabled></releases>
                   <snapshots><enabled>false</enabled></snapshots>
                   </repository>
                   <repository>
                   <id>che-incubator</id>
                   <url>https://maven.pkg.github.com/che-incubator/che-workspace-telemetry-client</url>
                </repository>
             </repositories>
          </profile>
       </profiles>
    </settings>
  5. Create MainConfiguration.java under src/main/java/mygroup. This file contains configuration provided to AnalyticsManager:

    Example 3. MainConfiguration.java
    package org.my.group;
    
    import java.util.Optional;
    
    import javax.enterprise.context.Dependent;
    import javax.enterprise.inject.Alternative;
    
    import org.eclipse.che.incubator.workspace.telemetry.base.BaseConfiguration;
    import org.eclipse.microprofile.config.inject.ConfigProperty;
    
    @Dependent
    @Alternative
    public class MainConfiguration extends BaseConfiguration {
        @ConfigProperty(name = "welcome.message")      (1)
        Optional<String> welcomeMessage;               (2)
    }
    1 A MicroProfile configuration annotation is used to inject the welcome.message configuration.

    For more details on how to set configuration properties specific to your backend, see the Quarkus Configuration Reference Guide.

  6. Create AnalyticsManager.java under src/main/java/mygroup. This file contains logic specific to the telemetry system:

    Example 4. AnalyticsManager.java
    package org.my.group;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import javax.enterprise.context.Dependent;
    import javax.enterprise.inject.Alternative;
    import javax.inject.Inject;
    
    import org.eclipse.che.incubator.workspace.telemetry.base.AbstractAnalyticsManager;
    import org.eclipse.che.incubator.workspace.telemetry.base.AnalyticsEvent;
    import org.eclipse.che.incubator.workspace.telemetry.finder.DevWorkspaceFinder;
    import org.eclipse.che.incubator.workspace.telemetry.finder.UsernameFinder;
    import org.eclipse.microprofile.rest.client.inject.RestClient;
    import org.slf4j.Logger;
    
    import static org.slf4j.LoggerFactory.getLogger;
    
    @Dependent
    @Alternative
    public class AnalyticsManager extends AbstractAnalyticsManager {
    
        private static final Logger LOG = getLogger(AbstractAnalyticsManager.class);
    
        public AnalyticsManager(MainConfiguration mainConfiguration, DevWorkspaceFinder devworkspaceFinder, UsernameFinder usernameFinder) {
            super(mainConfiguration, devworkspaceFinder, usernameFinder);
    
            mainConfiguration.welcomeMessage.ifPresentOrElse(     (1)
                (str) -> LOG.info("The welcome message is: {}", str),
                () -> LOG.info("No welcome message provided")
            );
        }
    
        @Override
        public boolean isEnabled() {
            return true;
        }
    
        @Override
        public void destroy() {}
    
        @Override
        public void onEvent(AnalyticsEvent event, String ownerId, String ip, String userAgent, String resolution, Map<String, Object> properties) {
            LOG.info("The received event is: {}", event);         (2)
        }
    
        @Override
        public void increaseDuration(AnalyticsEvent event, Map<String, Object> properties) { }
    
        @Override
        public void onActivity() {}
    }
    1 Log the welcome message if it was provided.
    2 Log the event received from the front-end plugin.
  7. Add the quarkus.arc.selected-alternatives property to src/main/resources/application.properties to specify the alternative beans:

    Example 5. application.properties
    quarkus.arc.selected-alternatives=MainConfiguration,AnalyticsManager
Verification
  • Run the Quarkus application and verify that it starts without errors:

    mvn quarkus:dev