Skip to content

Commit a803335

Browse files
committed
Refactor README
1 parent 565612e commit a803335

File tree

1 file changed

+48
-80
lines changed

1 file changed

+48
-80
lines changed

README.md

Lines changed: 48 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,15 @@ For more information, see the Splunk Enterprise [Installation Manual](https://do
2525

2626
### Installing the SDK
2727

28-
[uv](https://docs.astral.sh/uv/) is our tool of choice for development. Usually that means creating a project with `uv init` and installing the SDK with `uv add splunk-sdk`. When in doubt, consult `uv` docs.
29-
30-
If you prefer not using `uv`, the standard Python package installation method still works:
28+
Refer to standard Python package installation methods.
3129

3230
```sh
3331
python -m venv .venv
3432
source .venv/bin/activate
3533
python -m pip install splunk-sdk
3634
```
3735

38-
#### Create an .env file (optional)
36+
#### Optional: Create an `.env` file
3937

4038
To connect to Splunk Enterprise, many of the SDK examples and unit tests take command-line arguments that specify values for the host, port, and authentication. For convenience during development, you can store these arguments as key-value pairs in a `.env` file.
4139

@@ -80,27 +78,13 @@ service = client.connect(host=<HOST_URL>, token=<SESSION_KEY>, autologin=True)
8078
When working with custom search commands such as Custom Streaming Commands or Custom Generating Commands, we may need to add new fields to the records based on certain conditions. Structural changes like this may not be preserved.
8179
If you're having issues with field retention, make sure to use `add_field(record, fieldname, value)` method from SearchCommand to add a new field and value to the record.
8280

83-
<!-- TODO: Change this to a diff -->
84-
85-
#### Do
86-
87-
```python
81+
```diff
8882
class CustomStreamingCommand(StreamingCommand):
8983
def stream(self, records):
9084
for index, record in enumerate(records):
9185
if index % 1 == 0:
92-
self.add_field(record, "odd_record", "true")
93-
yield record
94-
```
95-
96-
#### Don't
97-
98-
```python
99-
class CustomStreamingCommand(StreamingCommand):
100-
def stream(self, records):
101-
for index, record in enumerate(records):
102-
if index % 1 == 0:
103-
record["odd_record"] = "true"
86+
- record["odd_record"] = "true"
87+
+ self.add_field(record, "odd_record", "true")
10488
yield record
10589
```
10690

@@ -109,104 +93,91 @@ class CustomStreamingCommand(StreamingCommand):
10993
- Generating Custom Search Command is used to generate events using SDK code.
11094
- Make sure to use `gen_record()` method from SearchCommand to add a new record and pass event data as comma-separated key=value pairs (mentioned in below example).
11195

112-
<!-- TODO: Change this to a diff -->
113-
114-
Do
115-
116-
```python
96+
```diff
11797
@Configuration()
11898
class GeneratorTest(GeneratingCommand):
11999
def generate(self):
120-
yield self.gen_record(_time=time.time(), one=1)
121-
yield self.gen_record(_time=time.time(), two=2)
122-
```
100+
- yield {'_time': time.time(), 'one': 1}
101+
- yield {'_time': time.time(), 'two': 2}
102+
+ yield self.gen_record(_time=time.time(), one=1)
103+
+ yield self.gen_record(_time=time.time(), two=2)
123104

124-
Don't
125-
126-
```python
127-
@Configuration()
128-
class GeneratorTest(GeneratingCommand):
129-
def generate(self):
130-
yield {'_time': time.time(), 'one': 1}
131-
yield {'_time': time.time(), 'two': 2}
132105
```
133106

134107
### Access metadata of Modular Inputs app example
135108

136109
- In `stream_events()` one can access modular input app metadata from `InputDefinition` object
137-
- See [GitHub Commit](https://github.com/splunk/splunk-app-examples/blob/master/modularinputs/python/github_commits/bin/github_commits.py) Modular Input App example for reference.
110+
- See the [Modular Input App example](https://github.com/splunk/splunk-app-examples/blob/master/modularinputs/python/github_commits/bin/github_commits.py) for reference.
138111

139112
```python
140113
def stream_events(self, inputs, ew):
141-
# [...] other code
114+
# [...]
142115

143-
# Access metadata (like server_host, server_uri, etc) of modular inputs app from InputDefinition object
144-
# Here, an InputDefinition`object data is used
116+
# Access the modular input app's metadata (like server_host, server_uri, etc) from `InputDefinition` object
145117
server_host = inputs.metadata["server_host"]
146118
server_uri = inputs.metadata["server_uri"]
147119
checkpoint_dir = inputs.metadata["checkpoint_dir"]
148120
```
149121

150-
### Access service object in Custom Search Command & Modular Input apps
122+
### Access `service` object in Custom Search Command & Modular Input apps
151123

152124
#### Custom Search Commands
153125

154-
- The service object is created from the `splunkd` URI and session key passed to the command invocation the search results info file.
155-
- Service object can be accessed using `self.service` in `generate`/`transform`/`stream`/`reduce` methods depending on the Custom Search Command.
156-
157-
##### Getting Splunk instance metadata
126+
- The `service` metadata object is created from the `splunkd` URI and session key passed to the command invocation the search results info file and is available in `MyCommand`.`generate`/`transform`/`stream`/`reduce` methods depending on the Custom Search Command.
158127

159128
```python
160-
def get_metadata(self):
161-
# [...] other code
162-
163-
# Access service object that can be used to connect Splunk Service
164-
service = self.service
165-
# Getting Splunk Service Info
166-
info = service.info
129+
from splunklib.searchcommands import StreamingCommand
130+
131+
class MyCommand(StreamingCommand):
132+
def get_metadata(self):
133+
# Access instance metadata
134+
service = self.service
135+
# Access Splunk service info
136+
info = service.info
137+
# [...]
167138
```
168139

169140
#### Modular Inputs app
170141

171-
- The service object is created from the `splunkd` URI and session key passed to the command invocation on the modular input stream respectively.
172-
- It is available as soon as the `Script.stream_events` method is called.
173-
174-
```python
175-
def stream_events(self, inputs, ew):
176-
# other code
177-
178-
# access service object that can be used to connect Splunk Service
179-
service = self.service
180-
# to get Splunk Service Info
181-
info = service.info
182-
```
142+
- The `service` metadata object is created from the `splunkd` URI and session key passed to the command invocation on the modular input stream respectively, and is available as soon as the `MyScript.stream_events` method is called.
183143

184-
### Running the test suite
185-
186-
This repo contains a collection of unit and integration tests.
144+
```python
145+
from splunklib.modularinput import Script
146+
147+
class MyScript(Script):
148+
def stream_events(self, inputs, ew):
149+
# Access instance metadata
150+
service = self.service
151+
# Access Splunk service info
152+
info = service.info
153+
# [...]
154+
```
187155

188-
#### Unit tests
156+
### Testing
189157

190-
To run both unit and integration tests:
158+
This repo contains a collection of both unit and integration tests. The latter need `docker`/`podman` to work.
191159

192160
```sh
161+
# Run the entire test suite:
162+
make test
163+
# Run only the unit tests:
193164
make test-unit
194165
```
195166

196167
#### Integration tests
197168

198169
> NOTE: Before running the integration tests, make sure the instance of Splunk you are testing against doesn't have new events being dumped continuously into it. Several of the tests rely on a stable event count. It's best to test against a clean install of Splunk but if you can't, you should at least disable the \*NIX and Windows apps.
199170
200-
Do not run the test suite against a production instance of Splunk! It will run just fine with the free Splunk license.
201-
202-
##### Prerequisites
203-
204-
- `docker`/`podman`
205-
206171
```sh
207-
SPLUNK_VERSION=latest && make docker-start
172+
# Make sure a local Splunk instance is running
173+
SPLUNK_VERSION=latest make docker-start
174+
175+
# Run the integration tests:
176+
make test-integration
208177
```
209178

179+
> Do not run the test suite against a production instance of Splunk! It will run just fine with the free Splunk license.
180+
210181
### Optional: Set up logging for splunklib
211182

212183
The default level is WARNING, which means that only events of this level and above will be visible
@@ -228,9 +199,6 @@ The [CHANGELOG](CHANGELOG.md) contains a description of changes for each version
228199

229200
### Branches
230201

231-
The `master` branch represents a stable and released version of the SDK.
232-
`develop` is where development between releases is happening.
233-
234202
To learn more about our branching model, see [Branching Model](https://github.com/splunk/splunk-sdk-python/wiki/Branching-Model) on GitHub.
235203

236204
## Documentation and resources

0 commit comments

Comments
 (0)