Skip to content

Commit e02a914

Browse files
committed
Implement more metrics
1 parent 5c5a6e3 commit e02a914

15 files changed

Lines changed: 301 additions & 43 deletions

src/main/java/eu/openanalytics/containerproxy/auth/UserLogoutHandler.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import javax.servlet.http.HttpServletRequest;
2525
import javax.servlet.http.HttpServletResponse;
2626

27-
import eu.openanalytics.containerproxy.session.UserSessionLogoutEvent;
27+
import eu.openanalytics.containerproxy.event.UserLogoutEvent;
2828
import org.springframework.context.ApplicationEventPublisher;
2929
import org.springframework.security.core.Authentication;
3030
import org.springframework.security.web.authentication.logout.LogoutHandler;
@@ -38,18 +38,9 @@ public class UserLogoutHandler implements LogoutHandler {
3838
@Inject
3939
private UserService userService;
4040

41-
@Inject
42-
private ApplicationEventPublisher applicationEventPublisher;
43-
4441
@Override
4542
public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
4643
userService.logout(authentication);
47-
48-
// TODO test for anonymous users
49-
applicationEventPublisher.publishEvent(new UserSessionLogoutEvent(
50-
this,
51-
authentication.getName(),
52-
request.getSession().getId()));
5344
}
5445

5546
}

