Skip to content

Commit

Permalink
feat: Add missing fields (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
phzietsman authored Oct 2, 2024
1 parent dfbffbe commit 8b9a350
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 4 deletions.
25 changes: 25 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use the Trino base image
FROM trinodb/trino:452

# Create the plugin directory
RUN mkdir -p /usr/lib/trino/plugin/trino-event-logger

# Copy the plugin JAR files
COPY /target/dependency/*.jar /usr/lib/trino/plugin/trino-event-logger/
COPY /target/trino-event-logger*.jar /usr/lib/trino/plugin/trino-event-logger/

# Create event-listener.properties file
RUN echo "event-listener.name=telogger" > /etc/trino/event-listener.properties && \
echo "telogger.url=jdbc:postgresql://postgres:5432/trino_db_event_listener" >> /etc/trino/event-listener.properties && \
echo "telogger.user=trino_user" >> /etc/trino/event-listener.properties && \
echo "telogger.password=trino_password" >> /etc/trino/event-listener.properties

# Create PostgreSQL catalog properties file
RUN mkdir -p /etc/trino/catalog && \
echo "connector.name=postgresql" > /etc/trino/catalog/postgresql.properties && \
echo "connection-url=jdbc:postgresql://postgres:5432/trino_db_event_listener" >> /etc/trino/catalog/postgresql.properties && \
echo "connection-user=trino_user" >> /etc/trino/catalog/postgresql.properties && \
echo "connection-password=trino_password" >> /etc/trino/catalog/postgresql.properties

# Start Trino
CMD ["/usr/lib/trino/bin/run-trino"]
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,8 @@ If the plugin is working correctly, you will see the following in the Trino
2022-04-29T16:03:12.663-0400 INFO main io.trino.dblistener.FlywayMigration Performed 1 migrations
```

## Testing
```bash
docker compose up --build
docker compose down -v
```
33 changes: 33 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
version: '3.8'

services:
trino:
build:
context: .
dockerfile: Dockerfile
ports:
- "8081:8080"
depends_on:
- postgres
networks:
- trino-network

postgres:
image: postgres:13
environment:
POSTGRES_DB: trino_db_event_listener
POSTGRES_USER: trino_user
POSTGRES_PASSWORD: trino_password
ports:
- "5455:5432"
volumes:
- postgres-data:/var/lib/postgresql/data
networks:
- trino-network

networks:
trino-network:
driver: bridge

volumes:
postgres-data:
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>cloudandthings</groupId>
<artifactId>trino-event-logger</artifactId>
<version>452.1</version>
<version>452.4</version>
<description>A Trino Event Logger. Supports MSSQL, MYSQL and PostGreSQL</description>

<properties>
Expand Down
32 changes: 29 additions & 3 deletions src/main/java/cloudandthings/plugin/telogger/TELogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* limitations under the License.
*/
package cloudandthings.plugin.telogger;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.inject.Inject;
import io.airlift.json.JsonCodec;
import io.airlift.json.JsonCodecFactory;
Expand Down Expand Up @@ -91,7 +92,13 @@ private void saveQuery(QueryCompletedEvent queryCompletedEvent, Handle handle)
" cumulative_memory," +
" failed_cumulative_memory," +
" completed_splits," +
" stage_gc_statistics" +
" stage_gc_statistics," +
" user_name," +
" user_original," +
" server_version," +
" enabled_roles," +
" groups," +
" principal" +
")" +
"VALUES (" +
" :query_id," +
Expand Down Expand Up @@ -135,7 +142,13 @@ private void saveQuery(QueryCompletedEvent queryCompletedEvent, Handle handle)
" :cumulative_memory," +
" :failed_cumulative_memory," +
" :completed_splits," +
" :stage_gc_statistics" +
" :stage_gc_statistics," +
" :user_name," +
" :user_original," +
" :server_version," +
" :enabled_roles," +
" :groups," +
" :principal" +
")";

QueryStatistics queryStatistics = queryCompletedEvent.getStatistics();
Expand All @@ -151,6 +164,13 @@ private void saveQuery(QueryCompletedEvent queryCompletedEvent, Handle handle)
long outputBlockedTime = queryStatistics.getOutputBlockedTime().isPresent() ? queryStatistics.getOutputBlockedTime().get().toMillis() : 0;
long failedOutputBlockedTime = queryStatistics.getFailedOutputBlockedTime().isPresent() ? queryStatistics.getFailedOutputBlockedTime().get().toMillis() : 0;

String contextUser = queryCompletedEvent.getContext().getUser();
String contextUserOriginal = queryCompletedEvent.getContext().getOriginalUser();
String contextServerVersion = queryCompletedEvent.getContext().getServerVersion();
String contextEnabledRoles = String.join(",", queryCompletedEvent.getContext().getEnabledRoles());
String contextGroups = String.join(",", queryCompletedEvent.getContext().getGroups());
String contextPrincipal = queryCompletedEvent.getContext().getPrincipal().orElse("");

handle.createUpdate(sql)
.bind("query_id", queryCompletedEvent.getMetadata().getQueryId())
.bind("catalog", queryCompletedEvent.getContext().getCatalog().orElse(""))
Expand Down Expand Up @@ -194,6 +214,12 @@ private void saveQuery(QueryCompletedEvent queryCompletedEvent, Handle handle)
.bind("failed_cumulative_memory", queryStatistics.getFailedCumulativeMemory())
.bind("completed_splits", queryStatistics.getCompletedSplits())
.bind("stage_gc_statistics", STAGE_GC_STATS_CODEC.toJson(queryStatistics.getStageGcStatistics()))
.bind("user_name", contextUser)
.bind("user_original", contextUserOriginal)
.bind("server_version", contextServerVersion)
.bind("enabled_roles", contextEnabledRoles)
.bind("groups", contextGroups)
.bind("principal", contextPrincipal)
.execute();
}
}
}
19 changes: 19 additions & 0 deletions src/main/resources/db/mysql/V2__add_username.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
ALTER TABLE queries
ADD COLUMN user_name VARCHAR(256);

