From d9beda7f2449ddf5f4e6c7f60de293a431ca2c00 Mon Sep 17 00:00:00 2001 From: Mainak Ghosh Date: Fri, 18 Sep 2020 00:46:31 -0700 Subject: [PATCH] Adding the missing sqlQueryContext api (#10368) * Adding the missing sqlQueryContext api * Adding a serialization test for DefaultRequestLogEvent * Fixing the unit test failure --- .../server/log/DefaultRequestLogEvent.java | 6 ++ .../log/DefaultRequestLogEventTest.java | 71 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 server/src/test/java/org/apache/druid/server/log/DefaultRequestLogEventTest.java diff --git a/server/src/main/java/org/apache/druid/server/log/DefaultRequestLogEvent.java b/server/src/main/java/org/apache/druid/server/log/DefaultRequestLogEvent.java index cb48d3567f1..2224e62ddb1 100644 --- a/server/src/main/java/org/apache/druid/server/log/DefaultRequestLogEvent.java +++ b/server/src/main/java/org/apache/druid/server/log/DefaultRequestLogEvent.java @@ -96,6 +96,12 @@ public final class DefaultRequestLogEvent implements RequestLogEvent return request.getSql(); } + @JsonProperty("sqlQueryContext") + public Map getSqlQueryContext() + { + return request.getSqlQueryContext(); + } + @JsonProperty("remoteAddr") public String getRemoteAddr() { diff --git a/server/src/test/java/org/apache/druid/server/log/DefaultRequestLogEventTest.java b/server/src/test/java/org/apache/druid/server/log/DefaultRequestLogEventTest.java new file mode 100644 index 00000000000..fe7899903ac --- /dev/null +++ b/server/src/test/java/org/apache/druid/server/log/DefaultRequestLogEventTest.java @@ -0,0 +1,71 @@ +/* + * 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.druid.server.log; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import org.apache.druid.jackson.DefaultObjectMapper; +import org.apache.druid.java.util.common.DateTimes; +import org.apache.druid.java.util.common.Intervals; +import org.apache.druid.java.util.common.granularity.Granularities; +import org.apache.druid.query.TableDataSource; +import org.apache.druid.query.spec.MultipleIntervalSegmentSpec; +import org.apache.druid.query.timeseries.TimeseriesQuery; +import org.apache.druid.segment.VirtualColumns; +import org.apache.druid.server.QueryStats; +import org.apache.druid.server.RequestLogLine; +import org.junit.Assert; +import org.junit.Test; + +public class DefaultRequestLogEventTest +{ + private ObjectMapper objectMapper = new DefaultObjectMapper(); + + @Test + public void testDefaultRequestLogEventSerde() throws Exception + { + RequestLogLine nativeLine = RequestLogLine.forNative( + new TimeseriesQuery( + new TableDataSource("dummy"), + new MultipleIntervalSegmentSpec(ImmutableList.of(Intervals.of("2015-01-01/2015-01-02"))), + true, + VirtualColumns.EMPTY, + null, + Granularities.ALL, + ImmutableList.of(), + ImmutableList.of(), + 5, + ImmutableMap.of("key", "value")), + DateTimes.of(2019, 12, 12, 3, 1), + "127.0.0.1", + new QueryStats(ImmutableMap.of("query/time", 13L, "query/bytes", 10L, "success", true, "identity", "allowAll")) + ); + + DefaultRequestLogEvent defaultRequestLogEvent = new DefaultRequestLogEvent( + ImmutableMap.of("service", "druid-service", "host", "127.0.0.1"), + "feed", + nativeLine); + + String logEventJson = objectMapper.writeValueAsString(defaultRequestLogEvent); + String expected = "{\"feed\":\"feed\",\"query\":{\"queryType\":\"timeseries\",\"dataSource\":{\"type\":\"table\",\"name\":\"dummy\"},\"intervals\":{\"type\":\"intervals\",\"intervals\":[\"2015-01-01T00:00:00.000Z/2015-01-02T00:00:00.000Z\"]},\"descending\":true,\"virtualColumns\":[],\"filter\":null,\"granularity\":{\"type\":\"all\"},\"aggregations\":[],\"postAggregations\":[],\"limit\":5,\"context\":{\"key\":\"value\"}},\"host\":\"127.0.0.1\",\"timestamp\":\"2019-12-12T03:01:00.000Z\",\"service\":\"druid-service\",\"sql\":null,\"sqlQueryContext\":{},\"remoteAddr\":\"127.0.0.1\",\"queryStats\":{\"query/time\":13,\"query/bytes\":10,\"success\":true,\"identity\":\"allowAll\"}}"; + Assert.assertEquals(objectMapper.readTree(expected), objectMapper.readTree(logEventJson)); + } +}