Skip to content

[grid] Add Node session-history endpoint and write to local file for other utility to consume #15879

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: trunk
Choose a base branch
from
Open
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
66 changes: 66 additions & 0 deletions java/src/org/openqa/selenium/grid/data/SessionHistoryEntry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.grid.data;

import java.time.Instant;
import java.util.Objects;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.remote.SessionId;

public class SessionHistoryEntry {
private final SessionId sessionId;
private final Instant startTime;
private Instant stopTime;

public SessionHistoryEntry(SessionId sessionId, Instant startTime, Instant stopTime) {
this.sessionId = Require.nonNull("Session ID", sessionId);
this.startTime = Require.nonNull("Start time", startTime);
this.stopTime = stopTime; // Can be null for ongoing sessions
}

public SessionId getSessionId() {
return sessionId;
}

public Instant getStartTime() {
return startTime;
}

public Instant getStopTime() {
return stopTime;
}

public void setStopTime(Instant stopTime) {
this.stopTime = stopTime;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof SessionHistoryEntry)) return false;
SessionHistoryEntry that = (SessionHistoryEntry) o;
return Objects.equals(sessionId, that.sessionId)
&& Objects.equals(startTime, that.startTime)
&& Objects.equals(stopTime, that.stopTime);
}

@Override
public int hashCode() {
return Objects.hash(sessionId, startTime, stopTime);
}
}
40 changes: 40 additions & 0 deletions java/src/org/openqa/selenium/grid/data/SessionStartedEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.grid.data;

import java.util.function.Consumer;
import org.openqa.selenium.events.Event;
import org.openqa.selenium.events.EventListener;
import org.openqa.selenium.events.EventName;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.remote.SessionId;

public class SessionStartedEvent extends Event {

private static final EventName SESSION_STARTED = new EventName("session-started");

public SessionStartedEvent(SessionId id) {
super(SESSION_STARTED, id);
}

public static EventListener<SessionId> listener(Consumer<SessionId> handler) {
Require.nonNull("Handler", handler);

return new EventListener<>(SESSION_STARTED, SessionId.class, handler);
}
}
45 changes: 45 additions & 0 deletions java/src/org/openqa/selenium/grid/node/GetNodeSessionHistory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.grid.node;

import static org.openqa.selenium.remote.http.Contents.asJson;

import com.google.common.collect.ImmutableMap;
import java.io.UncheckedIOException;
import java.util.List;
import org.openqa.selenium.grid.data.SessionHistoryEntry;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.remote.http.HttpHandler;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;

class GetNodeSessionHistory implements HttpHandler {

private final Node node;

GetNodeSessionHistory(Node node) {
this.node = Require.nonNull("Node", node);
}

@Override
public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
List<SessionHistoryEntry> sessionHistory = node.getSessionHistory();

return new HttpResponse().setContent(asJson(ImmutableMap.of("value", sessionHistory)));
}
}
10 changes: 10 additions & 0 deletions java/src/org/openqa/selenium/grid/node/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import java.io.IOException;
import java.net.URI;
import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;
Expand All @@ -45,6 +47,7 @@
import org.openqa.selenium.grid.data.NodeId;
import org.openqa.selenium.grid.data.NodeStatus;
import org.openqa.selenium.grid.data.Session;
import org.openqa.selenium.grid.data.SessionHistoryEntry;
import org.openqa.selenium.grid.security.RequiresSecretFilter;
import org.openqa.selenium.grid.security.Secret;
import org.openqa.selenium.internal.Either;
Expand Down Expand Up @@ -189,6 +192,9 @@ protected Node(
delete("/se/grid/node/session/{sessionId}")
.to(params -> new StopNodeSession(this, sessionIdFrom(params)))
.with(spanDecorator("node.stop_session").andThen(requiresSecret)),
get("/se/grid/node/session-history")
.to(() -> new GetNodeSessionHistory(this))
.with(spanDecorator("node.get_session_history").andThen(requiresSecret)),
get("/se/grid/node/session/{sessionId}")
.to(params -> new GetNodeSession(this, sessionIdFrom(params)))
.with(spanDecorator("node.get_session").andThen(requiresSecret)),
Expand Down Expand Up @@ -268,6 +274,10 @@ public TemporaryFilesystem getDownloadsFilesystem(SessionId id) throws IOExcepti

public abstract HealthCheck getHealthCheck();

public List<SessionHistoryEntry> getSessionHistory() {
return Collections.emptyList();
}

public Duration getSessionTimeout() {
return sessionTimeout;
}
Expand Down
21 changes: 21 additions & 0 deletions java/src/org/openqa/selenium/grid/node/config/NodeFlags.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,27 @@ public class NodeFlags implements HasRoles {
@ConfigValue(section = NODE_SECTION, name = "enable-managed-downloads", example = "false")
public Boolean managedDownloadsEnabled;

@Parameter(
names = {"--status-to-file"},
description =
"Path to a local file where the Node will write its status information "
+ "in JSON format. This file will be updated periodically and can be "
+ "consumed by other services running on the same machine.")
@ConfigValue(section = NODE_SECTION, name = "status-to-file", example = "node-status.json")
public String statusFile;

@Parameter(
names = {"--session-history-to-file"},
description =
"Path to a local file where the Node will write session history information "
+ "in JSON format. This file will contain chronological records of session "
+ "start and stop events with sessionId, startTime, and stopTime.")
@ConfigValue(
section = NODE_SECTION,
name = "session-history-to-file",
example = "session-history.json")
public String sessionHistoryFile;

@Override
public Set<Role> getRoles() {
return Collections.singleton(NODE_ROLE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@ public boolean isManagedDownloadsEnabled() {
return config.getBool(NODE_SECTION, "enable-managed-downloads").orElse(Boolean.FALSE);
}

public Optional<String> getStatusFile() {
return config.get(NODE_SECTION, "status-to-file");
}

public Optional<String> getSessionHistoryFile() {
return config.get(NODE_SECTION, "session-history-to-file");
}

public String getGridSubPath() {
return normalizeSubPath(getPublicGridUri().map(URI::getPath).orElse(""));
}
Expand Down
Loading
Loading