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 12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
+ # [START functions_log_bigquery_stackdriver]
16
+ import base64
17
+ import json
18
+
19
+ # [END functions_log_bigquery_stackdriver]
20
+
15
21
# [START functions_log_helloworld]
16
22
import logging
17
23
@@ -66,3 +72,14 @@ def get_log_entries(request):
66
72
67
73
return 'Done!'
68
74
# [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 12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
+ import base64
16
+ import json
15
17
16
18
import main
17
19
@@ -21,3 +23,26 @@ def test_hello_world(capsys):
21
23
22
24
out , _ = capsys .readouterr ()
23
25
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
You can’t perform that action at this time.
0 commit comments