bowenliang123 commented on code in PR #5488: URL: https://github.com/apache/kyuubi/pull/5488#discussion_r1381423935
########## kyuubi-events/src/main/scala/org/apache/kyuubi/events/handler/ElasticSearchLoggingEventHandler.scala: ########## @@ -0,0 +1,137 @@ +/* + * 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.{ElasticClient, ElasticProperties, RequestFailure} +import com.sksamuel.elastic4s.ElasticApi.indexInto +import com.sksamuel.elastic4s.ElasticDsl._ +import com.sksamuel.elastic4s.http.JavaClient +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.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 +import org.apache.kyuubi.events.handler.ElasticSearchLoggingEventHandler.getElasticClient + +/** + * 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 = getElasticClient(serverUrl, username, password) + + 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 = { + val fields = { + objectMapper.readValue(event.toJson, classOf[Map[String, Any]]).map { + case (key: String, value: Any) => + 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 match { + case f: RequestFailure => + error(s"Failed to send event in ElasticSearchLoggingEventHandler, ${f.error}") + case _ => + } + } + + override def close(): Unit = { + esClient.close() + } + + private def throwExceptionIndexNotfound(indexId: String) = + throw new RuntimeException(s"the index '$indexId' is not found on ElasticSearch") Review Comment: Fixed. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
