Skip to content

Commit a0ee6c8

Browse files
authored
Add telemetry for flattened fields. (#48972) (#49125)
Currently we just record the number of flattened fields defined in the mappings.
1 parent 2ac38fd commit a0ee6c8

File tree

4 files changed

+158
-3
lines changed

4 files changed

+158
-3
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/flattened/FlattenedFeatureSetUsage.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,57 @@
66

77
package org.elasticsearch.xpack.core.flattened;
88

9+
import org.elasticsearch.Version;
910
import org.elasticsearch.common.io.stream.StreamInput;
11+
import org.elasticsearch.common.io.stream.StreamOutput;
12+
import org.elasticsearch.common.xcontent.XContentBuilder;
1013
import org.elasticsearch.xpack.core.XPackFeatureSet;
1114
import org.elasticsearch.xpack.core.XPackField;
1215

1316
import java.io.IOException;
17+
import java.util.Objects;
1418

1519
public class FlattenedFeatureSetUsage extends XPackFeatureSet.Usage {
20+
private final int fieldCount;
1621

1722
public FlattenedFeatureSetUsage(StreamInput input) throws IOException {
1823
super(input);
24+
this.fieldCount = input.getVersion().onOrAfter(Version.V_7_6_0) ? input.readInt() : 0;
1925
}
2026

21-
public FlattenedFeatureSetUsage(boolean available, boolean enabled) {
27+
public FlattenedFeatureSetUsage(boolean available, boolean enabled, int fieldCount) {
2228
super(XPackField.FLATTENED, available, enabled);
29+
this.fieldCount = fieldCount;
30+
}
31+
32+
int fieldCount() {
33+
return fieldCount;
34+
}
35+
36+
@Override
37+
public void writeTo(StreamOutput out) throws IOException {
38+
super.writeTo(out);
39+
if (out.getVersion().onOrAfter(Version.V_7_6_0)) {
40+
out.writeInt(fieldCount);
41+
}
42+
}
43+
44+
@Override
45+
protected void innerXContent(XContentBuilder builder, Params params) throws IOException {
46+
super.innerXContent(builder, params);
47+
builder.field("field_count", fieldCount);
48+
}
49+
50+
@Override
51+
public boolean equals(Object o) {
52+
if (this == o) return true;
53+
if (o == null || getClass() != o.getClass()) return false;
54+
FlattenedFeatureSetUsage that = (FlattenedFeatureSetUsage) o;
55+
return available == that.available && enabled == that.enabled && fieldCount == that.fieldCount;
56+
}
57+
58+
@Override
59+
public int hashCode() {
60+
return Objects.hash(available, enabled, fieldCount);
2361
}
2462
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
package org.elasticsearch.xpack.core.flattened;
7+
8+
import org.elasticsearch.common.io.stream.Writeable;
9+
import org.elasticsearch.test.AbstractWireSerializingTestCase;
10+
11+
import java.io.IOException;
12+
13+
public class FlattenedFeatureSetUsageTests extends AbstractWireSerializingTestCase<FlattenedFeatureSetUsage> {
14+
15+
@Override
16+
protected FlattenedFeatureSetUsage createTestInstance() {
17+
return new FlattenedFeatureSetUsage(randomBoolean(), randomBoolean(), randomIntBetween(0, 1000));
18+
}
19+
20+
@Override
21+
protected FlattenedFeatureSetUsage mutateInstance(FlattenedFeatureSetUsage instance) throws IOException {
22+
23+
boolean available = instance.available();
24+
boolean enabled = instance.enabled();
25+
int fieldCount = instance.fieldCount();
26+
27+
switch (between(0, 2)) {
28+
case 0:
29+
available = !available;
30+
break;
31+
case 1:
32+
enabled = !enabled;
33+
break;
34+
case 2:
35+
fieldCount = randomValueOtherThan(instance.fieldCount(), () -> randomIntBetween(0, 1000));
36+
break;
37+
}
38+
39+
return new FlattenedFeatureSetUsage(available, enabled, fieldCount);
40+
}
41+
42+
@Override
43+
protected Writeable.Reader<FlattenedFeatureSetUsage> instanceReader() {
44+
return FlattenedFeatureSetUsage::new;
45+
}
46+
47+
}

x-pack/plugin/mapper-flattened/src/main/java/org/elasticsearch/xpack/flattened/FlattenedFeatureSet.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,31 @@
66
package org.elasticsearch.xpack.flattened;
77

88
import org.elasticsearch.action.ActionListener;
9+
import org.elasticsearch.cluster.metadata.IndexMetaData;
10+
import org.elasticsearch.cluster.metadata.MappingMetaData;
11+
import org.elasticsearch.cluster.service.ClusterService;
912
import org.elasticsearch.common.inject.Inject;
1013
import org.elasticsearch.common.settings.Settings;
1114
import org.elasticsearch.license.XPackLicenseState;
1215
import org.elasticsearch.xpack.core.XPackFeatureSet;
1316
import org.elasticsearch.xpack.core.XPackField;
1417
import org.elasticsearch.xpack.core.XPackSettings;
1518
import org.elasticsearch.xpack.core.flattened.FlattenedFeatureSetUsage;
19+
import org.elasticsearch.xpack.flattened.mapper.FlatObjectFieldMapper;
1620

1721
import java.util.Map;
1822

1923
public class FlattenedFeatureSet implements XPackFeatureSet {
2024

2125
private final boolean enabled;
2226
private final XPackLicenseState licenseState;
27+
private final ClusterService clusterService;
2328

2429
@Inject
25-
public FlattenedFeatureSet(Settings settings, XPackLicenseState licenseState) {
30+
public FlattenedFeatureSet(Settings settings, XPackLicenseState licenseState, ClusterService clusterService) {
2631
this.enabled = XPackSettings.FLATTENED_ENABLED.get(settings);
2732
this.licenseState = licenseState;
33+
this.clusterService = clusterService;
2834
}
2935

3036
@Override
@@ -49,6 +55,29 @@ public Map<String, Object> nativeCodeInfo() {
4955

5056
@Override
5157
public void usage(ActionListener<Usage> listener) {
52-
listener.onResponse(new FlattenedFeatureSetUsage(available(), enabled()));
58+
int fieldCount = 0;
59+
if (available() && enabled() && clusterService.state() != null) {
60+
for (IndexMetaData indexMetaData : clusterService.state().metaData()) {
61+
MappingMetaData mappingMetaData = indexMetaData.mapping();
62+
63+
if (mappingMetaData != null) {
64+
Map<String, Object> mappings = mappingMetaData.getSourceAsMap();
65+
66+
if (mappings.containsKey("properties")) {
67+
@SuppressWarnings("unchecked")
68+
Map<String, Map<String, Object>> fieldMappings = (Map<String, Map<String, Object>>) mappings.get("properties");
69+
70+
for (Map<String, Object> fieldMapping : fieldMappings.values()) {
71+
String fieldType = (String) fieldMapping.get("type");
72+
if (fieldType != null && fieldType.equals(FlatObjectFieldMapper.CONTENT_TYPE)) {
73+
fieldCount++;
74+
}
75+
}
76+
}
77+
}
78+
}
79+
}
80+
81+
listener.onResponse(new FlattenedFeatureSetUsage(available(), enabled(), fieldCount));
5382
}
5483
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
setup:
2+
- skip:
3+
version: " - 7.99.99"
4+
reason: "telemetry for flattened fields was added in 8.0"
5+
6+
---
7+
"Usage stats for flattened fields":
8+
- do:
9+
xpack.usage: {}
10+
11+
- match: { flattened.available: true }
12+
- match: { flattened.enabled: true }
13+
- match: { flattened.field_count: 0 }
14+
15+
- do:
16+
indices.create:
17+
index: test-index1
18+
body:
19+
mappings:
20+
properties:
21+
flattened_1:
22+
type: flattened
23+
24+
- do:
25+
indices.create:
26+
index: test-index2
27+
body:
28+
mappings:
29+
properties:
30+
flattened_2:
31+
type: flattened
32+
flattened_3:
33+
type: flattened
34+
ignore_above: 10
35+
36+
- do:
37+
xpack.usage: {}
38+
39+
- match: { flattened.available: true }
40+
- match: { flattened.enabled: true }
41+
- match: { flattened.field_count: 3 }

0 commit comments

Comments
 (0)