Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ public ClientSqlParameter(
this.value = value;
}

@JsonProperty
public String getType()
{
return type;
}

@JsonProperty
public Object getValue()
{
return value;
}

@Override
public boolean equals(Object o)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.query.http;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import nl.jqno.equalsverifier.EqualsVerifier;
import org.apache.druid.segment.TestHelper;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class ClientSqlParameterTest
{
@Test
public void testSerde() throws JsonProcessingException
{
ObjectMapper mapper = TestHelper.makeJsonMapper();
ClientSqlParameter sqlParameter = new ClientSqlParameter(
"BIGINT",
1234
);
// serde here suffers normal jackson problems e.g. if 1234 was 1234L this test would fail
Assertions.assertEquals(
sqlParameter,
mapper.readValue(mapper.writeValueAsString(sqlParameter), ClientSqlParameter.class)
);
}

@Test
public void testEqualsAndHashcode()
{
EqualsVerifier.forClass(ClientSqlParameter.class).usingGetClass().verify();
}
}
34 changes: 31 additions & 3 deletions server/src/main/java/org/apache/druid/server/RequestLogLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
import com.google.common.collect.ImmutableMap;
import org.apache.druid.java.util.common.StringUtils;
import org.apache.druid.query.Query;
import org.apache.druid.query.http.ClientSqlParameter;
import org.joda.time.DateTime;

import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

Expand All @@ -41,6 +43,7 @@ public class RequestLogLine