src/main/java/eu/openanalytics/containerproxy/backend/AbstractContainerBackend.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ public void initialize() throws ContainerProxyException {
129129
public void startProxy(Proxy proxy) throws ContainerProxyException {
130130
proxy.setId(UUID.randomUUID().toString());
131131
proxy.setStatus(ProxyStatus.Starting);
132-
proxy.setStartupTimestamp(System.currentTimeMillis());
133-
132+
proxy.setCreatedTimestamp(System.currentTimeMillis());
133+
134134
try {
135135
doStartProxy(proxy);
136136
} catch (Throwable t) {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* ContainerProxy
3+
*
4+
* Copyright (C) 2016-2020 Open Analytics
5+
*
6+
* ===========================================================================
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the Apache License as published by
10+
* The Apache Software Foundation, either version 2 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* Apache License for more details.
17+
*
18+
* You should have received a copy of the Apache License
19+
* along with this program. If not, see <http://www.apache.org/licenses/>
20+
*/
21+
package eu.openanalytics.containerproxy.event;
22+
23+
import org.springframework.context.ApplicationEvent;
24+
25+
public class AuthFailedEvent extends ApplicationEvent {
26+
27+
private final String userId;
28+
private final String sessionId;
29+
30+
public AuthFailedEvent(Object source, String userId, String sessionId) {
31+
super(source);
32+
this.userId = userId;
33+
this.sessionId = sessionId;
34+
}
35+
36+
public String getSessionId() {
37+
return sessionId;
38+
}
39+
40+
public String getUserId() {
41+
return userId;
42+
}
43+
44+
}
45+
46+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package eu.openanalytics.containerproxy.event;
2+
3+
import org.springframework.context.ApplicationEvent;
4+
5+
import java.time.Duration;
6+
7+
public class ProxyStartEvent extends ApplicationEvent {
8+
9+
private final String userId;
10+
private final String specId;
11+
private final Duration startupTime;
12+
13+
public ProxyStartEvent(Object source, String userId, String specId, Duration startupTime) {
14+
super(source);
15+
this.userId = userId;
16+
this.specId = specId;
17+
this.startupTime = startupTime;
18+
}
19+
20+
public String getUserId() {
21+
return userId;
22+
}
23+
24+
public String getSpecId() {
25+
return specId;
26+
}
27+
28+
public Duration getStartupTime() {
29+
return startupTime;
30+
}
31+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package eu.openanalytics.containerproxy.event;
2+
3+
import org.springframework.context.ApplicationEvent;
4+
5+
public class ProxyStartFailedEvent extends ApplicationEvent {
6+
7+
private final String userId;
8+
private final String specId;
9+
10+
public ProxyStartFailedEvent(Object source, String userId, String specId) {
11+
super(source);
12+
this.userId = userId;
13+
this.specId = specId;
14+
}
15+
16+
public String getUserId() {
17+
return userId;
18+
}
19+
20+
public String getSpecId() {
21+
return specId;
22+
}
23+
24+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package eu.openanalytics.containerproxy.event;
2+
3+
import org.springframework.context.ApplicationEvent;
4+
5+
import java.time.Duration;
6+
7+
public class ProxyStopEvent extends ApplicationEvent {
8+
9+
private final String userId;
10+
private final String specId;
11+
private final Duration usageTime;
12+
13+
public ProxyStopEvent(Object source, String userId, String specId, Duration usageTime) {
14+
super(source);
15+
this.userId = userId;
16+
this.specId = specId;
17+
this.usageTime = usageTime;
18+
}
19+
20+
public String getUserId() {
21+
return userId;
22+
}
23+
24+
public String getSpecId() {
25+
return specId;
26+
}
27+
28+
public Duration getUsageTime() {
29+
return usageTime;
30+
}
31+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* ContainerProxy
3+
*
4+
* Copyright (C) 2016-2020 Open Analytics
5+
*
6+
* ===========================================================================
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the Apache License as published by
10+
* The Apache Software Foundation, either version 2 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* Apache License for more details.
17+
*
18+
* You should have received a copy of the Apache License
19+
* along with this program. If not, see <http://www.apache.org/licenses/>
20+
*/
21+
package eu.openanalytics.containerproxy.event;
22+
23+
import org.springframework.context.ApplicationEvent;
24+
25+
public class UserLoginEvent extends ApplicationEvent {
26+
27+
private final String userId;
28+
private final String sessionId;
29+
30+
public UserLoginEvent(Object source, String userId, String sessionId) {
31+
super(source);
32+
this.userId = userId;
33+
this.sessionId = sessionId;
34+
}
35+
36+
public String getSessionId() {
37+
return sessionId;
38+
}
39+
40+
public String getUserId() {
41+
return userId;
42+
}
43+
44+
}
45+
46+

src/main/java/eu/openanalytics/containerproxy/session/UserSessionLogoutEvent.java renamed to src/main/java/eu/openanalytics/containerproxy/event/UserLogoutEvent.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@
1818
* You should have received a copy of the Apache License
1919
* along with this program. If not, see <http://www.apache.org/licenses/>
2020
*/
21-
package eu.openanalytics.containerproxy.session;
21+
package eu.openanalytics.containerproxy.event;
2222

2323
import org.springframework.context.ApplicationEvent;
2424

25-
public class UserSessionLogoutEvent extends ApplicationEvent {
25+
public class UserLogoutEvent extends ApplicationEvent {
2626

27-
private String userId;
28-
private String sessionId;
27+
private final String userId;
28+
private final String sessionId;
2929

30-
public UserSessionLogoutEvent(Object source, String userId, String sessionId) {
30+
public UserLogoutEvent(Object source, String userId, String sessionId) {
3131
super(source);
3232
this.userId = userId;
3333
this.sessionId = sessionId;

src/main/java/eu/openanalytics/containerproxy/model/runtime/Proxy.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class Proxy {
3737
private ProxyStatus status;
3838

3939
private long startupTimestamp;
40+
private long createdTimestamp;
4041
private String userId;
4142
private String namespace;
4243

@@ -80,6 +81,14 @@ public void setStartupTimestamp(long startupTimestamp) {
8081
this.startupTimestamp = startupTimestamp;
8182
}
8283

84+
public long getCreatedTimestamp() {
85+
return createdTimestamp;
86+
}
87+
88+
public void setCreatedTimestamp(long createdTimestamp) {
89+
this.createdTimestamp = createdTimestamp;
90+
}
91+
8392
public String getUserId() {
8493
return userId;
8594
}

src/main/java/eu/openanalytics/containerproxy/service/ProxyService.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.io.OutputStream;
2424
import java.net.URI;
25+
import java.time.Duration;
2526
import java.util.ArrayList;
2627
import java.util.Collections;
2728
import java.util.List;
@@ -36,8 +37,12 @@
3637
import javax.annotation.PreDestroy;
3738
import javax.inject.Inject;
3839

40+
import eu.openanalytics.containerproxy.event.ProxyStartFailedEvent;
41+
import eu.openanalytics.containerproxy.event.ProxyStartEvent;
42+
import eu.openanalytics.containerproxy.event.ProxyStopEvent;
3943
import org.apache.logging.log4j.LogManager;
4044
import org.apache.logging.log4j.Logger;
45+
import org.springframework.context.ApplicationEventPublisher;
4146
import org.springframework.security.access.AccessDeniedException;
4247
import org.springframework.stereotype.Service;
4348

@@ -92,7 +97,10 @@ public class ProxyService {
9297

9398
@Inject
9499
private LogService logService;
95-
100+
101+
@Inject
102+
private ApplicationEventPublisher applicationEventPublisher;
103+
96104
@PreDestroy
97105
public void shutdown() {
98106
try {
@@ -222,6 +230,7 @@ public Proxy startProxy(ProxySpec spec, boolean ignoreAccessControl) throws Cont
222230
backend.startProxy(proxy);
223231
} finally {
224232
if (proxy.getStatus() != ProxyStatus.Up) activeProxies.remove(proxy);
233+
applicationEventPublisher.publishEvent(new ProxyStartFailedEvent(this, proxy.getUserId(), spec.getId()));
225234
}
226235

227236
for (Entry<String, URI> target: proxy.getTargets().entrySet()) {
@@ -239,7 +248,9 @@ public Proxy startProxy(ProxySpec spec, boolean ignoreAccessControl) throws Cont
239248

240249
log.info(String.format("Proxy activated [user: %s] [spec: %s] [id: %s]", proxy.getUserId(), spec.getId(), proxy.getId()));
241250
eventService.post(EventType.ProxyStart.toString(), proxy.getUserId(), spec.getId());
242-
251+
252+
applicationEventPublisher.publishEvent(new ProxyStartEvent(this, proxy.getUserId(), spec.getId(), Duration.ofMillis(proxy.getStartupTimestamp() - proxy.getCreatedTimestamp())));
253+
243254
return proxy;
244255
}
245256

@@ -263,6 +274,11 @@ public void stopProxy(Proxy proxy, boolean async, boolean ignoreAccessControl) {
263274
logService.detach(proxy);
264275
log.info(String.format("Proxy released [user: %s] [spec: %s] [id: %s]", proxy.getUserId(), proxy.getSpec().getId(), proxy.getId()));
265276
eventService.post(EventType.ProxyStop.toString(), proxy.getUserId(), proxy.getSpec().getId());
277+
278+
applicationEventPublisher.publishEvent(new ProxyStopEvent(this, proxy.getUserId(),
279+
proxy.getSpec().getId(),
280+
Duration.ofMillis(System.currentTimeMillis() - proxy.getStartupTimestamp())));
281+
266282
} catch (Exception e){
267283
log.error("Failed to release proxy " + proxy.getId(), e);
268284
}

0 commit comments

Comments
 (0)