Skip to content

Commit a8ba77e

Browse files
committed
Telemetry internals
1 parent 390d327 commit a8ba77e

File tree

6 files changed

+229
-0
lines changed

6 files changed

+229
-0
lines changed

splunklib/wire/__init__.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# coding=utf-8
2+
#
3+
# Copyright © 2011-2020 Splunk, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License"): you may
6+
# not use this file except in compliance with the License. You may obtain
7+
# a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
# License for the specific language governing permissions and limitations
15+
# under the License.
16+
17+
from ._internal import *

splunklib/wire/_internal/__init__.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# coding=utf-8
2+
#
3+
# Copyright © 2011-2020 Splunk, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License"): you may
6+
# not use this file except in compliance with the License. You may obtain
7+
# a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
# License for the specific language governing permissions and limitations
15+
# under the License.
16+
17+
from .telemetry import *
18+
from .telemetry_metric import *

splunklib/wire/_internal/json_sink.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# coding=utf-8
2+
#
3+
# Copyright © 2011-2020 Splunk, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License"): you may
6+
# not use this file except in compliance with the License. You may obtain
7+
# a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
# License for the specific language governing permissions and limitations
15+
# under the License.
16+
17+
import json
18+
19+
from splunklib.client import Entity
20+
21+
class JsonSink(Entity):
22+
"""This class represents a JSON-based write-only sink of entities in the Splunk
23+
instance, notably telemetry-metric.
24+
"""
25+
JSON_HEADER = [('Content-Type', 'application/json')]
26+
27+
def __init__(self, service, path, **kwargs):
28+
super(JsonSink, self).__init__(service, path, skip_refresh=True, **kwargs)
29+
30+
def _post(self, url, **kwargs):
31+
owner, app, sharing = self._proper_namespace()
32+
33+
return self.service.post(self.path + url, owner=owner, app=app, sharing=sharing, **kwargs)
34+
35+
def submit(self, data):
36+
"""
37+
Submits an item to the sink.
38+
39+
:param data: data to submit
40+
:type data: ``dict``
41+
42+
:return: return data
43+
:rtype: ``dict``
44+
"""
45+
46+
response = self._post('', headers=self.__class__.JSON_HEADER, body=json.dumps(data))
47+
body = json.loads(response.body.read().decode('utf-8'))
48+
49+
return response, body

splunklib/wire/_internal/telemetry.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# coding=utf-8
2+
#
3+
# Copyright © 2011-2020 Splunk, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License"): you may
6+
# not use this file except in compliance with the License. You may obtain
7+
# a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
# License for the specific language governing permissions and limitations
15+
# under the License.
16+
17+
from splunklib.wire._internal.json_sink import JsonSink
18+
19+
PATH_TELEMETRY = "telemetry-metric"
20+
21+
class Telemetry(JsonSink):
22+
def __init__(self, service, **kwargs):
23+
super(Telemetry, self).__init__(service, PATH_TELEMETRY, **kwargs)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# coding=utf-8
2+
#
3+
# Copyright © 2011-2020 Splunk, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License"): you may
6+
# not use this file except in compliance with the License. You may obtain
7+
# a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
# License for the specific language governing permissions and limitations
15+
# under the License.
16+
17+
class TelemetryMetric:
18+
def __init__(self, metric_type, component, data, opt_in_required=2):
19+
self.metric_type = metric_type
20+
self.component = component
21+
self.data = data
22+
self.opt_in_required = opt_in_required
23+
24+
@property
25+
def metric_type(self):
26+
return self._metric_type
27+
28+
@metric_type.setter
29+
def metric_type(self, value):
30+
self._metric_type = value
31+
32+
@property
33+
def component(self):
34+
return self._component
35+
36+
@component.setter
37+
def component(self, value):
38+
self._component = value
39+
40+
@property
41+
def data(self):
42+
return self._data
43+
44+
@data.setter
45+
def data(self, value):
46+
self._data = value
47+
48+
@property
49+
def opt_in_required(self):
50+
return self._opt_in_required
51+
52+
@opt_in_required.setter
53+
def opt_in_required(self, value):
54+
self._opt_in_required = value
55+
56+
def to_wire(self):
57+
return {
58+
'type': self.metric_type,
59+
'component': self.component,
60+
'data': self.data,
61+
'optInRequired': self.opt_in_required,
62+
}

tests/test_telemetry.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env python
2+
#
3+
# Copyright 2011-2014 Splunk, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License"): you may
6+
# not use this file except in compliance with the License. You may obtain
7+
# a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
# License for the specific language governing permissions and limitations
15+
# under the License.
16+
17+
from __future__ import absolute_import
18+
import pytest
19+
20+
from tests import testlib
21+
from splunklib.wire._internal.telemetry import Telemetry
22+
from splunklib.wire._internal.telemetry_metric import TelemetryMetric
23+
24+
try:
25+
import unittest
26+
except ImportError:
27+
import unittest2 as unittest
28+
29+
@pytest.mark.app
30+
class TelemetryTestCase(testlib.SDKTestCase):
31+
def setUp(self):
32+
super(TelemetryTestCase, self).setUp()
33+
34+
self.service.namespace['owner'] = 'nobody'
35+
self.service.namespace['app'] = 'sdk-app-collection'
36+
37+
self.telemetry = Telemetry(self.service)
38+
39+
def test_submit(self):
40+
# create a telemetry metric
41+
metric = TelemetryMetric(**{
42+
'metric_type': 'event',
43+
'component': 'telemetry_test_case',
44+
'data': {
45+
'testValue': 32
46+
}
47+
})
48+
49+
# call out to telemetry
50+
response, _body = self.telemetry.submit(metric.to_wire())
51+
52+
# it should return a 201
53+
self.assertEqual(response.status, 201)
54+
55+
if __name__ == "__main__":
56+
try:
57+
import unittest2 as unittest
58+
except ImportError:
59+
import unittest
60+
unittest.main()

0 commit comments

Comments
 (0)