ALTER TABLE queries
ADD COLUMN user_original VARCHAR(256);

ALTER TABLE queries
ADD COLUMN server_version VARCHAR(256);

ALTER TABLE queries
ADD COLUMN enabled_roles LONGTEXT;

ALTER TABLE queries
ADD COLUMN groups LONGTEXT;

ALTER TABLE queries
ADD COLUMN principal VARCHAR(256);

CREATE INDEX idx_queries_user_name ON queries (user_name);
19 changes: 19 additions & 0 deletions src/main/resources/db/postgresql/V2__add_username.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
ALTER TABLE queries
ADD COLUMN user_name VARCHAR(256);

ALTER TABLE queries
ADD COLUMN user_original VARCHAR(256);

ALTER TABLE queries
ADD COLUMN server_version VARCHAR(256);

ALTER TABLE queries
ADD COLUMN enabled_roles TEXT;

ALTER TABLE queries
ADD COLUMN groups TEXT;

ALTER TABLE queries
ADD COLUMN principal VARCHAR(256);

CREATE INDEX idx_queries_user_name ON queries (user_name);
19 changes: 19 additions & 0 deletions src/main/resources/db/sqlserver/V2__add_username.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
ALTER TABLE queries
ADD user_name VARCHAR(256);

ALTER TABLE queries
ADD user_original VARCHAR(256);

ALTER TABLE queries
ADD server_version VARCHAR(256);

ALTER TABLE queries
ADD enabled_roles NVARCHAR(MAX);

ALTER TABLE queries
ADD groups NVARCHAR(MAX);

ALTER TABLE queries
ADD principal VARCHAR(256);

CREATE INDEX idx_queries_user_name ON queries (user_name);

0 comments on commit 8b9a350

Please sign in to comment.