8000 GCF: Add Stackdriver triggers sample (#1707) · cevaris/python-docs-samples@a34f991 · GitHub
[go: up one dir, main page]

Skip to content

Commit a34f991

Browse files
author
Ace Nassri
authored
GCF: Add Stackdriver triggers sample (GoogleCloudPlatform#1707)
* GCF: Add Stackdriver triggers sample Change-Id: I55fa7bdc307a23fcb12c8f63c091e787b522626a * Make sample product-agnostic Change-Id: Ie0499ac5662c7bf0eaeccceb6a789a0e81f64e0a
1 parent eac8daf commit a34f991

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

functions/log/main.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
# [START functions_log_bigquery_stackdriver]
16+
import base64
17+
import json
18+
19+
# [END functions_log_bigquery_stackdriver]
20+
1521
# [START functions_log_helloworld]
1622
import logging
1723

@@ -66,3 +72,14 @@ def get_log_entries(request):
6672

6773
return 'Done!'
6874
# [END functions_log_retrieve]
75+
76+
77+
# [START functions_log_stackdriver]
78+
def process_log_entry(data, context):
79+
data_buffer = base64.b64decode(data['data'])
80+
log_entry = json.loads(data_buffer)['protoPayload']
81+
82+
print(f"Method: {log_entry['methodName']}")
83+
print(f"Resource: {log_entry['resourceName']}")
84+
print(f"Initiator: {log_entry['authenticationInfo']['principalEmail']}")
85+
# [END functions_log_stackdriver]

functions/log/main_test.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import base64
16+
import json
1517

1618
import main
1719

@@ -21,3 +23,26 @@ def test_hello_world(capsys):
2123

2224
out, _ = capsys.readouterr()
2325
assert "Hello, stdout!" in out
26+
27+
28+
def test_process_log_entry(capsys):
29+
inner_json = {
30+
'protoPayload': {
31+
'methodName': 'method',
32+
'resourceName': 'resource',
33+
'authenticationInfo': {
34+
'principalEmail': 'me@example.com'
35+
}
36+
}
37+
}
38+
39+
data = {
40+
'data': base64.b64encode(json.dumps(inner_json).encode())
41+
}
42+
43+
main.process_log_entry(data, None)
44+
45+
out, _ = capsys.readouterr()
46+
assert 'Method: method' in out
47+
assert 'Resource: resource' in out
48+
assert 'Initiator: me@example.com' in out

0 commit comments

Comments
 (0)
0