Skip to content

Commit

Permalink
Merge pull request #12 from camunda-community-hub/mixpanel-tracking
Browse files Browse the repository at this point in the history
feat: Add Mixpanel tracking
  • Loading branch information
saig0 authored Jul 20, 2023
2 parents 528f05c + d7fd8db commit d7d3a25
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 3 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ The API has the following endpoints:
- Type: `POST`
- Path: `/api/v1/feel/evaluate`

Request:
Request:

```json
{
Expand All @@ -41,7 +41,7 @@ Response:
- Type: `POST`
- Path: `/api/v1/feel-unary-tests/evaluate`

Request:
Request:

```json
{
Expand Down Expand Up @@ -89,6 +89,12 @@ kubectl create namespace feel
kubectl apply -f service.yaml -n feel
```

To deploy the secrets, edit `secret.yaml` and set `mixpanel_project_token` to your Mixpanel project token. Then run the following:

```shell
kubectl apply -f secret.yaml -n feel
```

To deploy the rest api, edit `deployment.yaml` and set `spec.template.spec.containers[0].img` to point
to the latest docker image. Then run the following:

Expand Down
6 changes: 6 additions & 0 deletions k8s/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ spec:
port: 8080
initialDelaySeconds: 10
timeoutSeconds: 5
env:
- name: MIXPANEL_PROJECT_TOKEN
valueFrom:
secretKeyRef:
name: feel-scala-playground-secret
key: mixpanel_project_token
7 changes: 7 additions & 0 deletions k8s/secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: Secret
metadata:
name: feel-scala-playground-secret
type: Opaque
stringData:
mixpanel_project_token: x
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
<version>${feel.version}</version>
</dependency>

<dependency>
<groupId>com.mixpanel</groupId>
<artifactId>mixpanel-java</artifactId>
<version>1.5.2</version>
</dependency>

</dependencies>
</dependencyManagement>

Expand All @@ -59,6 +65,11 @@
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
<groupId>com.mixpanel</groupId>
<artifactId>mixpanel-java</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.camunda.feel.playground.dto.FeelEvaluationResponse;
import org.camunda.feel.playground.dto.FeelUnaryTestsEvaluationRequest;
import org.camunda.feel.playground.sevice.FeelEvaluationService;
import org.camunda.feel.playground.sevice.TrackingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
Expand All @@ -22,9 +23,11 @@ public class FeelEvaluationController {
private static final Logger LOG = LoggerFactory.getLogger(FeelEvaluationController.class);

private final FeelEvaluationService evaluationService;
private final TrackingService trackingService;

public FeelEvaluationController(final FeelEvaluationService evaluationService) {
public FeelEvaluationController(final FeelEvaluationService evaluationService, TrackingService trackingService) {
this.evaluationService = evaluationService;
this.trackingService = trackingService;
}

@PostMapping("/feel/evaluate")
Expand All @@ -40,6 +43,9 @@ public ResponseEntity<FeelEvaluationResponse> evaluate(

} catch (Exception e) {
return new ResponseEntity<>(FeelEvaluationResponse.withError(e.getMessage()), HttpStatus.OK);

} finally {
trackingService.trackExpressionEvaluation(request.metadata);
}
}

Expand All @@ -58,6 +64,9 @@ public ResponseEntity<FeelEvaluationResponse> evaluateUnaryTests(

} catch (Exception e) {
return new ResponseEntity<>(FeelEvaluationResponse.withError(e.getMessage()), HttpStatus.OK);

} finally {
trackingService.trackUnaryTestsExpressionEvaluation(request.metadata);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.camunda.feel.playground.sevice;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnProperty(prefix = "playground.tracking", name = "enabled", havingValue = "true")
public class MixpanelProperties {

@Value("${MIXPANEL_PROJECT_TOKEN}")
private String projectToken;

public String getProjectToken() {
return projectToken;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.camunda.feel.playground.sevice;

import com.mixpanel.mixpanelapi.MessageBuilder;
import com.mixpanel.mixpanelapi.MixpanelAPI;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Optional;

@Component
@ConditionalOnBean(MixpanelProperties.class)
public class MixpanelTrackingService implements TrackingService {

private static final Logger LOGGER = LoggerFactory.getLogger(MixpanelTrackingService.class);

private final MixpanelAPI mixpanelAPI = new MixpanelAPI();

private final MessageBuilder messageBuilder;

public MixpanelTrackingService(MixpanelProperties mixpanelProperties) {
messageBuilder = new MessageBuilder(mixpanelProperties.getProjectToken());
}

private void sendMixpanelEvent(EVENT_TYPE eventType, Map<String, String> metadata) {

var properties = new JSONObject();
Optional.ofNullable(metadata).ifPresent(entry -> entry.forEach(properties::put));

var message = messageBuilder.event(null, eventType.name(), properties);

try {
mixpanelAPI.sendMessage(message);
} catch (Exception e) {
LOGGER.warn("Failed to send Mixpanel event.", e);
}
}

@Override
public void trackExpressionEvaluation(Map<String, String> metadata) {
sendMixpanelEvent(EVENT_TYPE.FEEL_EXPRESSION_EVALUATION, metadata);
}

@Override
public void trackUnaryTestsExpressionEvaluation(Map<String, String> metadata) {
sendMixpanelEvent(EVENT_TYPE.FEEL_UNARY_TESTS_EXPRESSION_EVALUATION, metadata);
}

private enum EVENT_TYPE {
FEEL_EXPRESSION_EVALUATION,
FEEL_UNARY_TESTS_EXPRESSION_EVALUATION
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.camunda.feel.playground.sevice;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@ConditionalOnMissingBean(MixpanelProperties.class)
public class NoopTrackingService implements TrackingService {

@Override
public void trackExpressionEvaluation(Map<String, String> metadata) {
// no-op
}

@Override
public void trackUnaryTestsExpressionEvaluation(Map<String, String> metadata) {
// no-op
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.camunda.feel.playground.sevice;

import java.util.Map;

public interface TrackingService {

void trackExpressionEvaluation(Map<String, String> metadata);

void trackUnaryTestsExpressionEvaluation(Map<String, String> metadata);

}
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
server.port=${PORT:8080}

playground.tracking.enabled=true
3 changes: 3 additions & 0 deletions src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
server.port=${PORT:8080}

playground.tracking.enabled=false

0 comments on commit d7d3a25

Please sign in to comment.