-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ability to store session metadata (fixes #583)
- Loading branch information
1 parent
4dd57fe
commit 484dfd3
Showing
13 changed files
with
296 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
== Saving Session Metadata | ||
|
||
IMPORTANT: This feature only becomes available when Selenoid is compiled with `metadata` build tag (missing by default). | ||
|
||
Selenoid can save session metadata to a separate JSON file. Main use case is to somehow post-process session logs, e.g. send them to map-reduce cluster. No additional configuration is needed to enable this feature. When enabled and `-log-output-dir` flag is set - Selenoid will automatically save JSON files `<log-output-dir>/<session-id>.json` with the following content: | ||
|
||
.Example metadata JSON file | ||
[source,javascript] | ||
---- | ||
{ | ||
"id": "62a4d82d-edf6-43d5-886f-895b77ff23b7", | ||
"capabilities": { | ||
"browserName": "chrome", | ||
"version": "70.0", | ||
"name": "MyCoolTest", | ||
"screenResolution": "1920x1080x24" | ||
}, | ||
"started": "2018-11-15T16:23:12.440916+03:00", | ||
"finished": "2018-11-15T16:23:12.480928+03:00" | ||
} | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package event | ||
|
||
import "github.com/aerokube/selenoid/session" | ||
|
||
var ( | ||
fileCreatedListeners []FileCreatedListener | ||
sessionStoppedListeners []SessionStoppedListener | ||
) | ||
|
||
type InitRequired interface { | ||
Init() | ||
} | ||
|
||
type Event struct { | ||
RequestId uint64 | ||
SessionId string | ||
Session *session.Session | ||
} | ||
|
||
type CreatedFile struct { | ||
Event | ||
Name string | ||
Type string | ||
} | ||
|
||
type FileCreatedListener interface { | ||
OnFileCreated(createdFile CreatedFile) | ||
} | ||
|
||
type StoppedSession struct { | ||
Event | ||
} | ||
|
||
type SessionStoppedListener interface { | ||
OnSessionStopped(stoppedSession StoppedSession) | ||
} | ||
|
||
func FileCreated(createdFile CreatedFile) { | ||
for _, l := range fileCreatedListeners { | ||
go l.OnFileCreated(createdFile) | ||
} | ||
} | ||
|
||
func InitIfNeeded(listener interface{}) { | ||
if l, ok := listener.(InitRequired); ok { | ||
l.Init() | ||
} | ||
} | ||
|
||
func AddFileCreatedListener(listener FileCreatedListener) { | ||
InitIfNeeded(listener) | ||
fileCreatedListeners = append(fileCreatedListeners, listener) | ||
} | ||
|
||
func SessionStopped(stoppedSession StoppedSession) { | ||
for _, l := range sessionStoppedListeners { | ||
go l.OnSessionStopped(stoppedSession) | ||
} | ||
} | ||
|
||
func AddSessionStoppedListener(listener SessionStoppedListener) { | ||
InitIfNeeded(listener) | ||
sessionStoppedListeners = append(sessionStoppedListeners, listener) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// +build metadata | ||
|
||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/aerokube/selenoid/event" | ||
"github.com/aerokube/selenoid/session" | ||
"io/ioutil" | ||
"log" | ||
"path/filepath" | ||
"time" | ||
) | ||
|
||
const metadataFileExtension = ".json" | ||
|
||
func init() { | ||
mp := &MetadataProcessor{} | ||
event.AddSessionStoppedListener(mp) | ||
} | ||
|
||
type MetadataProcessor struct { | ||
} | ||
|
||
func (mp *MetadataProcessor) OnSessionStopped(stoppedSession event.StoppedSession) { | ||
if logOutputDir != "" { | ||
meta := session.Metadata{ | ||
ID: stoppedSession.SessionId, | ||
Started: stoppedSession.Session.Started, | ||
Finished: time.Now(), | ||
Capabilities: stoppedSession.Session.Caps, | ||
} | ||
data, err := json.MarshalIndent(meta, "", " ") | ||
if err != nil { | ||
log.Printf("[%d] [METADATA] [%s] [Failed to marshal: %v]", stoppedSession.RequestId, stoppedSession.SessionId, err) | ||
return | ||
} | ||
filename := filepath.Join(logOutputDir, stoppedSession.SessionId+metadataFileExtension) | ||
err = ioutil.WriteFile(filename, data, 0644) | ||
if err != nil { | ||
log.Printf("[%d] [METADATA] [%s] [Failed to save to %s: %v]", stoppedSession.RequestId, stoppedSession.SessionId, filename, err) | ||
return | ||
} | ||
log.Printf("[%d] [METADATA] [%s] [%s]", stoppedSession.RequestId, stoppedSession.SessionId, filename) | ||
createdFile := event.CreatedFile{ | ||
Event: stoppedSession.Event, | ||
Name: filename, | ||
Type: "metadata", | ||
} | ||
event.FileCreated(createdFile) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.