-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
152 additions
and
1 deletion.
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
11 changes: 11 additions & 0 deletions
11
flint-core/src/main/java/org/opensearch/flint/core/metrics/aop/MetricConstants.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,11 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.flint.core.metrics.aop; | ||
|
||
public class MetricConstants { | ||
public static final String OS_READ_METRIC = "opensearch.read"; | ||
public static final String OS_WRITE_METRIC = "opensearch.write"; | ||
} |
55 changes: 55 additions & 0 deletions
55
flint-core/src/main/java/org/opensearch/flint/core/metrics/aop/MetricsAspect.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,55 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.flint.core.metrics.aop; | ||
|
||
import org.aspectj.lang.annotation.AfterReturning; | ||
import org.aspectj.lang.annotation.AfterThrowing; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.annotation.Pointcut; | ||
import org.opensearch.OpenSearchException; | ||
|
||
@Aspect | ||
public class MetricsAspect { | ||
@Pointcut("@annotation(publishMetricsAnnotation)") | ||
public void annotatedWithPublishMetrics(PublishMetrics publishMetricsAnnotation) {} | ||
|
||
@AfterReturning(pointcut = "annotatedWithPublishMetrics(publishMetricsAnnotation)", argNames = "publishMetricsAnnotation") | ||
public String logSuccess(PublishMetrics publishMetricsAnnotation) { | ||
int statusCode = 200; // Assume success with a status code of 200 | ||
String metricName = publishMetricsAnnotation.metricName(); | ||
return publishStatusMetrics(metricName, statusCode); | ||
} | ||
|
||
@AfterThrowing(pointcut = "annotatedWithPublishMetrics(publishMetricsAnnotation)", throwing = "ex", argNames = "ex,publishMetricsAnnotation") | ||
public String logException(Throwable ex, PublishMetrics publishMetricsAnnotation) { | ||
if (ex instanceof OpenSearchException) { | ||
OpenSearchException openSearchException = (OpenSearchException) ex; | ||
int statusCode = openSearchException.status().getStatus(); | ||
String metricName = publishMetricsAnnotation.metricName(); | ||
return publishStatusMetrics(metricName, statusCode); | ||
} | ||
return null; | ||
} | ||
|
||
private String publishStatusMetrics(String metricName, int statusCode) { | ||
String metricSuffix = null; | ||
|
||
if (statusCode == 200) { | ||
metricSuffix = "2xx.count"; | ||
} else if (statusCode == 403) { | ||
metricSuffix = "403.count"; | ||
} else if (statusCode >= 500) { | ||
metricSuffix = "5xx.count"; | ||
} else if (statusCode >= 400) { | ||
metricSuffix = "4xx.count"; | ||
} | ||
|
||
String fullMetricName = metricName + "." + metricSuffix; | ||
|
||
// TODO: Add metrics to the source | ||
return fullMetricName; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
flint-core/src/main/java/org/opensearch/flint/core/metrics/aop/PublishMetrics.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,12 @@ | ||
package org.opensearch.flint.core.metrics.aop; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.METHOD, ElementType.TYPE}) | ||
public @interface PublishMetrics { | ||
String metricName(); | ||
} |
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
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
49 changes: 49 additions & 0 deletions
49
flint-core/src/test/java/org/opensearch/flint/core/metrics/aop/MetricsAspectTest.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,49 @@ | ||
package org.opensearch.flint.core.metrics.aop; | ||
|
||
import org.aspectj.lang.JoinPoint; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.MockitoAnnotations; | ||
import org.opensearch.OpenSearchException; | ||
import org.opensearch.rest.RestStatus; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.Mockito.*; | ||
|
||
public class MetricsAspectTest { | ||
|
||
@InjectMocks | ||
private MetricsAspect metricsAspect; | ||
|
||
@Mock | ||
private JoinPoint joinPoint; | ||
|
||
@Before | ||
public void setUp() { | ||
MockitoAnnotations.initMocks(this); | ||
} | ||
|
||
@Test | ||
public void testLogSuccess() { | ||
PublishMetrics publishMetricsAnnotation = mock(PublishMetrics.class); | ||
when(publishMetricsAnnotation.metricName()).thenReturn("testMetric"); | ||
|
||
assert ("testMetric.2xx.count".equals(metricsAspect.logSuccess(publishMetricsAnnotation))); | ||
} | ||
|
||
@Test | ||
public void testLogExceptionWithOpenSearchException() { | ||
PublishMetrics publishMetricsAnnotation = mock(PublishMetrics.class); | ||
when(publishMetricsAnnotation.metricName()).thenReturn("testMetric"); | ||
|
||
OpenSearchException exception = Mockito.mock(OpenSearchException.class); | ||
Mockito.when(exception.getMessage()).thenReturn("Error"); | ||
Mockito.when(exception.getCause()).thenReturn(new RuntimeException()); | ||
Mockito.when(exception.status()).thenReturn(RestStatus.INTERNAL_SERVER_ERROR); | ||
|
||
assertEquals("testMetric.5xx.count", metricsAspect.logException(exception, publishMetricsAnnotation)); | ||
} | ||
} |
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