Skip to content

Commit 3516bc1

Browse files
Merge branch 'main' into address-AuthorizationTaskExecutorIT-failures
2 parents 0160813 + 3c7aab8 commit 3516bc1

35 files changed

Lines changed: 740 additions & 158 deletions

File tree

distribution/tools/server-cli/src/main/java/org/elasticsearch/server/cli/APMJvmOptions.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -141,20 +141,23 @@ class APMJvmOptions {
141141
*/
142142
static List<String> apmJvmOptions(Settings settings, @Nullable SecureSettings secrets, Path logsDir, Path tmpdir) throws UserException,
143143
IOException {
144-
boolean tracingEnabled = settings.getAsBoolean("telemetry.tracing.enabled", false);
145-
boolean metricsEnabled = settings.getAsBoolean("telemetry.metrics.enabled", false);
144+
return apmJvmOptions(settings, secrets, logsDir, tmpdir, System.getProperty("user.dir"));
145+
}
146+
147+
// for testing
148+
static List<String> apmJvmOptions(Settings settings, @Nullable SecureSettings secrets, Path logsDir, Path tmpdir, String installDir)
149+
throws UserException, IOException {
146150
boolean agentMetricsEnabled = Booleans.parseBoolean(System.getProperty(OTEL_METRICS_ENABLED_SYSTEM_PROPERTY, "false")) == false;
147-
boolean attachAgent = tracingEnabled || (metricsEnabled && agentMetricsEnabled);
148151

149-
final Path agentJar = findAgentJar();
152+
final Path agentJar = findAgentJar(installDir);
150153

151-
if (attachAgent == false || agentJar == null) {
154+
if (agentJar == null) {
152155
return List.of();
153156
}
154157

155158
final Map<String, String> propertiesMap = extractApmSettings(settings);
156159

157-
if (metricsEnabled == false || agentMetricsEnabled == false) {
160+
if (agentMetricsEnabled == false) {
158161
propertiesMap.put("metrics_interval", "0s");
159162
propertiesMap.put("disable_metrics", "*");
160163
}
@@ -324,11 +327,6 @@ private static Path writeApmProperties(Path tmpdir, Map<String, String> properti
324327
* @throws IOException if a problem occurs reading the filesystem
325328
*/
326329
@Nullable
327-
private static Path findAgentJar() throws IOException, UserException {
328-
return findAgentJar(System.getProperty("user.dir"));
329-
}
330-
331-
// package private for testing
332330
static Path findAgentJar(String installDir) throws IOException, UserException {
333331
final Path apmModule = Path.of(installDir).resolve("modules").resolve("apm");
334332

distribution/tools/server-cli/src/test/java/org/elasticsearch/server/cli/APMJvmOptionsTests.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,24 @@
1212
import org.elasticsearch.cli.UserException;
1313
import org.elasticsearch.common.settings.MockSecureSettings;
1414
import org.elasticsearch.common.settings.Settings;
15+
import org.elasticsearch.core.SuppressForbidden;
1516
import org.elasticsearch.monitor.jvm.JvmInfo;
1617
import org.elasticsearch.node.Node;
1718
import org.elasticsearch.test.ESTestCase;
1819
import org.junit.After;
1920
import org.junit.Before;
2021

2122
import java.io.IOException;
23+
import java.io.InputStream;
2224
import java.nio.file.Files;
2325
import java.nio.file.Path;
2426
import java.util.Arrays;
2527
import java.util.HashMap;
2628
import java.util.List;
2729
import java.util.Map;
30+
import java.util.Properties;
2831

32+
import static org.elasticsearch.telemetry.TelemetryProvider.OTEL_METRICS_ENABLED_SYSTEM_PROPERTY;
2933
import static org.elasticsearch.test.MapMatcher.matchesMap;
3034
import static org.hamcrest.Matchers.allOf;
3135
import static org.hamcrest.Matchers.containsInAnyOrder;
@@ -42,16 +46,20 @@ public class APMJvmOptionsTests extends ESTestCase {
4246

4347
private Path installDir;
4448
private Path agentPath;
49+
private String otelMetricsEnabled;
4550

4651
@Before
4752
public void setup() throws IOException, UserException {
53+
otelMetricsEnabled = System.getProperty(OTEL_METRICS_ENABLED_SYSTEM_PROPERTY);
4854
installDir = makeFakeAgentJar();
4955
agentPath = APMJvmOptions.findAgentJar(installDir.toAbsolutePath().toString());
56+
clearSystemProperty(OTEL_METRICS_ENABLED_SYSTEM_PROPERTY);
5057
}
5158

5259
@After
5360
public void cleanup() throws IOException {
5461
Files.delete(agentPath);
62+
restoreSystemProperty(otelMetricsEnabled, OTEL_METRICS_ENABLED_SYSTEM_PROPERTY);
5563
}
5664

5765
public void testFindJar() throws IOException {
@@ -137,6 +145,57 @@ public void testExtractSettings() throws UserException {
137145
assertThat(labels, containsInAnyOrder("deployment_name=APM_Tracing", "organization_id=_456"));
138146
}
139147

148+
public void testAPMAgentAlwaysAttached() throws Exception {
149+
Path tempDir = createTempDir();
150+
List<String> options = APMJvmOptions.apmJvmOptions(
151+
Settings.builder()
152+
.put("telemetry.tracing.enabled", false)
153+
.put("telemetry.metrics.enabled", false)
154+
.put("telemetry.agent.server_url", "https://myurl:443")
155+
.build(),
156+
null,
157+
createTempDir(),
158+
tempDir,
159+
installDir.toAbsolutePath().toString()
160+
);
161+
162+
assertFalse(options.isEmpty());
163+
164+
Properties properties = extractProperties(tempDir);
165+
assertThat(properties.getProperty("metrics_interval"), equalTo("120s"));
166+
assertNull(properties.getProperty("disable_metrics"));
167+
}
168+
169+
public void testAPMAgentMetricsDisabledWhenOtelMetricsEnabled() throws Exception {
170+
setSystemProperty(OTEL_METRICS_ENABLED_SYSTEM_PROPERTY, "true");
171+
Path tempDir = createTempDir();
172+
List<String> options = APMJvmOptions.apmJvmOptions(
173+
Settings.builder().put("telemetry.agent.server_url", "https://myurl:443").build(),
174+
null,
175+
createTempDir(),
176+
tempDir,
177+
installDir.toAbsolutePath().toString()
178+
);
179+
180+
assertFalse(options.isEmpty());
181+
182+
Properties properties = extractProperties(tempDir);
183+
assertThat(properties.getProperty("metrics_interval"), equalTo("0s"));
184+
assertThat(properties.getProperty("disable_metrics"), equalTo("*"));
185+
}
186+
187+
private static Properties extractProperties(Path tempDir) throws IOException {
188+
Properties properties = new Properties();
189+
Path configPath;
190+
try (var files = Files.list(tempDir)) {
191+
configPath = files.findFirst().orElseThrow(() -> new AssertionError("expected temp APM config file"));
192+
}
193+
try (InputStream inputStream = Files.newInputStream(configPath)) {
194+
properties.load(inputStream);
195+
}
196+
return properties;
197+
}
198+
140199
private Path makeFakeAgentJar() throws IOException {
141200
Path tempFile = createTempFile();
142201
Path apmPathDir = tempFile.getParent().resolve("modules").resolve("apm");
@@ -146,4 +205,22 @@ private Path makeFakeAgentJar() throws IOException {
146205

147206
return tempFile.getParent();
148207
}
208+
209+
@SuppressForbidden(reason = "sets system property for test scenario")
210+
private static void setSystemProperty(String key, String value) {
211+
System.setProperty(key, value);
212+
}
213+
214+
@SuppressForbidden(reason = "clears system property for test setup/teardown")
215+
private static void clearSystemProperty(String key) {
216+
System.clearProperty(key);
217+
}
218+
219+
private static void restoreSystemProperty(String value, String key) {
220+
if (value == null) {
221+
clearSystemProperty(key);
222+
} else {
223+
setSystemProperty(key, value);
224+
}
225+
}
149226
}

docs/changelog/144010.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
area: Search
2+
issues: []
3+
pr: 144010
4+
summary: Expose keep_alive in async task status
5+
type: enhancement

modules/apm/src/main/java/org/elasticsearch/telemetry/apm/internal/APMAgentSettings.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.elasticsearch.common.settings.SecureString;
1818
import org.elasticsearch.common.settings.Setting;
1919
import org.elasticsearch.common.settings.Settings;
20+
import org.elasticsearch.core.Booleans;
2021
import org.elasticsearch.core.SuppressForbidden;
2122
import org.elasticsearch.telemetry.apm.internal.tracing.APMTracer;
2223

@@ -26,6 +27,7 @@
2627

2728
import static org.elasticsearch.common.settings.Setting.Property.NodeScope;
2829
import static org.elasticsearch.common.settings.Setting.Property.OperatorDynamic;
30+
import static org.elasticsearch.telemetry.TelemetryProvider.OTEL_METRICS_ENABLED_SYSTEM_PROPERTY;
2931

3032
/**
3133
* This class is responsible for APM settings, both for Elasticsearch and the APM Java agent.
@@ -42,17 +44,17 @@ public void addClusterSettingsListeners(ClusterService clusterService, APMTeleme
4244

4345
clusterSettings.addSettingsUpdateConsumer(TELEMETRY_TRACING_ENABLED_SETTING, enabled -> {
4446
apmTracer.setEnabled(enabled);
45-
// The agent records data other than spans, e.g. JVM metrics, so we toggle this setting in order to
46-
// minimise its impact to a running Elasticsearch.
47-
boolean recording = enabled || clusterSettings.get(TELEMETRY_METRICS_ENABLED_SETTING);
48-
this.setAgentSetting("recording", Boolean.toString(recording));
47+
this.setAgentSetting(
48+
"recording",
49+
Boolean.toString(shouldRecord(enabled, clusterSettings.get(TELEMETRY_METRICS_ENABLED_SETTING)))
50+
);
4951
});
5052
clusterSettings.addSettingsUpdateConsumer(TELEMETRY_METRICS_ENABLED_SETTING, enabled -> {
5153
apmMeterService.setEnabled(enabled);
52-
// The agent records data other than spans, e.g. JVM metrics, so we toggle this setting in order to
53-
// minimise its impact to a running Elasticsearch.
54-
boolean recording = enabled || clusterSettings.get(TELEMETRY_TRACING_ENABLED_SETTING);
55-
this.setAgentSetting("recording", Boolean.toString(recording));
54+
this.setAgentSetting(
55+
"recording",
56+
Boolean.toString(shouldRecord(clusterSettings.get(TELEMETRY_TRACING_ENABLED_SETTING), enabled))
57+
);
5658
});
5759
clusterSettings.addSettingsUpdateConsumer(TELEMETRY_TRACING_NAMES_INCLUDE_SETTING, apmTracer::setIncludeNames);
5860
clusterSettings.addSettingsUpdateConsumer(TELEMETRY_TRACING_NAMES_EXCLUDE_SETTING, apmTracer::setExcludeNames);
@@ -69,11 +71,17 @@ public void initAgentSystemProperties(Settings settings) {
6971
boolean tracing = TELEMETRY_TRACING_ENABLED_SETTING.get(settings);
7072
boolean metrics = TELEMETRY_METRICS_ENABLED_SETTING.get(settings);
7173

72-
this.setAgentSetting("recording", Boolean.toString(tracing || metrics));
74+
this.setAgentSetting("recording", Boolean.toString(shouldRecord(tracing, metrics)));
7375
// Apply values from the settings in the cluster state
7476
APM_AGENT_SETTINGS.getAsMap(settings).forEach(this::setAgentSetting);
7577
}
7678

79+
// Keep the agent active only when it still has work to do: tracing or metrics when OTEL does not own them.
80+
private boolean shouldRecord(boolean tracingEnabled, boolean metricsEnabled) {
81+
return tracingEnabled
82+
|| (metricsEnabled && Booleans.parseBoolean(System.getProperty(OTEL_METRICS_ENABLED_SYSTEM_PROPERTY, "false")) == false);
83+
}
84+
7785
/**
7886
* Copies a setting to the APM agent's system properties under <code>elastic.apm</code>, either
7987
* by setting the property if {@code value} has a value, or by deleting the property if it doesn't.

modules/apm/src/test/java/org/elasticsearch/telemetry/apm/internal/APMAgentSettingsTests.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@
1212
import org.elasticsearch.cluster.service.ClusterService;
1313
import org.elasticsearch.common.settings.ClusterSettings;
1414
import org.elasticsearch.common.settings.Settings;
15+
import org.elasticsearch.core.SuppressForbidden;
1516
import org.elasticsearch.test.ESTestCase;
17+
import org.junit.After;
1618
import org.mockito.Mockito;
1719

1820
import java.util.List;
1921
import java.util.Set;
2022

23+
import static org.elasticsearch.telemetry.TelemetryProvider.OTEL_METRICS_ENABLED_SYSTEM_PROPERTY;
2124
import static org.elasticsearch.telemetry.apm.internal.APMAgentSettings.APM_AGENT_SETTINGS;
2225
import static org.elasticsearch.telemetry.apm.internal.APMAgentSettings.TELEMETRY_METRICS_ENABLED_SETTING;
2326
import static org.elasticsearch.telemetry.apm.internal.APMAgentSettings.TELEMETRY_TRACING_ENABLED_SETTING;
@@ -33,10 +36,17 @@
3336
import static org.mockito.Mockito.verify;
3437
import static org.mockito.Mockito.when;
3538

39+
@SuppressForbidden(reason = "Need to change value of system property to cover all the scenarios")
3640
public class APMAgentSettingsTests extends ESTestCase {
41+
private final String otelMetricsEnabled = System.getProperty(OTEL_METRICS_ENABLED_SYSTEM_PROPERTY);
3742
APMAgentSettings apmAgentSettings = spy(new APMAgentSettings());
3843
APMTelemetryProvider apmTelemetryProvider = mock(Mockito.RETURNS_DEEP_STUBS);
3944

45+
@After
46+
public void restoreSystemProperty() {
47+
restoreSystemProperty(otelMetricsEnabled, OTEL_METRICS_ENABLED_SYSTEM_PROPERTY);
48+
}
49+
4050
/**
4151
* Check that when the tracer is enabled, it also sets the APM agent's recording system property to true.
4252
*/
@@ -61,6 +71,8 @@ public void testEnableTracing() {
6171
}
6272

6373
public void testEnableMetrics() {
74+
System.setProperty(OTEL_METRICS_ENABLED_SYSTEM_PROPERTY, "false");
75+
6476
for (boolean tracingEnabled : List.of(true, false)) {
6577
clearInvocations(apmAgentSettings, apmTelemetryProvider.getMeterService());
6678

@@ -84,6 +96,8 @@ public void testEnableMetrics() {
8496
* Check that when the tracer is disabled, it also sets the APM agent's recording system property to false unless metrics are enabled.
8597
*/
8698
public void testDisableTracing() {
99+
System.setProperty(OTEL_METRICS_ENABLED_SYSTEM_PROPERTY, "false");
100+
87101
for (boolean metricsEnabled : List.of(true, false)) {
88102
clearInvocations(apmAgentSettings, apmTelemetryProvider.getTracer());
89103

@@ -103,6 +117,44 @@ public void testDisableTracing() {
103117
}
104118
}
105119

120+
public void testTracingEnablesRecordingWhenOTelMetricsEnabled() {
121+
System.setProperty(OTEL_METRICS_ENABLED_SYSTEM_PROPERTY, "true");
122+
123+
Settings initial = Settings.builder()
124+
.put(TELEMETRY_TRACING_ENABLED_SETTING.getKey(), false)
125+
.put(TELEMETRY_METRICS_ENABLED_SETTING.getKey(), true)
126+
.build();
127+
apmAgentSettings.initAgentSystemProperties(initial);
128+
129+
verify(apmAgentSettings).setAgentSetting("recording", "false");
130+
clearInvocations(apmAgentSettings, apmTelemetryProvider.getTracer());
131+
132+
Settings update = Settings.builder().put(initial).put(TELEMETRY_TRACING_ENABLED_SETTING.getKey(), true).build();
133+
triggerUpdateConsumer(initial, update);
134+
135+
verify(apmAgentSettings).setAgentSetting("recording", "true");
136+
verify(apmTelemetryProvider.getTracer()).setEnabled(true);
137+
}
138+
139+
public void testMetricsDisabledWhenOTelMetricsEnabled() {
140+
System.setProperty(OTEL_METRICS_ENABLED_SYSTEM_PROPERTY, "true");
141+
142+
Settings initial = Settings.builder()
143+
.put(TELEMETRY_TRACING_ENABLED_SETTING.getKey(), false)
144+
.put(TELEMETRY_METRICS_ENABLED_SETTING.getKey(), false)
145+
.build();
146+
apmAgentSettings.initAgentSystemProperties(initial);
147+
148+
verify(apmAgentSettings).setAgentSetting("recording", "false");
149+
clearInvocations(apmAgentSettings, apmTelemetryProvider.getMeterService());
150+
151+
Settings update = Settings.builder().put(initial).put(TELEMETRY_METRICS_ENABLED_SETTING.getKey(), true).build();
152+
triggerUpdateConsumer(initial, update);
153+
154+
verify(apmAgentSettings).setAgentSetting("recording", "false");
155+
verify(apmTelemetryProvider.getMeterService()).setEnabled(true);
156+
}
157+
106158
public void testDisableMetrics() {
107159
for (boolean tracingEnabled : List.of(true, false)) {
108160
clearInvocations(apmAgentSettings, apmTelemetryProvider.getMeterService());
@@ -183,4 +235,13 @@ public void testRejectUnknownSettingResemblingAnAllowedOne() {
183235
Exception exception = expectThrows(IllegalArgumentException.class, () -> APM_AGENT_SETTINGS.getAsMap(settings));
184236
assertThat(exception.getMessage(), containsString("[telemetry.agent.unknown.service_name]"));
185237
}
238+
239+
@SuppressForbidden(reason = "Uses System.setProperty")
240+
private void restoreSystemProperty(String value, String key) {
241+
if (value == null) {
242+
System.clearProperty(key);
243+
} else {
244+
System.setProperty(key, value);
245+
}
246+
}
186247
}

muted-tests.yml

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -300,27 +300,6 @@ tests:
300300
- class: org.elasticsearch.snapshots.ConcurrentSnapshotsIT
301301
method: testDeletesAreBatched
302302
issue: https://github.com/elastic/elasticsearch/issues/144034
303-
- class: org.elasticsearch.index.query.functionscore.RandomScoreFunctionBuilderTests
304-
method: testRandomScoreWithExplicitFieldWhenSeqNoDisabled
305-
issue: https://github.com/elastic/elasticsearch/issues/144156
306-
- class: org.elasticsearch.index.query.functionscore.RandomScoreFunctionBuilderTests
307-
method: testRandomScoreWithoutFieldRequiresFieldWhenSeqNoDisabled
308-
issue: https://github.com/elastic/elasticsearch/issues/144157
309-
- class: org.elasticsearch.index.query.functionscore.RandomScoreFunctionBuilderTests
310-
method: testRandomScoreWithoutSeedFallsBackToDocIdWhenSeqNoDisabled
311-
issue: https://github.com/elastic/elasticsearch/issues/144158
312-
- class: org.elasticsearch.xpack.inference.InferenceRestIT
313-
method: test {p0=inference/30_semantic_text_inference/Calculates embeddings using the default ELSER 2 endpoint}
314-
issue: https://github.com/elastic/elasticsearch/issues/144159
315-
- class: org.elasticsearch.xpack.inference.InferenceRestIT
316-
method: test {p0=inference/30_semantic_text_inference_bwc/Calculates embeddings using the default ELSER 2 endpoint}
317-
issue: https://github.com/elastic/elasticsearch/issues/144160
318-
- class: org.elasticsearch.xpack.inference.InferenceRestIT
319-
method: test {p0=inference/40_semantic_text_query/Query a field that uses the default ELSER 2 endpoint}
320-
issue: https://github.com/elastic/elasticsearch/issues/144161
321-
- class: org.elasticsearch.xpack.inference.DefaultEndPointsIT
322-
method: testInferDeploysDefaultRerank
323-
issue: https://github.com/elastic/elasticsearch/issues/144162
324303
- class: org.elasticsearch.upgrades.MappingSupplementaryCharacterRollingUpgradeIT
325304
method: testMappingWithSupplementaryCharacterFieldName {upgradedNodes=3}
326305
issue: https://github.com/elastic/elasticsearch/issues/144163

rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/search/310_sequence_numbers_disabled.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ setup:
8585
- match: { items.1.index._primary_term: 0 }
8686

8787
---
88-
"requesting seq_no_primary_term is rejected":
88+
"seq_no_primary_term returns sentinel values":
8989
- do:
90-
catch: bad_request
9190
search:
9291
index: test
9392
body:
9493
seq_no_primary_term: true
95-
- match: { error.root_cause.0.type: "illegal_argument_exception" }
96-
- match: { error.root_cause.0.reason: "Cannot request seq_no_primary_term on index [test] because [index.disable_sequence_numbers] is [true]" }
94+
- match: { hits.total.value: 1 }
95+
- match: { hits.hits.0._seq_no: -2 }
96+
- match: { hits.hits.0._primary_term: 0 }
9797

0 commit comments

Comments
 (0)