Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into monitoring-6
Browse files Browse the repository at this point in the history
  • Loading branch information
bshien committed Jun 10, 2024
2 parents fa3cfcc + c0dbd62 commit c597361
Show file tree
Hide file tree
Showing 15 changed files with 120 additions and 48 deletions.
1 change: 1 addition & 0 deletions DEVELOPER_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ So you want to contribute code to this project? Excellent! We're glad you're her
- `cdk deploy OpenSearchMetrics-HostedZone`: To deploy the route53 and DNS setup.
- `cdk deploy OpenSearchMetricsNginxReadonly`: To deploy the dashboard read only setup.
- `cdk deploy OpenSearchWAF`: To deploy the AWS WAF for the project ALB's.
- `cdk deploy OpenSearchMetrics-Monitoring`: To deploy the alerting stack which will monitor the step functions and URL of the project coming from [METRICS_HOSTED_ZONE](https://github.com/opensearch-project/opensearch-metrics/blob/main/infrastructure/lib/enums/project.ts)

### Forking and Cloning

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ dependencies {
implementation 'io.github.acm19:aws-request-signing-apache-interceptor:2.3.1'

implementation 'com.amazonaws:aws-lambda-java-core:1.2.3'
implementation 'com.amazonaws:aws-lambda-java-events:3.7.0'
implementation 'com.amazonaws:aws-lambda-java-events:3.11.5'

implementation 'com.google.code.gson:gson:2.10.1'

Expand Down
5 changes: 3 additions & 2 deletions infrastructure/lib/constructs/snsMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { Construct } from 'constructs';
import * as sns from "aws-cdk-lib/aws-sns";
import * as subscriptions from "aws-cdk-lib/aws-sns-subscriptions";
import * as actions from "aws-cdk-lib/aws-cloudwatch-actions";
import { Canary } from 'aws-cdk-lib/aws-synthetics';
import {OpenSearchLambda} from "./lambda";
import Project from '../enums/project';


export interface SnsMonitorsProps {
readonly region: string;
Expand Down Expand Up @@ -33,7 +34,7 @@ export class SnsMonitors extends Construct {

// The email list for receiving alerts
this.emailList = [
'[email protected]'
Project.SNS_ALERT_EMAIL
];

// Create alarms
Expand Down
1 change: 1 addition & 0 deletions infrastructure/lib/enums/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ enum Project{
RESTRICTED_PREFIX = '',
LAMBDA_PACKAGE = 'opensearch-metrics-1.0.zip',
EC2_AMI_SSM = '',
SNS_ALERT_EMAIL = '[email protected]'
}
export default Project;
6 changes: 4 additions & 2 deletions infrastructure/lib/infrastructure-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ export class InfrastructureStack extends Stack {

// Create Secrets Manager

const openSearchMetricsSecretsStack = new OpenSearchMetricsSecrets(app, "OpenSearchMetrics-Secrets");
const openSearchMetricsSecretsStack = new OpenSearchMetricsSecrets(app, "OpenSearchMetrics-Secrets", {
secretName: 'metrics-creds'
});

// Create Monitoring Dashboard

Expand All @@ -52,7 +54,7 @@ export class InfrastructureStack extends Stack {
account: Project.AWS_ACCOUNT,
workflowComponent: openSearchMetricsWorkflowStack.workflowComponent,
lambdaPackage: Project.LAMBDA_PACKAGE,
secrets: openSearchMetricsSecretsStack.secretsObject,
secrets: openSearchMetricsSecretsStack.secret,
vpcStack: vpcStack
});

Expand Down
12 changes: 8 additions & 4 deletions infrastructure/lib/stacks/secrets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import {Stack} from "aws-cdk-lib";
import { Construct } from 'constructs';
import {Secret} from "aws-cdk-lib/aws-secretsmanager";

export interface SecretProps {
readonly secretName: string
}

export class OpenSearchMetricsSecrets extends Stack {
readonly secretsObject: Secret;
readonly secret: Secret;

constructor(scope: Construct, id: string) {
constructor(scope: Construct, id: string, props: SecretProps ) {
super(scope, id);
this.secretsObject = new Secret(this, 'MetricsCreds', {
secretName: 'metrics-creds',
this.secret = new Secret(this, `MetricsCreds-${props.secretName}`, {
secretName: props.secretName,
});
}
}
1 change: 1 addition & 0 deletions infrastructure/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions infrastructure/test/monitoring-stack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ test('Monitoring Stack Test', () => {
vpcStack: vpcStack,
lambdaPackage: Project.LAMBDA_PACKAGE
});
const openSearchMetricsSecretsStack = new OpenSearchMetricsSecrets(app, "OpenSearchMetrics-Secrets");
const openSearchMetricsSecretsStack = new OpenSearchMetricsSecrets(app, "OpenSearchMetrics-Secrets", {
secretName: 'metrics-creds'
});
const openSearchMetricsMonitoringStack = new OpenSearchMetricsMonitoringStack(app, "OpenSearchMetrics-Monitoring", {
region: Project.REGION,
account: Project.AWS_ACCOUNT,
workflowComponent: openSearchMetricsWorkflowStack.workflowComponent,
lambdaPackage: Project.LAMBDA_PACKAGE,
secrets: openSearchMetricsSecretsStack.secretsObject,
secrets: openSearchMetricsSecretsStack.secret,
vpcStack: vpcStack
});
const template = Template.fromStack(openSearchMetricsMonitoringStack);
Expand Down Expand Up @@ -63,10 +65,7 @@ test('Monitoring Stack Test', () => {
"Statement": [
{
"Action": "secretsmanager:GetSecretValue",
"Effect": "Allow",
"Resource": {
"Fn::ImportValue": "OpenSearchMetrics-Secrets:ExportsOutputRefMetricsCreds2260E61E4655F9C2"
}
"Effect": "Allow"
},
{
"Action": [
Expand Down
4 changes: 3 additions & 1 deletion infrastructure/test/secrets-stack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {OpenSearchMetricsSecrets} from "../lib/stacks/secrets";

test('Secrets Stack Test', () => {
const app = new App();
const openSearchMetricsSecretsStack = new OpenSearchMetricsSecrets(app, "OpenSearchMetrics-Secrets");
const openSearchMetricsSecretsStack = new OpenSearchMetricsSecrets(app, "OpenSearchMetrics-Secrets", {
secretName: 'metrics-creds'
});
const template = Template.fromStack(openSearchMetricsSecretsStack);
template.resourceCountIs('AWS::SecretsManager::Secret', 1);
template.hasResourceProperties('AWS::SecretsManager::Secret', {
Expand Down
25 changes: 3 additions & 22 deletions src/main/java/org/opensearchmetrics/lambda/SlackLambda.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package org.opensearchmetrics.lambda;

import org.opensearchmetrics.model.alarm.AlarmData;
import org.opensearchmetrics.util.SecretsManagerUtil;
import org.opensearchmetrics.datasource.DataSourceType;
import com.google.common.annotations.VisibleForTesting;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import lombok.Data;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.SNSEvent;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.http.HttpResponse;
Expand Down Expand Up @@ -40,22 +37,6 @@ public SlackLambda() {
this.mapper = COMPONENT.getObjectMapper();
}

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@Data
private static class AlarmObject {
@JsonProperty("AlarmName")
private String alarmName;
@JsonProperty("AlarmDescription")
private String alarmDescription;
@JsonProperty("StateChangeTime")
private String stateChangeTime;
@JsonProperty("Region")
private String region;
@JsonProperty("AlarmArn")
private String alarmArn;
}

@Override
public Void handleRequest(SNSEvent event, Context context) {
String slackWebhookURL;
Expand All @@ -80,8 +61,8 @@ public Void handleRequest(SNSEvent event, Context context) {
}

private void sendMessageToSlack(String message, String slackWebhookURL, String slackChannel, String slackUsername) throws IOException {
AlarmObject alarmObject =
mapper.readValue(message, AlarmObject.class);
AlarmData alarmObject =
mapper.readValue(message, AlarmData.class);
String alarmMessage = ":alert: OpenSearch Metrics Dashboard Monitoring alarm activated. Please investigate the issue. \n" +
"- Name: " + alarmObject.getAlarmName() + "\n" +
"- Description: " + alarmObject.getAlarmDescription() + "\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ public enum ReleaseInputs {
VERSION_3_0_0("3.0.0", "open", "main"),
VERSION_2_12_0("2.12.0", "closed", "2.12"),
VERSION_2_13_0("2.13.0", "closed", "2.13"),

VERSION_2_14_0("2.14.0", "open", "2.x"),
VERSION_1_3_15("1.3.15", "closed", "1.3");
VERSION_2_14_0("2.14.0", "closed", "2.14"),
VERSION_2_15_0("2.15.0", "open", "2.x"),
VERSION_1_3_15("1.3.15", "closed", "1.3"),
VERSION_1_3_16("1.3.16", "closed", "1.3"),
VERSION_1_3_17("1.3.17", "open", "1.3");

private final String version;
private final String state;
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/org/opensearchmetrics/model/alarm/AlarmData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.opensearchmetrics.model.alarm;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

@Data
public class AlarmData {
@JsonProperty("AlarmName")
private String alarmName;
@JsonProperty("AlarmDescription")
private String alarmDescription;
@JsonProperty("StateChangeTime")
private String stateChangeTime;
@JsonProperty("Region")
private String region;
@JsonProperty("AlarmArn")
private String alarmArn;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Optional;

@Slf4j
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,46 @@ public void testGetVersion() {
assertEquals("2.12.0", ReleaseInputs.VERSION_2_12_0.getVersion());
assertEquals("2.13.0", ReleaseInputs.VERSION_2_13_0.getVersion());
assertEquals("2.14.0", ReleaseInputs.VERSION_2_14_0.getVersion());
assertEquals("2.15.0", ReleaseInputs.VERSION_2_15_0.getVersion());
assertEquals("1.3.15", ReleaseInputs.VERSION_1_3_15.getVersion());
assertEquals("1.3.16", ReleaseInputs.VERSION_1_3_16.getVersion());
assertEquals("1.3.17", ReleaseInputs.VERSION_1_3_17.getVersion());
}

@Test
public void testGetState() {
assertEquals("open", ReleaseInputs.VERSION_3_0_0.getState());
assertEquals("closed", ReleaseInputs.VERSION_2_12_0.getState());
assertEquals("open", ReleaseInputs.VERSION_2_14_0.getState());
assertEquals("closed", ReleaseInputs.VERSION_2_13_0.getState());
assertEquals("closed", ReleaseInputs.VERSION_2_14_0.getState());
assertEquals("open", ReleaseInputs.VERSION_2_15_0.getState());
assertEquals("closed", ReleaseInputs.VERSION_1_3_15.getState());
assertEquals("closed", ReleaseInputs.VERSION_1_3_16.getState());
assertEquals("open", ReleaseInputs.VERSION_1_3_17.getState());
}

@Test
public void testGetBranch() {
assertEquals("main", ReleaseInputs.VERSION_3_0_0.getBranch());
assertEquals("2.12", ReleaseInputs.VERSION_2_12_0.getBranch());
assertEquals("2.13", ReleaseInputs.VERSION_2_13_0.getBranch());
assertEquals("2.x", ReleaseInputs.VERSION_2_14_0.getBranch());
assertEquals("2.14", ReleaseInputs.VERSION_2_14_0.getBranch());
assertEquals("2.x", ReleaseInputs.VERSION_2_15_0.getBranch());
assertEquals("1.3", ReleaseInputs.VERSION_1_3_15.getBranch());
assertEquals("1.3", ReleaseInputs.VERSION_1_3_16.getBranch());
}

@Test
public void testGetAllReleaseInputs() {
ReleaseInputs[] releaseInputs = ReleaseInputs.getAllReleaseInputs();
assertEquals(5, releaseInputs.length);
assertEquals(8, releaseInputs.length);
assertEquals(ReleaseInputs.VERSION_3_0_0, releaseInputs[0]);
assertEquals(ReleaseInputs.VERSION_2_12_0, releaseInputs[1]);
assertEquals(ReleaseInputs.VERSION_2_13_0, releaseInputs[2]);
assertEquals(ReleaseInputs.VERSION_2_14_0, releaseInputs[3]);
assertEquals(ReleaseInputs.VERSION_1_3_15, releaseInputs[4]);
assertEquals(ReleaseInputs.VERSION_2_15_0, releaseInputs[4]);
assertEquals(ReleaseInputs.VERSION_1_3_15, releaseInputs[5]);
assertEquals(ReleaseInputs.VERSION_1_3_16, releaseInputs[6]);
assertEquals(ReleaseInputs.VERSION_1_3_17, releaseInputs[7]);
}
}
52 changes: 52 additions & 0 deletions src/test/java/org/opensearchmetrics/model/alarm/AlarmDataTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.opensearchmetrics.model.alarm;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class AlarmDataTest {
@Mock
ObjectMapper objectMapper;

private AlarmData alarmData;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
alarmData = new AlarmData();
}

@Test
public void testAlarmName() {
alarmData.setAlarmName("testName");
assertEquals("testName", alarmData.getAlarmName());
}

@Test
public void testAlarmDescription() {
alarmData.setAlarmDescription("testDescription");
assertEquals("testDescription", alarmData.getAlarmDescription());
}

@Test
public void testStateChangeTime() {
alarmData.setStateChangeTime("testStateChangeTime");
assertEquals("testStateChangeTime", alarmData.getStateChangeTime());
}

@Test
public void testRegion() {
alarmData.setRegion("testRegion");
assertEquals("testRegion", alarmData.getRegion());
}

@Test
public void testAlarmArn() {
alarmData.setAlarmArn("testArn");
assertEquals("testArn", alarmData.getAlarmArn());
}
}

0 comments on commit c597361

Please sign in to comment.