-
Notifications
You must be signed in to change notification settings - Fork 931
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
1 parent
cfd90e0
commit 39adfff
Showing
13 changed files
with
422 additions
and
17 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
139 changes: 139 additions & 0 deletions
139
...ts/src/main/scala/org/apache/kyuubi/events/handler/ElasticSearchLoggingEventHandler.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,139 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.kyuubi.events.handler | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.module.scala.DefaultScalaModule | ||
import com.sksamuel.elastic4s._ | ||
import com.sksamuel.elastic4s.ElasticApi.indexInto | ||
import com.sksamuel.elastic4s.ElasticDsl._ | ||
import com.sksamuel.elastic4s.http._ | ||
import com.sksamuel.elastic4s.requests.common.RefreshPolicy | ||
import com.sksamuel.elastic4s.requests.mappings.MappingDefinition | ||
import com.sksamuel.elastic4s.requests.mappings.dynamictemplate.DynamicMapping | ||
import org.apache.commons.lang3.StringUtils | ||
import org.apache.hadoop.util.Preconditions.checkArgument | ||
import org.apache.http.auth.{AuthScope, UsernamePasswordCredentials} | ||
import org.apache.http.client.config.RequestConfig | ||
import org.apache.http.impl.client.BasicCredentialsProvider | ||
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder | ||
|
||
import org.apache.kyuubi.Logging | ||
import org.apache.kyuubi.config.KyuubiConf | ||
import org.apache.kyuubi.config.KyuubiConf._ | ||
import org.apache.kyuubi.events.KyuubiEvent | ||
|
||
/** | ||
* This event logger logs events to ElasticSearch. | ||
*/ | ||
class ElasticSearchLoggingEventHandler( | ||
indexId: String, | ||
serverUrl: String, | ||
username: Option[String] = None, | ||
password: Option[String] = None, | ||
kyuubiConf: KyuubiConf) extends EventHandler[KyuubiEvent] with Logging { | ||
|
||
private val isAutoCreateIndex: Boolean = | ||
kyuubiConf.get(SERVER_EVENT_ELASTICSEARCH_INDEX_AUTOCREATE_ENABLED) | ||
|
||
private val esClient: ElasticClient = { | ||
val props = ElasticProperties(serverUrl) | ||
lazy val provider = { | ||
val provider = new BasicCredentialsProvider | ||
val credentials = | ||
new UsernamePasswordCredentials(username.getOrElse(""), password.getOrElse("")) | ||
provider.setCredentials(AuthScope.ANY, credentials) | ||
provider | ||
} | ||
val javaClient = JavaClient( | ||
props, | ||
(requestConfigBuilder: RequestConfig.Builder) => requestConfigBuilder, | ||
(httpClientBuilder: HttpAsyncClientBuilder) => { | ||
httpClientBuilder.setDefaultCredentialsProvider(provider) | ||
}) | ||
ElasticClient(javaClient) | ||
} | ||
|
||
private def checkIndexExisted: String => Boolean = (indexId: String) => { | ||
checkArgument( | ||
StringUtils.isNotBlank(indexId), | ||
"index name must be configured for ElasticSearchEventHandler, please ensure %s is set", | ||
SERVER_EVENT_ELASTICSEARCH_INDEX.key) | ||
// check | ||
esClient.execute { | ||
indexExists(indexId) | ||
}.await.result.isExists | ||
} | ||
|
||
private def checkAndEnsureIndex(indexId: String): Unit = { | ||
if (!checkIndexExisted(indexId) && isAutoCreateIndex) { | ||
esClient.execute { | ||
createIndex(indexId) | ||
.mapping(MappingDefinition(dynamic = Some(DynamicMapping.Dynamic))) | ||
}.await | ||
if (!checkIndexExisted(indexId)) { | ||
throwExceptionIndexNotfound(indexId) | ||
} | ||
} else { | ||
throwExceptionIndexNotfound(indexId) | ||
} | ||
} | ||
|
||
checkAndEnsureIndex(indexId) | ||
|
||
private val objectMapper = new ObjectMapper().registerModule(DefaultScalaModule) | ||
|
||
override def apply(event: KyuubiEvent): Unit = { | ||
|
||
try { | ||
val fields = { | ||
val entries = objectMapper.readValue(event.toJson, classOf[Map[String, Any]]) | ||
entries.map(p => { | ||
val key = p._1 | ||
val value = p._2 | ||
val isNumber = (obj: Any) => { | ||
(obj.isInstanceOf[Byte] || obj.isInstanceOf[Short] || | ||
obj.isInstanceOf[Int] || | ||
obj.isInstanceOf[Long]).||(obj.isInstanceOf[Float]) || | ||
obj.isInstanceOf[Double] | ||
} | ||
if (value.isInstanceOf[String] || isNumber(value)) { | ||
(key, value) | ||
} else { | ||
(key, objectMapper.writeValueAsString(value)) | ||
} | ||
}) | ||
} | ||
esClient.execute { | ||
indexInto(indexId) | ||
.fields(fields) | ||
.refresh(RefreshPolicy.Immediate) | ||
}.await | ||
} catch { | ||
case e: Exception => | ||
error("Failed to send event in ElasticSearchEventHandler", e) | ||
} | ||
} | ||
|
||
override def close(): Unit = { | ||
esClient.close() | ||
} | ||
|
||
private def throwExceptionIndexNotfound(indexId: String) = | ||
throw new RuntimeException(s"the index '$indexId' is not found on ElasticSearch") | ||
} |
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
28 changes: 28 additions & 0 deletions
28
.../main/scala/org/apache/kyuubi/events/handler/ServerElasticSearchLoggingEventHandler.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,28 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.kyuubi.events.handler | ||
|
||
import org.apache.kyuubi.config.KyuubiConf | ||
|
||
case class ServerElasticSearchLoggingEventHandler( | ||
indexId: String, | ||
serverUrl: String, | ||
username: Option[String], | ||
password: Option[String], | ||
kyuubiConf: KyuubiConf) | ||
extends ElasticSearchLoggingEventHandler(indexId, serverUrl, username, password, kyuubiConf) |
Oops, something went wrong.