private final Query<?> query;
private final String sql;
private final List<ClientSqlParameter> sqlParameters;
private final Map<String, Object> sqlQueryContext;
private final DateTime timestamp;
private final String remoteAddr;
Expand All @@ -49,6 +52,7 @@ public class RequestLogLine
private RequestLogLine(
@Nullable Query<?> query,
@Nullable String sql,
@Nullable List<ClientSqlParameter> sqlParameters,
@Nullable Map<String, Object> sqlQueryContext,
DateTime timestamp,
@Nullable String remoteAddr,
Expand All @@ -57,6 +61,7 @@ private RequestLogLine(
{
this.query = query;
this.sql = sql;
this.sqlParameters = sqlParameters;
this.sqlQueryContext = sqlQueryContext != null ? sqlQueryContext : ImmutableMap.of();
this.timestamp = Preconditions.checkNotNull(timestamp, "timestamp");
this.remoteAddr = StringUtils.nullToEmptyNonDruidDataString(remoteAddr);
Expand All @@ -65,7 +70,7 @@ private RequestLogLine(

public static RequestLogLine forNative(Query<?> query, DateTime timestamp, String remoteAddr, QueryStats queryStats)
{
return new RequestLogLine(query, null, null, timestamp, remoteAddr, queryStats);
return new RequestLogLine(query, null, null, null, timestamp, remoteAddr, queryStats);
}

public static RequestLogLine forSql(
Expand All @@ -76,7 +81,19 @@ public static RequestLogLine forSql(
QueryStats queryStats
)
{
return new RequestLogLine(null, sql, sqlQueryContext, timestamp, remoteAddr, queryStats);
return forSql(sql, null, sqlQueryContext, timestamp, remoteAddr, queryStats);
}

public static RequestLogLine forSql(
String sql,
List<ClientSqlParameter> parameters,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add @Nullable since we explicitly pass null in the above call

Map<String, Object> sqlQueryContext,
DateTime timestamp,
String remoteAddr,
QueryStats queryStats
)
{
return new RequestLogLine(null, sql, parameters, sqlQueryContext, timestamp, remoteAddr, queryStats);
}

public String getNativeQueryLine(ObjectMapper objectMapper) throws JsonProcessingException
Expand All @@ -96,6 +113,9 @@ public String getSqlQueryLine(ObjectMapper objectMapper) throws JsonProcessingEx
final Map<String, Object> queryMap = new LinkedHashMap<>();
queryMap.put("context", sqlQueryContext);
queryMap.put("query", sql == null ? "<unavailable>" : sql);
if (sqlParameters != null) {
queryMap.put("parameters", sqlParameters);
}

return JOINER.join(
Arrays.asList(
Expand All @@ -122,6 +142,13 @@ public String getSql()
return sql;
}

@Nullable
@JsonProperty("sqlParameters")
public List<ClientSqlParameter> getSqlParameters()
{
return sqlParameters;
}

@Nullable
@JsonProperty
public Map<String, Object> getSqlQueryContext()
Expand Down Expand Up @@ -160,6 +187,7 @@ public boolean equals(Object o)
RequestLogLine that = (RequestLogLine) o;
return Objects.equals(query, that.query) &&
Objects.equals(sql, that.sql) &&
Objects.equals(sqlParameters, that.sqlParameters) &&
Objects.equals(sqlQueryContext, that.sqlQueryContext) &&
Objects.equals(timestamp, that.timestamp) &&
Objects.equals(remoteAddr, that.remoteAddr) &&
Expand All @@ -169,7 +197,7 @@ public boolean equals(Object o)
@Override
public int hashCode()
{
return Objects.hash(query, sql, sqlQueryContext, timestamp, remoteAddr, queryStats);
return Objects.hash(query, sql, sqlParameters, sqlQueryContext, timestamp, remoteAddr, queryStats);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.apache.druid.server.log;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonValue;
import com.google.common.collect.ImmutableMap;
Expand All @@ -29,6 +30,8 @@
import org.apache.druid.server.RequestLogLine;
import org.joda.time.DateTime;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Map;

/**
Expand Down Expand Up @@ -69,7 +72,8 @@ public EventMap toMap()

if (getSql() != null) {
builder.put("sqlQueryContext", getSqlQueryContext())
.put("sql", getSql());
.put("sql", getSql())
.putNonNull("sqlParameters", getSqlParameters());
}

return builder.build();
Expand Down Expand Up @@ -112,6 +116,14 @@ public String getSql()
return request.getSql();
}

@Nullable
@JsonProperty("sqlParameters")
@JsonInclude(JsonInclude.Include.NON_NULL)
public List<?> getSqlParameters()
{
return request.getSqlParameters();
}

@JsonProperty("sqlQueryContext")
public Map<String, Object> getSqlQueryContext()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.druid.java.util.emitter.core.EventMap;
import org.apache.druid.query.Query;
import org.apache.druid.query.TableDataSource;
import org.apache.druid.query.http.ClientSqlParameter;
import org.apache.druid.query.spec.MultipleIntervalSegmentSpec;
import org.apache.druid.query.timeseries.TimeseriesQuery;
import org.apache.druid.segment.VirtualColumns;
Expand All @@ -43,6 +44,7 @@

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DefaultRequestLogEventTest
Expand All @@ -53,29 +55,32 @@ public class DefaultRequestLogEventTest
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"))
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);
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\"},"
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,\"granularity\":{\"type\":\"all\"},\"limit\":5,"
+ "\"context\":{\"key\":\"value\"}},\"host\":\"127.0.0.1\",\"timestamp\":\"2019-12-12T03:01:00.000Z\","
Expand All @@ -101,7 +106,8 @@ public void testDefaultRequestLogEventToMap()
ImmutableList.of(),
ImmutableList.of(),
5,
ImmutableMap.of("key", "value"));
ImmutableMap.of("key", "value")
);
final QueryStats queryStats = new QueryStats(
ImmutableMap.of("query/time", 13L, "query/bytes", 10L, "success", true, "identity", "allowAll"));
RequestLogLine nativeLine = RequestLogLine.forNative(
Expand Down Expand Up @@ -133,7 +139,10 @@ public void testDefaultRequestLogEventToMapSQL() throws JsonProcessingException
final DateTime timestamp = DateTimes.of(2019, 12, 12, 3, 1);
final String service = "druid-service";
final String host = "127.0.0.1";
final String sql = "select * from 1337";
final String sql = "select * from foo where x = ?";
final List<ClientSqlParameter> parameters = List.of(
new ClientSqlParameter("BIGINT", 1234L)
);
final QueryStats queryStats = new QueryStats(
ImmutableMap.of(
"sqlQuery/time", 13L,
Expand All @@ -146,6 +155,7 @@ public void testDefaultRequestLogEventToMapSQL() throws JsonProcessingException

RequestLogLine nativeLine = RequestLogLine.forSql(
sql,
parameters,
ImmutableMap.of(),
timestamp,
host,
Expand All @@ -161,6 +171,7 @@ public void testDefaultRequestLogEventToMapSQL() throws JsonProcessingException
expected.put("service", service);
expected.put("host", host);
expected.put("sql", sql);
expected.put("sqlParameters", parameters);
expected.put("sqlQueryContext", ImmutableMap.of());
expected.put("remoteAddr", host);
expected.put("queryStats", queryStats);
Expand All @@ -169,7 +180,7 @@ public void testDefaultRequestLogEventToMapSQL() throws JsonProcessingException
Assert.assertEquals(expected, observedEventMap);
Assert.assertEquals(
StringUtils.format(
"{\"feed\":\"test\",\"timestamp\":\"%s\",\"service\":\"druid-service\",\"host\":\"127.0.0.1\",\"remoteAddr\":\"127.0.0.1\",\"queryStats\":{\"sqlQuery/time\":13,\"sqlQuery/planningTimeMs\":1,\"sqlQuery/bytes\":10,\"success\":true,\"identity\":\"allowAll\"},\"sqlQueryContext\":{},\"sql\":\"select * from 1337\"}",
"{\"feed\":\"test\",\"timestamp\":\"2019-12-12T03:01:00.000Z\",\"service\":\"druid-service\",\"host\":\"127.0.0.1\",\"remoteAddr\":\"127.0.0.1\",\"queryStats\":{\"sqlQuery/time\":13,\"sqlQuery/planningTimeMs\":1,\"sqlQuery/bytes\":10,\"success\":true,\"identity\":\"allowAll\"},\"sqlQueryContext\":{},\"sql\":\"select * from foo where x = ?\",\"sqlParameters\":[{\"type\":\"BIGINT\",\"value\":1234}]}",
timestamp
),
new DefaultObjectMapper().writeValueAsString(observedEventMap)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.apache.druid.query.TableDataSource;
import org.apache.druid.query.UnionDataSource;
import org.apache.druid.query.filter.DimFilter;
import org.apache.druid.query.http.ClientSqlParameter;
import org.apache.druid.query.spec.QuerySegmentSpec;
import org.apache.druid.server.QueryStats;
import org.apache.druid.server.RequestLogLine;
Expand Down Expand Up @@ -212,15 +213,20 @@ public void testSimpleLogging() throws Exception
public void testSqlLogging() throws Exception
{
final RequestLogLine sqlLogLine = RequestLogLine.forSql(
"select * from foo", Map.of("sqlQueryId", "id1"), DateTimes.of("2026-01-01"), null, new QueryStats(Map.of("query/time", 13L))
"select * from foo WHERE x = ?",
List.of(new ClientSqlParameter("BIGINT", 1234L)),
Map.of("sqlQueryId", "id1"),
DateTimes.of("2026-01-01"),
null,
new QueryStats(Map.of("query/time", 13L))
);
final LoggingRequestLogger requestLogger = new LoggingRequestLogger(MAPPER, true, false);

requestLogger.logSqlQuery(sqlLogLine);
final String observedLogLine = BAOS.toString(StandardCharsets.UTF_8);

Assert.assertEquals(
"2026-01-01T00:00:00.000Z\t\t\t{\"query/time\":13}\t{\"context\":{\"sqlQueryId\":\"id1\"},\"query\":\"select * from foo\"}",
"2026-01-01T00:00:00.000Z\t\t\t{\"query/time\":13}\t{\"context\":{\"sqlQueryId\":\"id1\"},\"query\":\"select * from foo WHERE x = ?\",\"parameters\":[{\"type\":\"BIGINT\",\"value\":1234}]}",
MAPPER.readTree(observedLogLine).get("message").asText()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ public void emit()
stmt.sqlToolbox.requestLogger.logSqlQuery(
RequestLogLine.forSql(
stmt.queryPlus.sql(),
stmt.queryPlus.sqlParameters(),
queryContext,
DateTimes.utc(startMs),
remoteAddress,
Expand Down
Loading
Loading