-
Notifications
You must be signed in to change notification settings - Fork 459
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
websocket: new generic integration #9926
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
32599cc
New Generic Integration
muskan-agarwal26 76ca939
Updated changelog entry.
muskan-agarwal26 6c5b2b0
Resolved comments
muskan-agarwal26 11612ac
Resolved comments
muskan-agarwal26 d9434af
Fixed the system test as suggested
muskan-agarwal26 77c9164
updating port to 443
ShourieG e64bbb5
resolved merge conflict and merged with upstream
ShourieG b16068b
downgraded image version and go version to 1.20 from 1.21 for mock se…
ShourieG 69fddcd
re-structured the mock service to upgrade connection earlier
ShourieG 38628e5
added health check to mock-service container
ShourieG File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Validating CODEOWNERS rules …
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,4 @@ | ||
dependencies: | ||
ecs: | ||
reference: [email protected] | ||
import_mappings: true |
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,27 @@ | ||
# Custom WebSocket Input | ||
|
||
The WebSocket input integration enables ingestion of real-time data from WebSocket servers. WebSockets provide a full-duplex communication channel over a single, long-lived connection, which makes it suitable for scenarios where low latency data transmission is required. | ||
|
||
This input type connects to a WebSocket URL, listens for messages sent by the server, and processes these messages as they arrive. The data is then published to Elasticsearch, making it searchable and analyzable in near real-time. | ||
|
||
## Configuration | ||
|
||
The full documentation for configuring the WebSocket input can be found [here](https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-input-websocket.html). | ||
|
||
To configure the WebSocket input, specify the connection URL and other optional parameters such as headers for authentication or protocol versions. Advanced options for connection handling, such as timeouts and subprotocols, can be configured in the "Advanced options" section. | ||
|
||
### Example Configuration | ||
|
||
Here is a basic example of how to configure the WebSocket input: | ||
|
||
![Configuration Page](../img/websocket_configuration.png) | ||
|
||
This configuration establishes a WebSocket connection to ws://localhost:443/v1/stream and uses basic authentication. | ||
|
||
## Data Processing | ||
|
||
The WebSocket input will consume messages from the server as they are transmitted. These messages are expected to be in a format that Filebeat can process, such as JSON. If the message format is different, you may need to define a processor to parse and structure the data before it is sent to Elasticsearch. | ||
|
||
## Connection Management | ||
|
||
The WebSocket input manages the connection to the WebSocket server, including automatically reconnecting if the connection is lost. The input does not maintain any state between restarts, so if the server sends historical data, it will be re-ingested upon reconnection. |
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,15 @@ | ||
version: "2.3" | ||
services: | ||
websocket-mock-service: | ||
image: golang:1.21-alpine | ||
hostname: websocket | ||
working_dir: /app | ||
volumes: | ||
- ./websocket-mock-service:/app | ||
ports: | ||
- "3000:3000" | ||
healthcheck: | ||
test: "wget --no-verbose --tries=1 --spider http://localhost:3000/health || exit 1" | ||
interval: 10s | ||
timeout: 5s | ||
command: ["go", "run", "main.go"] |
7 changes: 7 additions & 0 deletions
7
packages/websocket/_dev/deploy/docker/websocket-mock-service/go.mod
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,7 @@ | ||
module websocket-mock-service | ||
|
||
go 1.21.3 | ||
|
||
require github.com/gorilla/websocket v1.5.1 | ||
|
||
require golang.org/x/net v0.17.0 // indirect |
4 changes: 4 additions & 0 deletions
4
packages/websocket/_dev/deploy/docker/websocket-mock-service/go.sum
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,4 @@ | ||
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY= | ||
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY= | ||
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= | ||
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= |
81 changes: 81 additions & 0 deletions
81
packages/websocket/_dev/deploy/docker/websocket-mock-service/main.go
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please reverse the order of function declarations. The convention in Go code is caller before callee. |
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,81 @@ | ||||||||||||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||||||||||||
// or more contributor license agreements. Licensed under the Elastic License; | ||||||||||||
// you may not use this file except in compliance with the Elastic License. | ||||||||||||
|
||||||||||||
package main | ||||||||||||
ShourieG marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
|
||||||||||||
import ( | ||||||||||||
"log" | ||||||||||||
"net/http" | ||||||||||||
|
||||||||||||
"github.com/gorilla/websocket" | ||||||||||||
) | ||||||||||||
|
||||||||||||
func main() { | ||||||||||||
http.HandleFunc("/", handleWebSocket) | ||||||||||||
log.Fatal(http.ListenAndServe(":3000", nil)) | ||||||||||||
} | ||||||||||||
|
||||||||||||
func handleWebSocket(w http.ResponseWriter, r *http.Request) { | ||||||||||||
|
||||||||||||
ShourieG marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
if r.URL.Path == "/health" { | ||||||||||||
return | ||||||||||||
} | ||||||||||||
|
||||||||||||
if r.URL.Path == "/testbasicauth" { | ||||||||||||
// Check if the 'Authorization' header is set for basic authentication | ||||||||||||
authHeader := r.Header.Get("Authorization") | ||||||||||||
if authHeader != "Basic dGVzdDp0ZXN0" { | ||||||||||||
// If the header is incorrect, return an authentication error message | ||||||||||||
w.WriteHeader(http.StatusUnauthorized) | ||||||||||||
w.Write([]byte("Error: Authentication failed.")) | ||||||||||||
return | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
upgrader := websocket.Upgrader{ | ||||||||||||
CheckOrigin: func(r *http.Request) bool { return true }, | ||||||||||||
} | ||||||||||||
conn, err := upgrader.Upgrade(w, r, nil) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
if err != nil { | ||||||||||||
log.Println(err) | ||||||||||||
return | ||||||||||||
} | ||||||||||||
defer conn.Close() | ||||||||||||
|
||||||||||||
var responseMessage []map[string]string | ||||||||||||
|
||||||||||||
if r.URL.Path == "/testbasicauth" { | ||||||||||||
// Check if the 'Authorization' header is set for basic authentication | ||||||||||||
authHeader := r.Header.Get("Authorization") | ||||||||||||
if authHeader == "Basic dGVzdDp0ZXN0" { | ||||||||||||
// If the header is correct, return a success message | ||||||||||||
responseMessage = []map[string]string{ | ||||||||||||
{ | ||||||||||||
"message": "You are now authenticated to the WebSocket server.", | ||||||||||||
}, | ||||||||||||
} | ||||||||||||
} | ||||||||||||
} else if r.URL.Path == "/test" { | ||||||||||||
// Return a success message | ||||||||||||
responseMessage = []map[string]string{ | ||||||||||||
{ | ||||||||||||
"ts": "2024-01-01T01:00:00.000000-00:00", | ||||||||||||
"data": "testdata1", | ||||||||||||
"id": "test1234567891", | ||||||||||||
}, | ||||||||||||
{ | ||||||||||||
"ts": "2024-01-01T02:00:00.000000-00:00", | ||||||||||||
"data": "testdata2", | ||||||||||||
"id": "test1234567890", | ||||||||||||
}, | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
// Send a message to the client upon successful WebSocket connection | ||||||||||||
err = conn.WriteJSON(responseMessage) | ||||||||||||
if err != nil { | ||||||||||||
log.Println("write:", err) | ||||||||||||
return | ||||||||||||
} | ||||||||||||
} |
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,9 @@ | ||
input: websocket | ||
service: websocket-mock-service | ||
vars: | ||
url: ws://{{Hostname}}:{{Port}}/testbasicauth | ||
basic_token: "dGVzdDp0ZXN0" | ||
program: | | ||
bytes(state.response).decode_json().as(body,{ | ||
"events": body, | ||
}) |
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,13 @@ | ||
input: websocket | ||
service: websocket-mock-service | ||
vars: | ||
url: ws://{{Hostname}}:{{Port}}/test | ||
program: | | ||
bytes(state.response).decode_json().as(body,{ | ||
"events": body.map(e, { | ||
"message": e.encode_json(), | ||
}), | ||
"cursor": { | ||
"max_ts": body.map(e, e.ts).max() | ||
} | ||
}) |
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,60 @@ | ||
data_stream: | ||
dataset: {{data_stream.dataset}} | ||
{{#if ssl}} | ||
resource.ssl: {{ssl}} | ||
{{/if}} | ||
url: {{url}} | ||
|
||
program: {{escape_string program}} | ||
|
||
{{#if state}} | ||
state: | ||
{{state}} | ||
{{/if}} | ||
redact.delete: {{delete_redacted_fields}} | ||
{{#if redact_fields}} | ||
redact.fields: | ||
{{#each redact_fields as |field|}} | ||
- {{field}} | ||
{{/each}} | ||
{{/if}} | ||
|
||
{{#if regexp}} | ||
regexp: | ||
{{regexp}} | ||
{{/if}} | ||
|
||
{{#if basic_token}} | ||
auth.basic_token: {{basic_token}} | ||
{{else if bearer_token}} | ||
auth.bearer_token: {{bearer_token}} | ||
{{/if}} | ||
|
||
{{#unless basic_token}} | ||
{{#unless bearer_token}} | ||
{{#if header_key}} | ||
auth.custom.header: {{header_key}} | ||
{{/if}} | ||
{{#if header_value}} | ||
auth.custom.value: {{header_value}} | ||
{{/if}} | ||
{{/unless}} | ||
{{/unless}} | ||
|
||
{{#if pipeline}} | ||
pipeline: {{pipeline}} | ||
{{/if}} | ||
|
||
{{#if tags}} | ||
tags: | ||
{{#each tags as |tag|}} | ||
- {{tag}} | ||
{{/each}} | ||
{{/if}} | ||
{{#contains "forwarded" tags}} | ||
publisher_pipeline.disable_host: true | ||
{{/contains}} | ||
{{#if processors}} | ||
processors: | ||
{{processors}} | ||
{{/if}} |
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,6 @@ | ||
# newer versions go on top | ||
- version: "0.1.0" | ||
changes: | ||
- description: Initial Implementation. | ||
type: enhancement | ||
link: https://github.com/elastic/integrations/pull/9926 |
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,27 @@ | ||
# Custom WebSocket Input | ||
|
||
The WebSocket input integration enables ingestion of real-time data from WebSocket servers. WebSockets provide a full-duplex communication channel over a single, long-lived connection, which makes it suitable for scenarios where low latency data transmission is required. | ||
|
||
This input type connects to a WebSocket URL, listens for messages sent by the server, and processes these messages as they arrive. The data is then published to Elasticsearch, making it searchable and analyzable in near real-time. | ||
|
||
## Configuration | ||
|
||
The full documentation for configuring the WebSocket input can be found [here](https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-input-websocket.html). | ||
|
||
To configure the WebSocket input, specify the connection URL and other optional parameters such as headers for authentication or protocol versions. Advanced options for connection handling, such as timeouts and subprotocols, can be configured in the "Advanced options" section. | ||
|
||
### Example Configuration | ||
|
||
Here is a basic example of how to configure the WebSocket input: | ||
|
||
![Configuration Page](../img/websocket_configuration.png) | ||
|
||
This configuration establishes a WebSocket connection to ws://localhost:443/v1/stream and uses basic authentication. | ||
|
||
## Data Processing | ||
|
||
The WebSocket input will consume messages from the server as they are transmitted. These messages are expected to be in a format that Filebeat can process, such as JSON. If the message format is different, you may need to define a processor to parse and structure the data before it is sent to Elasticsearch. | ||
|
||
## Connection Management | ||
|
||
The WebSocket input manages the connection to the WebSocket server, including automatically reconnecting if the connection is lost. The input does not maintain any state between restarts, so if the server sends historical data, it will be re-ingested upon reconnection. |
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,16 @@ | ||
- name: data_stream.type | ||
type: constant_keyword | ||
description: Data stream type. | ||
- name: data_stream.dataset | ||
type: constant_keyword | ||
description: Data stream dataset. | ||
- name: data_stream.namespace | ||
type: constant_keyword | ||
description: Data stream namespace. | ||
- name: event.module | ||
type: constant_keyword | ||
description: Event module. | ||
value: websocket | ||
- name: '@timestamp' | ||
type: date | ||
description: Event timestamp. |
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,9 @@ | ||
- name: input.type | ||
type: keyword | ||
description: Type of filebeat input. | ||
- name: log.offset | ||
type: long | ||
description: Log offset. | ||
- name: tags | ||
type: keyword | ||
description: User defined tags. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please
go fmt
this code.