File tree Expand file tree Collapse file tree 2 files changed +42
-0
lines changed
Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change 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]
1622import 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]
Original file line number Diff line number Diff line change 1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15+ import base64
16+ import json
1517
1618import 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' :
'[email protected] ' 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: [email protected] ' in out
You can’t perform that action at this time.
0 commit comments