This repository has been archived by the owner on Feb 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add markers notification sender (#470)
Add marker notification type which sends the STARTED deployment as a logzio marker to our public markers api (there is no smarted mechanism for disabling some of the notifiers, so let's discuss it or add one in the future)
- Loading branch information
Showing
7 changed files
with
281 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
apollo-backend/src/main/java/io/logz/apollo/notifications/senders/MarkersSender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package io.logz.apollo.notifications.senders; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.google.common.collect.ImmutableList; | ||
import io.logz.apollo.common.HttpStatus; | ||
import io.logz.apollo.models.DeploymentMarker; | ||
import io.logz.apollo.models.DeploymentMarkers; | ||
import io.logz.apollo.notifications.NotificationTemplateMetadata; | ||
import okhttp3.MediaType; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.RequestBody; | ||
import org.mariadb.jdbc.internal.logging.Logger; | ||
import org.mariadb.jdbc.internal.logging.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
|
||
public class MarkersSender implements NotificationSender { | ||
private static final Logger logger = LoggerFactory.getLogger(MarkersSender.class); | ||
private final String X_API_TOKEN_HEADER = "X-API-TOKEN"; | ||
private final String apiToken; | ||
private final String markersEndpointUrl; | ||
private final ObjectMapper objectMapper; | ||
private final OkHttpClient httpClient; | ||
|
||
public MarkersSender(String notificationJsonConfiguration) throws IOException { | ||
objectMapper = new ObjectMapper(); | ||
httpClient = new OkHttpClient(); | ||
DeploymentMarkersConfiguration markersConfiguration = objectMapper.readValue(notificationJsonConfiguration, DeploymentMarkersConfiguration.class); | ||
this.apiToken = markersConfiguration.getApiToken(); | ||
this.markersEndpointUrl = markersConfiguration.getMarkersEndpointUrl(); | ||
} | ||
|
||
@Override | ||
public boolean send(NotificationTemplateMetadata notificationTemplateMetadata) { | ||
Request.Builder builder = new Request.Builder().url(this.markersEndpointUrl) | ||
.header(X_API_TOKEN_HEADER, this.apiToken); | ||
|
||
DeploymentMarkers deploymentMarkers = getDeploymentMarkers(notificationTemplateMetadata); | ||
return executeMarkersRequest(httpClient, builder, deploymentMarkers); | ||
} | ||
|
||
private boolean executeMarkersRequest(OkHttpClient httpClient, Request.Builder builder, DeploymentMarkers deploymentMarkers) { | ||
try { | ||
String markersJson = objectMapper.writeValueAsString(deploymentMarkers); | ||
|
||
Request request = builder.post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), markersJson)).build(); | ||
try (okhttp3.Response response = httpClient.newCall(request).execute()) { | ||
if (HttpStatus.isPositive(response.code())) { | ||
logger.info("successfully sent marker"); | ||
return true; | ||
} else { | ||
logger.warn("exception while sending marker"); | ||
} | ||
} catch (IOException e) { | ||
logger.warn("IO Exception when trying to send deployment markers request", e); | ||
} | ||
} catch (JsonProcessingException jsonProcessingException) { | ||
logger.warn("Could not process json", jsonProcessingException); | ||
} | ||
return false; | ||
} | ||
|
||
private DeploymentMarkers getDeploymentMarkers(NotificationTemplateMetadata deployment) { | ||
DeploymentMarker deploymentMarker = new DeploymentMarker(); | ||
deploymentMarker.setTitle(deployment.getServiceName()); | ||
deploymentMarker.setDescription(String.format("Deployment to service: %s, with commit message: %s", deployment.getServiceName(), deployment.getDeploymentMessage())); | ||
|
||
HashMap<String, String> metadata = new HashMap<>(); | ||
metadata.put("deployment_message", String.valueOf(deployment.getDeploymentMessage())); | ||
metadata.put("deployment_id", String.valueOf(deployment.getDeploymentId())); | ||
metadata.put("deployment_status", String.valueOf(deployment.getStatus())); | ||
metadata.put("group_name", String.valueOf(deployment.getGroupName())); | ||
metadata.put("service_name", String.valueOf(deployment.getServiceName())); | ||
metadata.put("environment_name", String.valueOf(deployment.getEnvironmentName())); | ||
deploymentMarker.setMetadata(metadata); | ||
|
||
DeploymentMarkers deploymentMarkers = new DeploymentMarkers(); | ||
deploymentMarkers.setMarkers(ImmutableList.of(deploymentMarker)); | ||
return deploymentMarkers; | ||
} | ||
|
||
public static class DeploymentMarkersConfiguration { | ||
private String apiToken; | ||
private String markersEndpointUrl; | ||
|
||
public DeploymentMarkersConfiguration() { | ||
} | ||
|
||
public String getApiToken() { | ||
return apiToken; | ||
} | ||
|
||
public String getMarkersEndpointUrl() { | ||
return markersEndpointUrl; | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
apollo-model/src/main/java/io/logz/apollo/models/DeploymentMarker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package io.logz.apollo.models; | ||
|
||
import java.util.Map; | ||
|
||
public class DeploymentMarker { | ||
private String description; | ||
private String title; | ||
private Map<String, String> metadata; | ||
private String tag = "DEPLOYMENT"; | ||
|
||
public DeploymentMarker() { | ||
|
||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
public Map<String, String> getMetadata() { | ||
return metadata; | ||
} | ||
|
||
public void setMetadata(Map<String, String> metadata) { | ||
this.metadata = metadata; | ||
} | ||
|
||
public String getTag() { | ||
return tag; | ||
} | ||
|
||
public void setTag(String tag) { | ||
this.tag = tag; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
apollo-model/src/main/java/io/logz/apollo/models/DeploymentMarkers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package io.logz.apollo.models; | ||
|
||
import java.util.List; | ||
|
||
public class DeploymentMarkers { | ||
private List<DeploymentMarker> markers; | ||
|
||
public DeploymentMarkers() { | ||
} | ||
|
||
public List<DeploymentMarker> getMarkers() { | ||
return markers; | ||
} | ||
|
||
public void setMarkers(List<DeploymentMarker> markers) { | ||
this.markers = markers; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters