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 @@ -5,15 +5,25 @@
*/
package org.elasticsearch.xpack.deprecation;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import static org.elasticsearch.test.hamcrest.RegexMatcher.matches;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasSize;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
Expand All @@ -27,26 +37,6 @@
import org.elasticsearch.test.rest.ESRestTestCase;
import org.hamcrest.Matcher;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.elasticsearch.test.hamcrest.RegexMatcher.matches;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.hasSize;

/**
* Tests {@code DeprecationLogger} uses the {@code ThreadContext} to add response headers.
*/
Expand Down Expand Up @@ -222,104 +212,6 @@ private void doTestDeprecationWarningsAppearInHeaders() throws IOException {
}
}

/**
* Check that deprecation messages can be recorded to an index
*/
public void testDeprecationMessagesCanBeIndexed() throws Exception {
try {
configureWriteDeprecationLogsToIndex(true);

final Request request = new Request("GET", "/_test_cluster/deprecated_settings");
final RequestOptions options = request.getOptions().toBuilder().addHeader("X-Opaque-Id", "some xid").build();
request.setOptions(options);
request.setEntity(
buildSettingsRequest(Collections.singletonList(TestDeprecationHeaderRestAction.TEST_DEPRECATED_SETTING_TRUE1), true)
);
assertOK(client().performRequest(request));

assertBusy(() -> {
Response response;
try {
response = client().performRequest(new Request("GET", "logs-deprecation-elasticsearch/_search"));
} catch (Exception e) {
// It can take a moment for the index to be created. If it doesn't exist then the client
// throws an exception. Translate it into an assertion error so that assertBusy() will
// continue trying.
throw new AssertionError(e);
}
assertOK(response);

ObjectMapper mapper = new ObjectMapper();
final JsonNode jsonNode = mapper.readTree(response.getEntity().getContent());

final int hits = jsonNode.at("/hits/total/value").intValue();
assertThat(hits, greaterThan(0));

List<Map<String, Object>> documents = new ArrayList<>();

for (int i = 0; i < hits; i++) {
final JsonNode hit = jsonNode.at("/hits/hits/" + i + "/_source");

final Map<String, Object> document = new HashMap<>();
hit.fields().forEachRemaining(entry -> document.put(entry.getKey(), entry.getValue().textValue()));

documents.add(document);
}

logger.warn(documents);
assertThat(documents, hasSize(2));

assertThat(
documents,
hasItems(
allOf(
hasKey("@timestamp"),
hasKey("cluster.name"),
hasKey("cluster.uuid"),
hasKey("component"),
hasEntry("data_stream.dataset", "deprecation.elasticsearch"),
hasEntry("data_stream.namespace", "default"),
hasEntry("data_stream.type", "logs"),
hasEntry("ecs.version", "1.6"),
hasEntry("key", "deprecated_settings"),
hasEntry("level", "DEPRECATION"),
hasEntry("message", "[deprecated_settings] usage is deprecated. use [settings] instead"),
hasKey("node.id"),
hasKey("node.name"),
hasEntry("x-opaque-id", "some xid")
),
allOf(
hasKey("@timestamp"),
hasKey("cluster.name"),
hasKey("cluster.uuid"),
hasKey("component"),
hasEntry("data_stream.dataset", "deprecation.elasticsearch"),
hasEntry("data_stream.namespace", "default"),
hasEntry("data_stream.type", "logs"),
hasEntry("ecs.version", "1.6"),
hasEntry("key", "deprecated_route"),
hasEntry("level", "DEPRECATION"),
hasEntry("message", "[/_test_cluster/deprecated_settings] exists for deprecated tests"),
hasKey("node.id"),
hasKey("node.name"),
hasEntry("x-opaque-id", "some xid")
)
)
);
});
} finally {
configureWriteDeprecationLogsToIndex(null);
client().performRequest(new Request("DELETE", "_data_stream/logs-deprecation-elasticsearch"));
}
}

private void configureWriteDeprecationLogsToIndex(Boolean value) throws IOException {
final Request request = new Request("PUT", "_cluster/settings");
request.setJsonEntity("{ \"transient\": { \"cluster.deprecation_indexing.enabled\": " + value + " } }");
final Response response = client().performRequest(request);
assertOK(response);
}

private List<String> getWarningHeaders(Header[] headers) {
List<String> warnings = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,25 @@
*/
package org.elasticsearch.xpack.deprecation;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;

import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.node.DiscoveryNodes;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.IndexScopedSettings;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.SettingsFilter;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.env.Environment;
import org.elasticsearch.env.NodeEnvironment;
import org.elasticsearch.plugins.ActionPlugin;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.repositories.RepositoriesService;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestHandler;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.watcher.ResourceWatcherService;
import org.elasticsearch.xpack.core.deprecation.DeprecationInfoAction;
import org.elasticsearch.xpack.core.deprecation.NodesDeprecationCheckAction;
import org.elasticsearch.xpack.deprecation.logging.DeprecationIndexingComponent;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;

import static org.elasticsearch.xpack.deprecation.logging.DeprecationIndexingComponent.WRITE_DEPRECATION_LOGS_TO_INDEX;

/**
* The plugin class for the Deprecation API
Expand All @@ -61,30 +46,4 @@ public List<RestHandler> getRestHandlers(Settings settings, RestController restC

return Collections.singletonList(new RestDeprecationInfoAction());
}

@Override
public Collection<Object> createComponents(
Client client,
ClusterService clusterService,
ThreadPool threadPool,
ResourceWatcherService resourceWatcherService,
ScriptService scriptService,
NamedXContentRegistry xContentRegistry,
Environment environment,
NodeEnvironment nodeEnvironment,
NamedWriteableRegistry namedWriteableRegistry,
IndexNameExpressionResolver indexNameExpressionResolver,
Supplier<RepositoriesService> repositoriesServiceSupplier
) {
DeprecationIndexingComponent component = new DeprecationIndexingComponent(client, environment.settings());

clusterService.addListener(component);

return Collections.singletonList(component);
}

@Override
public List<Setting<?>> getSettings() {
return Collections.singletonList(WRITE_DEPRECATION_LOGS_TO_INDEX);
}
}

This file was deleted.

Loading