Skip to content

Commit

Permalink
Add support for packages without data streams in the event doc helper (
Browse files Browse the repository at this point in the history
  • Loading branch information
jsoriano authored Jul 31, 2024
1 parent bce51fd commit 25c5064
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 6 deletions.
7 changes: 5 additions & 2 deletions internal/docs/readme.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,11 @@ func renderReadme(fileName, packageRoot, templatePath string, linksMap linkMap)

t := template.New(fileName)
t, err := t.Funcs(template.FuncMap{
"event": func(dataStreamName string) (string, error) {
return renderSampleEvent(packageRoot, dataStreamName)
"event": func(args ...string) (string, error) {
if len(args) > 0 {
return renderSampleEvent(packageRoot, args[0])
}
return renderSampleEvent(packageRoot, "")
},
"fields": func(args ...string) (string, error) {
if len(args) > 0 {
Expand Down
15 changes: 12 additions & 3 deletions internal/docs/sample_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ import (
const sampleEventFile = "sample_event.json"

func renderSampleEvent(packageRoot, dataStreamName string) (string, error) {
eventPath := filepath.Join(packageRoot, "data_stream", dataStreamName, sampleEventFile)
var eventPath string
if dataStreamName == "" {
eventPath = filepath.Join(packageRoot, sampleEventFile)
} else {
eventPath = filepath.Join(packageRoot, "data_stream", dataStreamName, sampleEventFile)
}

body, err := os.ReadFile(eventPath)
if err != nil {
Expand All @@ -42,8 +47,12 @@ func renderSampleEvent(packageRoot, dataStreamName string) (string, error) {
}

var builder strings.Builder
builder.WriteString(fmt.Sprintf("An example event for `%s` looks as following:\n\n",
stripDataStreamFolderSuffix(dataStreamName)))
if dataStreamName == "" {
builder.WriteString("An example event looks as following:\n\n")
} else {
builder.WriteString(fmt.Sprintf("An example event for `%s` looks as following:\n\n",
stripDataStreamFolderSuffix(dataStreamName)))
}
builder.WriteString("```json\n")
builder.Write(formatted)
builder.WriteString("\n```")
Expand Down
4 changes: 3 additions & 1 deletion test/packages/parallel/sql_input/_dev/build/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

Hello from the SQL input package!

{{fields}}
{{fields}}

{{event}}
80 changes: 80 additions & 0 deletions test/packages/parallel/sql_input/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -437,3 +437,83 @@ Hello from the SQL input package!
| user_agent.os.platform | Operating system platform (such centos, ubuntu, windows). | keyword |
| user_agent.os.version | Operating system version as a raw string. | keyword |
| user_agent.version | Version of the user agent. | keyword |


An example event looks as following:

```json
{
"@timestamp": "2022-11-17T12:14:12.854Z",
"agent": {
"ephemeral_id": "876869a1-dcb7-4415-b899-9556b3f85917",
"id": "445a5230-6f4c-475a-9bd4-74b2a88ff556",
"name": "docker-fleet-agent",
"type": "metricbeat",
"version": "8.5.0"
},
"data_stream": {
"dataset": "sql_input.sql_query",
"namespace": "ep",
"type": "metrics"
},
"ecs": {
"version": "8.0.0"
},
"elastic_agent": {
"id": "445a5230-6f4c-475a-9bd4-74b2a88ff556",
"snapshot": true,
"version": "8.5.0"
},
"event": {
"dataset": "sql_input.sql_query",
"duration": 1350933,
"module": "sql"
},
"host": {
"architecture": "x86_64",
"containerized": false,
"hostname": "docker-fleet-agent",
"id": "0addaca3101a43f4a52be882837fb33d",
"ip": [
"192.168.16.7"
],
"mac": [
"02-42-C0-A8-10-07"
],
"name": "docker-fleet-agent",
"os": {
"codename": "focal",
"family": "debian",
"kernel": "5.15.0-50-generic",
"name": "Ubuntu",
"platform": "ubuntu",
"type": "linux",
"version": "20.04.5 LTS (Focal Fossa)"
}
},
"metricset": {
"name": "query",
"period": 10000
},
"service": {
"address": "elastic-package-service-sql_input-1:3306",
"type": "sql"
},
"sql": {
"driver": "mysql",
"metrics": {
"numeric": {
"innodb_data_fsyncs": 7,
"innodb_data_pending_fsyncs": 0,
"innodb_data_pending_reads": 0,
"innodb_data_pending_writes": 0,
"innodb_data_read": 6754816,
"innodb_data_reads": 432,
"innodb_data_writes": 53,
"innodb_data_written": 624640
}
},
"query": "SHOW GLOBAL STATUS LIKE 'Innodb_data%';"
}
}
```

0 comments on commit 25c5064

Please sign in to comment.