Skip to content

Commit

Permalink
[LIVY-707] Add audit log for SqlJobs from ThriftServer
Browse files Browse the repository at this point in the history
## What changes were proposed in this pull request?

We should add audit logs in thriftServer for admin to easily to manage operations,

## How was this patch tested?

An audit log example showed below,

```
19/11/06 16:38:30 INFO ThriftServerAudit$: user: test ipAddress: 10.25.22.46 query: select count(*) from test1 beforeExecute: 1573029416951 afterExecute: 1573029510972 time spent: 94021
```

Author: BoneAn <[email protected]>

Closes apache#255 from huianyi/LIVY-707.
  • Loading branch information
BoneAn authored and jerryshao committed Nov 14, 2019
1 parent 7847c3f commit 6261c57
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ class LivyExecuteStatementOperation(
}
setState(OperationState.RUNNING)

val before = System.currentTimeMillis()

try {
rpcClient.executeSql(sessionHandle, statementId, statement).get()
} catch {
Expand All @@ -147,6 +149,10 @@ class LivyExecuteStatementOperation(
throw new HiveSQLException(e)
}
setState(OperationState.FINISHED)

val sessionInfo = sessionManager.getSessionInfo(sessionHandle)
val after = System.currentTimeMillis()
ThriftServerAudit.audit(sessionInfo.username, sessionInfo.ipAddress, statement, before, after)
}

def close(): Unit = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.livy.thriftserver

import org.apache.livy.Logging

object ThriftServerAudit extends Logging {

def audit(
user: String,
ipAddress: String,
query: String,
startTime: Long,
endTime: Long): Unit = {
info(
s"user: $user ipAddress: $ipAddress query: ${query.replace('\n', ' ')} " +
s"start time: ${startTime} end time: ${endTime} " +
s"time spent: ${Math.round((endTime - startTime) / 1000)}s")
}

}

0 comments on commit 6261c57

Please sign in to comment.