-
Notifications
You must be signed in to change notification settings - Fork 31
Validate against spec #31
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
This file contains hidden or 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 @@ | ||
[flake8] | ||
exclude= | ||
tests/**, | ||
conftest.py, | ||
setup.py | ||
max-line-length=120 | ||
ignore=E731,W503,E203,BLK100,B301 |
This file contains hidden or 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,72 @@ | ||
import datetime | ||
import json | ||
import os | ||
|
||
import pytest | ||
|
||
|
||
class ValidationError(Exception): | ||
pass | ||
|
||
|
||
@pytest.fixture | ||
def spec_validator(): | ||
with open( | ||
os.path.join(os.path.dirname(__file__), "resources", "spec.json"), "r" | ||
) as fh: | ||
spec = json.load(fh) | ||
|
||
def validator(data_json): | ||
""" | ||
Throws a ValidationError if anything doesn't match the spec. | ||
|
||
Returns the original json (pass-through) | ||
""" | ||
fields = spec["fields"] | ||
data = json.loads(data_json) | ||
for k, v in fields.items(): | ||
if v.get("required"): | ||
found = False | ||
if k in data: | ||
found = True | ||
elif "." in k: | ||
# Dotted keys could be nested, like ecs.version | ||
subkeys = k.split(".") | ||
subval = data | ||
for subkey in subkeys: | ||
subval = subval.get(subkey, {}) | ||
if subval: | ||
found = True | ||
if not found: | ||
raise ValidationError("Missing required key {}".format(k)) | ||
if k in data: | ||
if v["type"] == "string" and not ( | ||
isinstance(data[k], str) or isinstance(data[k], basestring) | ||
): | ||
raise ValidationError( | ||
"Value {0} for key {1} should be string, is {2}".format( | ||
data[k], k, type(data[k]) | ||
) | ||
) | ||
if v["type"] == "datetime": | ||
try: | ||
datetime.datetime.fromisoformat(data[k].rstrip("Z")) | ||
except ValueError: | ||
raise ValidationError( | ||
"Value {0} for key {1} doesn't parse as an ISO datetime".format( | ||
data[k], k | ||
) | ||
) | ||
if v.get("index"): | ||
index = v.get("index") | ||
key = json.loads( | ||
data_json.split(",")[index].split(":")[0].strip().lstrip("{") | ||
) | ||
if key != k: | ||
raise ValidationError( | ||
"Key {0} is not at index {1}".format(k, index) | ||
) | ||
|
||
return data_json | ||
|
||
return validator |
This file contains hidden or 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,139 @@ | ||
{ | ||
"version": 1.0, | ||
"url": "https://www.elastic.co/guide/en/ecs/current/index.html", | ||
"ecs": { | ||
"version": "1.x" | ||
}, | ||
"fields": { | ||
"@timestamp": { | ||
"type": "datetime", | ||
"required": true, | ||
"index": 0, | ||
"url": "https://www.elastic.co/guide/en/ecs/current/ecs-base.html" | ||
}, | ||
"log.level": { | ||
"type": "string", | ||
"required": true, | ||
"index": 1, | ||
"top_level_field": true, | ||
"url": "https://www.elastic.co/guide/en/ecs/current/ecs-log.html", | ||
"comment": [ | ||
"This field SHOULD NOT be a nested object field but at the top level with a dot in the property name.", | ||
"This is to make the JSON logs more human-readable.", | ||
"Loggers MAY indent the log level so that the `message` field always starts at the exact same offset,", | ||
"no matter the number of characters the log level has.", | ||
"For example: `'DEBUG'` (5 chars) will not be indented, whereas ` 'WARN'` (4 chars) will be indented by one space character." | ||
] | ||
}, | ||
"message": { | ||
"type": "string", | ||
"required": true, | ||
"index": 2, | ||
"url": "https://www.elastic.co/guide/en/ecs/current/ecs-base.html" | ||
}, | ||
"ecs.version": { | ||
"type": "string", | ||
"required": true, | ||
"url": "https://www.elastic.co/guide/en/ecs/current/ecs-ecs.html" | ||
}, | ||
"labels": { | ||
"type": "object", | ||
"required": false, | ||
"url": "https://www.elastic.co/guide/en/ecs/current/ecs-base.html", | ||
"sanitization": { | ||
"key": { | ||
"replacements": [ | ||
".", | ||
"*", | ||
"\\" | ||
], | ||
"substitute": "_" | ||
} | ||
} | ||
}, | ||
"trace.id": { | ||
"type": "string", | ||
"required": false, | ||
"url": "https://www.elastic.co/guide/en/ecs/current/ecs-tracing.html", | ||
"comment": "When APM agents add this field to the context, ecs loggers should pick it up and add it to the log event." | ||
}, | ||
"transaction.id": { | ||
"type": "string", | ||
"required": false, | ||
"url": "https://www.elastic.co/guide/en/ecs/current/ecs-tracing.html", | ||
"comment": "When APM agents add this field to the context, ecs loggers should pick it up and add it to the log event." | ||
}, | ||
"service.name": { | ||
"type": "string", | ||
"required": false, | ||
"url": "https://www.elastic.co/guide/en/ecs/current/ecs-service.html", | ||
"comment": [ | ||
"Configurable by users.", | ||
"When an APM agent is active, they should auto-configure it if not already set." | ||
] | ||
}, | ||
"event.dataset": { | ||
"type": "string", | ||
"required": false, | ||
"url": "https://www.elastic.co/guide/en/ecs/current/ecs-event.html", | ||
"default": "${service.name}.log OR ${service.name}.${appender.name}", | ||
"comment": [ | ||
"Configurable by users.", | ||
"If the user manually configures the service name,", | ||
"the logging library should set `event.dataset=${service.name}.log` if not explicitly configured otherwise.", | ||
"", | ||
"When agents auto-configure the app to use an ECS logger,", | ||
"they should set `event.dataset=${service.name}.${appender.name}` if the appender name is available in the logging library.", | ||
"Otherwise, agents should also set `event.dataset=${service.name}.log`", | ||
"", | ||
"The field helps to filter for different log streams from the same pod, for example and is required for log anomaly detection." | ||
] | ||
}, | ||
"process.thread.name": { | ||
"type": "string", | ||
"required": false, | ||
"url": "https://www.elastic.co/guide/en/ecs/current/ecs-process.html" | ||
}, | ||
"log.logger": { | ||
"type": "string", | ||
"required": false, | ||
"url": "https://www.elastic.co/guide/en/ecs/current/ecs-log.html" | ||
}, | ||
"log.origin.file.line": { | ||
"type": "integer", | ||
"required": false, | ||
"url": "https://www.elastic.co/guide/en/ecs/current/ecs-log.html", | ||
"comment": "Should be opt-in as it requires the logging library to capture a stack trace for each log event." | ||
}, | ||
"log.origin.file.name": { | ||
"type": "string", | ||
"required": false, | ||
"url": "https://www.elastic.co/guide/en/ecs/current/ecs-log.html", | ||
"comment": "Should be opt-in as it requires the logging library to capture a stack trace for each log event." | ||
}, | ||
"log.origin.function": { | ||
"type": "string", | ||
"required": false, | ||
"url": "https://www.elastic.co/guide/en/ecs/current/ecs-log.html", | ||
"comment": "Should be opt-in as it requires the logging library to capture a stack trace for each log event." | ||
}, | ||
"error.type": { | ||
"type": "string", | ||
"required": false, | ||
"url": "https://www.elastic.co/guide/en/ecs/current/ecs-error.html", | ||
"comment": "The exception type or class, such as `java.lang.IllegalArgumentException`." | ||
}, | ||
"error.message": { | ||
"type": "string", | ||
"required": false, | ||
"url": "https://www.elastic.co/guide/en/ecs/current/ecs-error.html", | ||
"comment": "The message of the exception." | ||
}, | ||
"error.stack_trace": { | ||
"type": "string", | ||
"required": false, | ||
"url": "https://www.elastic.co/guide/en/ecs/current/ecs-error.html", | ||
"comment": "The stack trace of the exception as plain text." | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.