Skip to content

Commit

Permalink
Populate data stream fields when xOpaqueId not provided (#62156)
Browse files Browse the repository at this point in the history
datastream related fields should be always be provided.
message field is already set with a lazy value (Object.toString) within
ESLogMessage
Also removing test cases to directly access deprecation logger with LogManger.getLogger("deprecation"). This is disallowed. DeprecationLogger.getLogger should be used.
  • Loading branch information
pgomulka authored Sep 18, 2020
1 parent 2ec7260 commit 95c1488
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.elasticsearch.common.logging.DeprecationLogger.DEPRECATION;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.hasEntry;
Expand Down Expand Up @@ -87,6 +86,40 @@ public void tearDown() throws Exception {
super.tearDown();
}

public void testDeprecatedMessageWithoutXOpaqueId() throws IOException {
final DeprecationLogger testLogger = DeprecationLogger.getLogger("test");

testLogger.deprecate("a key", "deprecated message1");

final Path path = PathUtils.get(System.getProperty("es.logs.base_path"),
System.getProperty("es.logs.cluster_name") + "_deprecated.json");

try (Stream<Map<String, String>> stream = JsonLogsStream.mapStreamFrom(path)) {
List<Map<String, String>> jsonLogs = stream
.collect(Collectors.toList());

assertThat(jsonLogs, contains(
allOf(
hasEntry("type", "deprecation"),
hasEntry("log.level", "DEPRECATION"),
hasEntry("log.logger", "deprecation.test"),
hasEntry("cluster.name", "elasticsearch"),
hasEntry("node.name", "sample-name"),
hasEntry("message", "deprecated message1"),
hasEntry("data_stream.type", "logs"),
hasEntry("data_stream.datatype", "deprecation"),
hasEntry("data_stream.namespace", "elasticsearch"),
hasEntry("ecs.version", DeprecatedMessage.ECS_VERSION),
hasEntry("key", "a key"),
not(hasKey("x-opaque-id"))
)
)
);
}

assertWarnings("deprecated message1");
}

public void testDeprecatedMessage() throws Exception {
withThreadContext(threadContext -> {
threadContext.putHeader(Task.X_OPAQUE_ID, "someId");
Expand All @@ -111,6 +144,11 @@ public void testDeprecatedMessage() throws Exception {
hasEntry("cluster.name", "elasticsearch"),
hasEntry("node.name", "sample-name"),
hasEntry("message", "deprecated message1"),
hasEntry("data_stream.type", "logs"),
hasEntry("data_stream.datatype", "deprecation"),
hasEntry("data_stream.namespace", "elasticsearch"),
hasEntry("ecs.version", DeprecatedMessage.ECS_VERSION),
hasEntry("key", "someKey"),
hasEntry("x-opaque-id", "someId")
)
)
Expand Down Expand Up @@ -178,49 +216,6 @@ public void testCustomMessageWithMultipleFields() throws IOException {
}
}


public void testDeprecatedMessageWithoutXOpaqueId() throws IOException {
final DeprecationLogger testLogger = DeprecationLogger.getLogger("test");

testLogger.deprecate("a key", "deprecated message1");

// Also test that a message sent directly to the logger comes through
final Logger rawLogger = LogManager.getLogger("deprecation.test");
rawLogger.log(DEPRECATION, "deprecated message2");

final Path path = PathUtils.get(System.getProperty("es.logs.base_path"),
System.getProperty("es.logs.cluster_name") + "_deprecated.json");

try (Stream<Map<String, String>> stream = JsonLogsStream.mapStreamFrom(path)) {
List<Map<String, String>> jsonLogs = stream
.collect(Collectors.toList());

assertThat(jsonLogs, contains(
allOf(
hasEntry("type", "deprecation"),
hasEntry("log.level", "DEPRECATION"),
hasEntry("log.logger", "deprecation.test"),
hasEntry("cluster.name", "elasticsearch"),
hasEntry("node.name", "sample-name"),
hasEntry("message", "deprecated message1"),
not(hasKey("x-opaque-id"))
),
allOf(
hasEntry("type", "deprecation"),
hasEntry("log.level", "DEPRECATION"),
hasEntry("log.logger", "deprecation.test"),
hasEntry("cluster.name", "elasticsearch"),
hasEntry("node.name", "sample-name"),
hasEntry("message", "deprecated message2"),
not(hasKey("x-opaque-id"))
)
)
);
}

assertWarnings("deprecated message1", "deprecated message2");
}

public void testJsonLayout() throws IOException {
final Logger testLogger = LogManager.getLogger("test");

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

package org.elasticsearch.common.logging;

import org.apache.logging.log4j.message.ParameterizedMessage;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.SuppressLoggerChecks;

Expand All @@ -29,28 +28,21 @@
*/
public class DeprecatedMessage {
public static final String X_OPAQUE_ID_FIELD_NAME = "x-opaque-id";
private static final String ECS_VERSION = "1.6";
public static final String ECS_VERSION = "1.6";

@SuppressLoggerChecks(reason = "safely delegates to logger")
public static ESLogMessage of(String key, String xOpaqueId, String messagePattern, Object... args){
if (Strings.isNullOrEmpty(xOpaqueId)) {
return new ESLogMessage(messagePattern, args).field("key", key);
}

Object value = new Object() {
@Override
public String toString() {
return ParameterizedMessage.format(messagePattern, args);
}
};

return new ESLogMessage(messagePattern, args)
public static ESLogMessage of(String key, String xOpaqueId, String messagePattern, Object... args) {
ESLogMessage esLogMessage = new ESLogMessage(messagePattern, args)
.field("data_stream.type", "logs")
.field("data_stream.datatype", "deprecation")
.field("data_stream.namespace", "elasticsearch")
.field("ecs.version", ECS_VERSION)
.field("key", key)
.field("message", value)
.field(X_OPAQUE_ID_FIELD_NAME, xOpaqueId);
.field("key", key);

if (Strings.isNullOrEmpty(xOpaqueId)) {
return esLogMessage;
}

return esLogMessage.field(X_OPAQUE_ID_FIELD_NAME, xOpaqueId);
}
}

0 comments on commit 95c1488

Please sign in to comment.