Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: creating a devworkspace telemetry plug-in #2231

Merged
merged 9 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM registry.access.redhat.com/ubi8/openjdk-11:1.11

ENV LANG='en_US.UTF-8' LANGUAGE='en_US:en'

COPY --chown=185 target/quarkus-app/lib/ /deployments/lib/
COPY --chown=185 target/quarkus-app/*.jar /deployments/
COPY --chown=185 target/quarkus-app/app/ /deployments/app/
COPY --chown=185 target/quarkus-app/quarkus/ /deployments/quarkus/

EXPOSE 8080
USER 185

ENTRYPOINT ["java", "-Dquarkus.http.host=0.0.0.0", "-Djava.util.logging.manager=org.jboss.logmanager.LogManager", "-Dquarkus.http.port=${DEVWORKSPACE_TELEMETRY_BACKEND_PORT}", "-jar", "/deployments/quarkus-run.jar"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM registry.access.redhat.com/ubi8/ubi-minimal:8.5
WORKDIR /work/
RUN chown 1001 /work \
&& chmod "g+rwX" /work \
&& chown 1001:root /work
COPY --chown=1001:root target/*-runner /work/application

EXPOSE 8080
USER 1001

CMD ["./application", "-Dquarkus.http.host=0.0.0.0", "-Dquarkus.http.port=$DEVWORKSPACE_TELEMETRY_BACKEND_PORT}"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.my.group;

import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

@RegisterRestClient
public interface TelemetryService {
@POST
@Path("/event") <1>
@Consumes(MediaType.APPLICATION_JSON)
Response sendEvent(Map<String, Object> payload);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@Override
public void destroy() {
onEvent(WORKSPACE_STOPPED, lastOwnerId, lastIp, lastUserAgent, lastResolution, commonProperties);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@Override
public void increaseDuration(AnalyticsEvent event, Map<String, Object> properties) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@Override
public boolean isEnabled() {
return true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main

import (
"io/ioutil"
"net/http"

"go.uber.org/zap"
)

var logger *zap.SugaredLogger

func event(w http.ResponseWriter, req *http.Request) {
switch req.Method {
case "GET":
logger.Info("GET /event")
case "POST":
logger.Info("POST /event")
}
body, err := req.GetBody()
if err != nil {
logger.With("err", err).Info("error getting body")
return
}
responseBody, err := ioutil.ReadAll(body)
if err != nil {
logger.With("error", err).Info("error reading response body")
return
}
logger.With("body", string(responseBody)).Info("got event")
}

func activity(w http.ResponseWriter, req *http.Request) {
switch req.Method {
case "GET":
logger.Info("GET /activity, doing nothing")
case "POST":
logger.Info("POST /activity")
body, err := req.GetBody()
if err != nil {
logger.With("error", err).Info("error getting body")
return
}
responseBody, err := ioutil.ReadAll(body)
if err != nil {
logger.With("error", err).Info("error reading response body")
return
}
logger.With("body", string(responseBody)).Info("got activity")
}
}

func main() {

log, _ := zap.NewProduction()
logger = log.Sugar()

http.HandleFunc("/event", event)
http.HandleFunc("/activity", activity)
logger.Info("Added Handlers")

logger.Info("Starting to serve")
http.ListenAndServe(":8080", nil)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class AnalyticsManager extends AbstractAnalyticsManager {

...

private long inactiveTimeLimit = 60000 * 3;

...

@Override
public void onActivity() {
if (System.currentTimeMillis() - lastEventTime >= inactiveTimeLimit) {
onEvent(WORKSPACE_INACTIVE, lastOwnerId, lastIp, lastUserAgent, lastResolution, commonProperties);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@Dependent
@Alternative
public class AnalyticsManager extends AbstractAnalyticsManager {
@Inject
@RestClient
TelemetryService telemetryService;

...

@Override
public void onEvent(AnalyticsEvent event, String ownerId, String ip, String userAgent, String resolution, Map<String, Object> properties) {
Map<String, Object> payload = new HashMap<String, Object>(properties);
payload.put("event", event);
telemetryService.sendEvent(payload);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
schemaVersion: 2.1.0
metadata:
name: devworkspace-telemetry-backend-plugin
version: 0.0.1
description: A Demo telemetry backend
displayName: Devworkspace Telemetry Backend
components:
- name: devworkspace-telemetry-backend-plugin
attributes:
workspaceEnv:
- name: DEVWORKSPACE_TELEMETRY_BACKEND_PORT
value: '4167'
container:
image: YOUR IMAGE <1>
env:
- name: WELCOME_MESSAGE <2>
value: 'hello world!'
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!-- 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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
kind: Deployment
apiVersion: apps/v1
metadata:
name: apache
spec:
replicas: 1
selector:
matchLabels:
app: apache
template:
metadata:
labels:
app: apache
spec:
volumes:
- name: plugin-yaml
configMap:
name: telemetry-plugin-yaml
defaultMode: 420
containers:
- name: apache
image: 'registry.redhat.io/rhscl/httpd-24-rhel7:latest'
ports:
- containerPort: 8080
protocol: TCP
resources: {}
volumeMounts:
- name: plugin-yaml
mountPath: /var/www/html
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 25%
maxSurge: 25%
revisionHistoryLimit: 10
progressDeadlineSeconds: 600
---
kind: Service
apiVersion: v1
metadata:
name: apache
spec:
ports:
- protocol: TCP
port: 8080
targetPort: 8080
selector:
app: apache
type: ClusterIP
---
kind: Route
apiVersion: route.openshift.io/v1
metadata:
name: apache
spec:
host: apache-che.apps-crc.testing
to:
kind: Service
name: apache
weight: 100
port:
targetPort: 8080
wildcardPolicy: None
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions modules/extensions/nav.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
** xref:connecting-source-code-from-github-to-a-openshift-component-using-openshift-connector.adoc[]
* xref:telemetry.adoc[]
** xref:creating-a-telemetry-plugin.adoc[]
** xref:creating-a-telemetry-plugin-for-devworkspaces.adoc[]
** xref:the-woopra-telemetry-plugin.adoc[]
* xref:java-lombok.adoc[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[id="creating-a-telemetry-plug-in-for-devworkspaces"]
// = Creating a telemetry plug-in for DevWorkspaces
:navtitle: Creating A Telemetry Plug-in For DevWorkspaces
dkwon17 marked this conversation as resolved.
Show resolved Hide resolved
:keywords: extensions, telemetry
:page-aliases: .:creating-a-telemetry-plug-in-for-devworkspaces

include::partial$proc_creating-a-telemetry-plugin-for-devworkspaces.adoc[]
Loading