-
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.
Signed-off-by: Vamsi Manohar <[email protected]>
- Loading branch information
Showing
5 changed files
with
198 additions
and
38 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
flint-core/src/main/scala/org/apache/spark/metrics/source/FlintMetricSource.scala
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 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.apache.spark.metrics.source | ||
|
||
import com.codahale.metrics.MetricRegistry | ||
|
||
class FlintMetricSource(val sourceName: String) extends Source { | ||
override val metricRegistry: MetricRegistry = new MetricRegistry | ||
} |
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
147 changes: 147 additions & 0 deletions
147
...src/main/scala/org/opensearch/flint/core/metrics/FlintOpensearchClientMetricsWrapper.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,147 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.flint.core.metrics; | ||
|
||
import com.amazonaws.services.opensearch.model.AccessDeniedException; | ||
import com.codahale.metrics.Counter; | ||
import java.util.List; | ||
import java.util.function.Supplier; | ||
import org.apache.spark.SparkEnv; | ||
import org.apache.spark.metrics.source.FlintMetricSource; | ||
import org.opensearch.client.RestHighLevelClient; | ||
import org.opensearch.flint.core.FlintClient; | ||
import org.opensearch.flint.core.metadata.FlintMetadata; | ||
import org.opensearch.flint.core.metadata.log.OptimisticTransaction; | ||
import org.opensearch.flint.core.metrics.reporter.DimensionedName; | ||
import org.opensearch.flint.core.storage.FlintOpenSearchClient; | ||
import org.opensearch.flint.core.storage.FlintReader; | ||
import org.opensearch.flint.core.storage.FlintWriter; | ||
|
||
/** | ||
* This class wraps FlintOpensearchClient and emit spark metrics to FlintMetricSource. | ||
*/ | ||
public class FlintOpensearchClientMetricsWrapper implements FlintClient { | ||
|
||
private final FlintOpenSearchClient delegate; | ||
|
||
public FlintOpensearchClientMetricsWrapper(FlintOpenSearchClient delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public <T> OptimisticTransaction<T> startTransaction(String indexName, String dataSourceName) { | ||
return handleExceptions(() -> delegate.startTransaction(indexName, dataSourceName)); | ||
} | ||
|
||
@Override | ||
public <T> OptimisticTransaction<T> startTransaction(String indexName, String dataSourceName, | ||
boolean forceInit) { | ||
return handleExceptions(() -> delegate.startTransaction(indexName, dataSourceName, forceInit)); | ||
} | ||
|
||
@Override | ||
public void createIndex(String indexName, FlintMetadata metadata) { | ||
try { | ||
delegate.createIndex(indexName, metadata); | ||
} catch (AccessDeniedException exception){ | ||
handleAccessDeniedException(); | ||
throw exception; | ||
} catch (Throwable t) { | ||
handleThrowable(); | ||
throw t; | ||
} | ||
} | ||
|
||
@Override | ||
public boolean exists(String indexName) { | ||
return handleExceptions(() -> delegate.exists(indexName)); | ||
} | ||
|
||
@Override | ||
public List<FlintMetadata> getAllIndexMetadata(String indexNamePattern) { | ||
return handleExceptions(() -> delegate.getAllIndexMetadata(indexNamePattern)); | ||
} | ||
|
||
@Override | ||
public FlintMetadata getIndexMetadata(String indexName) { | ||
return handleExceptions(() -> delegate.getIndexMetadata(indexName)); | ||
} | ||
|
||
@Override | ||
public void deleteIndex(String indexName) { | ||
try { | ||
delegate.deleteIndex(indexName); | ||
} catch (AccessDeniedException exception){ | ||
handleAccessDeniedException(); | ||
throw exception; | ||
} catch (Throwable t) { | ||
handleThrowable(); | ||
throw t; | ||
} | ||
} | ||
|
||
@Override | ||
public FlintReader createReader(String indexName, String query) { | ||
return handleExceptions(() -> delegate.createReader(indexName, query)); | ||
} | ||
|
||
@Override | ||
public FlintWriter createWriter(String indexName) { | ||
return handleExceptions(() -> delegate.createWriter(indexName)); | ||
} | ||
|
||
@Override | ||
public RestHighLevelClient createClient() { | ||
return handleExceptions(delegate::createClient); | ||
} | ||
|
||
private <T> T handleExceptions(Supplier<T> function) { | ||
try { | ||
return function.get(); | ||
} catch (AccessDeniedException exception) { | ||
handleAccessDeniedException(); | ||
throw exception; | ||
} catch (Throwable t) { | ||
handleThrowable(); | ||
throw new RuntimeException(t); | ||
} | ||
} | ||
|
||
private void handleThrowable(){ | ||
String clusterName = System.getenv("FLINT_AUTH_DOMAIN_IDENTIFIER"); | ||
if (clusterName == null) { | ||
clusterName = "unknown"; | ||
} | ||
DimensionedName metricName = DimensionedName.withName("FlintOpenSearchAccessError") | ||
.withDimension("domain_ident", clusterName) | ||
.build(); | ||
publishMetric(metricName); | ||
} | ||
|
||
private void handleAccessDeniedException() { | ||
String clusterName = System.getenv("FLINT_AUTH_DOMAIN_IDENTIFIER"); | ||
if (clusterName == null) { | ||
clusterName = "unknown"; | ||
} | ||
DimensionedName metricName = DimensionedName.withName("FlintOpenSearchAccessDeniedError") | ||
.withDimension("domain_ident", clusterName) | ||
.build(); | ||
publishMetric(metricName); | ||
} | ||
|
||
private void publishMetric(DimensionedName metricName) { | ||
FlintMetricSource flintMetricSource = | ||
(FlintMetricSource) SparkEnv.get().metricsSystem().getSourcesByName("FlintMetricSource"); | ||
if (flintMetricSource != null) { | ||
Counter flintOpenSearchAccessError = | ||
flintMetricSource.metricRegistry().getCounters().get(metricName.encode()); | ||
if (flintOpenSearchAccessError == null) { | ||
flintMetricSource.metricRegistry().counter(metricName.encode()); | ||
} | ||
} | ||
} | ||
|
||
} |
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