Skip to content

Commit 23e7d53

Browse files
Metrics for cloud requests (#135796)
* working commit * Update x-pack/plugin/security/src/main/java/module-info.java Co-authored-by: Slobodan Adamović <[email protected]> * clean up --------- Co-authored-by: Slobodan Adamović <[email protected]>
1 parent c24d431 commit 23e7d53

File tree

6 files changed

+60
-2
lines changed

6 files changed

+60
-2
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/SecurityExtension.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.elasticsearch.cluster.service.ClusterService;
1313
import org.elasticsearch.common.settings.Settings;
1414
import org.elasticsearch.env.Environment;
15+
import org.elasticsearch.telemetry.TelemetryProvider;
1516
import org.elasticsearch.threadpool.ThreadPool;
1617
import org.elasticsearch.watcher.ResourceWatcherService;
1718
import org.elasticsearch.xpack.core.security.authc.AuthenticationFailureHandler;
@@ -64,6 +65,9 @@ interface SecurityComponents {
6465

6566
/** Provides the ability to access project-scoped data from the global scope **/
6667
ProjectResolver projectResolver();
68+
69+
/** Provides the ability to access the APM tracer and meter registry **/
70+
TelemetryProvider telemetryProvider();
6771
}
6872

6973
/**

x-pack/plugin/security/src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
exports org.elasticsearch.xpack.security.transport.extension to org.elasticsearch.internal.security;
8181
exports org.elasticsearch.xpack.security.transport to org.elasticsearch.internal.security;
8282
exports org.elasticsearch.xpack.security.audit to org.elasticsearch.internal.security;
83+
exports org.elasticsearch.xpack.security.metric to org.elasticsearch.internal.security;
8384

8485
provides org.elasticsearch.index.SlowLogFieldProvider with org.elasticsearch.xpack.security.slowlog.SecuritySlowLogFieldProvider;
8586

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/Security.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,8 @@ Collection<Object> createComponents(
865865
clusterService,
866866
resourceWatcherService,
867867
userRoleMapper,
868-
projectResolver
868+
projectResolver,
869+
telemetryProvider
869870
);
870871
Map<String, Realm.Factory> realmFactories = new HashMap<>(
871872
InternalRealms.getFactories(

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/metric/InstrumentedSecurityActionListener.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,28 @@ public static <R, C> ActionListener<AuthenticationResult<R>> wrapForAuthc(
4242
}), () -> metrics.recordTime(context, startTimeNano));
4343
}
4444

45+
/**
46+
* A simpler variant that re-uses the Authentication Result as the context. This can be handy in situations where the attributes that
47+
* are of interest are available only after the authentication is completed and not before.
48+
* As a natural consequence, there will be no context available at the point of recording start time and in cases of exceptional failure
49+
*/
50+
public static <R> ActionListener<AuthenticationResult<R>> wrapForAuthc(
51+
final SecurityMetrics<AuthenticationResult<R>> metrics,
52+
final ActionListener<AuthenticationResult<R>> listener
53+
) {
54+
assert metrics.type().group() == SecurityMetricGroup.AUTHC;
55+
final long startTimeNano = metrics.relativeTimeInNanos();
56+
return ActionListener.runBefore(ActionListener.wrap(result -> {
57+
if (result.isAuthenticated()) {
58+
metrics.recordSuccess(result);
59+
} else {
60+
metrics.recordFailure(result, result.getMessage());
61+
}
62+
listener.onResponse(result);
63+
}, e -> {
64+
metrics.recordFailure(null, e.getMessage());
65+
listener.onFailure(e);
66+
}), () -> metrics.recordTime(null, startTimeNano));
67+
}
68+
4569
}

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/metric/SecurityMetricType.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,25 @@ public enum SecurityMetricType {
1919
new SecurityMetricInfo("es.security.authc.api_key.time", "Time it took (in nanoseconds) to execute API key authentication.", "ns")
2020
),
2121

22+
CLOUD_AUTHC_API_KEY(
23+
SecurityMetricGroup.AUTHC,
24+
new SecurityMetricInfo(
25+
"es.security.authc.cloud_api_key.success.total",
26+
"Number of successful cloud API key authentications.",
27+
"count"
28+
),
29+
new SecurityMetricInfo(
30+
"es.security.authc.cloud_api_key.failures.total",
31+
"Number of failed cloud API key authentications.",
32+
"count"
33+
),
34+
new SecurityMetricInfo(
35+
"es.security.authc.cloud_api_key.time",
36+
"Time it took (in nanoseconds) to execute cloud API key authentication.",
37+
"ns"
38+
)
39+
),
40+
2241
AUTHC_SERVICE_ACCOUNT(
2342
SecurityMetricGroup.AUTHC,
2443
new SecurityMetricInfo(

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/ExtensionComponents.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.elasticsearch.cluster.service.ClusterService;
1313
import org.elasticsearch.common.settings.Settings;
1414
import org.elasticsearch.env.Environment;
15+
import org.elasticsearch.telemetry.TelemetryProvider;
1516
import org.elasticsearch.threadpool.ThreadPool;
1617
import org.elasticsearch.watcher.ResourceWatcherService;
1718
import org.elasticsearch.xpack.core.security.SecurityExtension;
@@ -27,21 +28,24 @@ public final class ExtensionComponents implements SecurityExtension.SecurityComp
2728
private final ResourceWatcherService resourceWatcherService;
2829
private final UserRoleMapper roleMapper;
2930
private final ProjectResolver projectResolver;
31+
private final TelemetryProvider telemetryProvider;
3032

3133
public ExtensionComponents(
3234
Environment environment,
3335
Client client,
3436
ClusterService clusterService,
3537
ResourceWatcherService resourceWatcherService,
3638
UserRoleMapper roleMapper,
37-
ProjectResolver projectResolver
39+
ProjectResolver projectResolver,
40+
TelemetryProvider telemetryProvider
3841
) {
3942
this.environment = environment;
4043
this.client = client;
4144
this.clusterService = clusterService;
4245
this.resourceWatcherService = resourceWatcherService;
4346
this.roleMapper = roleMapper;
4447
this.projectResolver = projectResolver;
48+
this.telemetryProvider = telemetryProvider;
4549
}
4650

4751
@Override
@@ -83,4 +87,9 @@ public UserRoleMapper roleMapper() {
8387
public ProjectResolver projectResolver() {
8488
return projectResolver;
8589
}
90+
91+
@Override
92+
public TelemetryProvider telemetryProvider() {
93+
return telemetryProvider;
94+
}
8695
}

0 commit comments

Comments
 (0)