-
Notifications
You must be signed in to change notification settings - Fork 562
Expand file tree
/
Copy pathlog_collector.py
More file actions
145 lines (126 loc) · 4.62 KB
/
Copy pathlog_collector.py
File metadata and controls
145 lines (126 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# Copyright 2014 PerfKitBenchmarker Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Utilities for collecting logs."""
import datetime
from absl import flags
from perfkitbenchmarker import vm_util
GSUTIL_MV = 'mv'
GSUTIL_CP = 'cp'
GSUTIL_OPERATIONS = [GSUTIL_MV, GSUTIL_CP]
PKB_LOG_BUCKET = flags.DEFINE_string(
'pkb_log_bucket',
None,
'Name of the GCS bucket that PKB logs should route to. If this is not '
'specified, then PKB logs will remain on the VM. This bucket must exist '
'and the caller must have write permissions on the bucket for a successful '
'export.',
)
VM_LOG_BUCKET = flags.DEFINE_string(
'vm_log_bucket',
None,
'The GCS bucket to store VM logs in. If not provided, VM logs will go to '
'the calling machine only. This only applies if --capture_vm_logs is '
'set.',
)
_SAVE_LOG_TO_BUCKET_OPERATION = flags.DEFINE_enum(
'save_log_to_bucket_operation',
GSUTIL_MV,
GSUTIL_OPERATIONS,
'How to save the log to the bucket, available options are mv, cp',
)
_RELATIVE_GCS_LOG_PATH = flags.DEFINE_string(
'relative_gcs_log_path',
None,
'The relative path inside the GCS bucket where to save the log, e.g. '
'"root_dir/sub_dir", and the full file path would be '
'gs://<bucket>/<relative_gcs_log_path>',
)
def CollectPKBLogs(run_uri: str, log_local_path: str) -> None:
"""Move PKB log files over to a GCS bucket (`pkb_log_bucket` flag).
All failures in the process of log collection are suppressed to avoid causing
a run to fail unnecessarily.
Args:
run_uri: The run URI of the benchmark run.
log_local_path: Path to local log file.
"""
if PKB_LOG_BUCKET.value:
# Generate the log path to the cloud bucket based on the invocation date of
# this function.
gcs_log_path = GetLogCloudPath(PKB_LOG_BUCKET.value, f'{run_uri}-pkb.log')
# The gsutil top-level flag '-h' is not supported by 'gcloud storage cp' or 'gcloud storage mv'.
vm_util.IssueCommand(
[
'gcloud',
'storage',
_SAVE_LOG_TO_BUCKET_OPERATION.value,
'--gzip-local-all',
log_local_path,
gcs_log_path,
],
raise_on_failure=False,
raise_on_timeout=False,
)
def CollectVMLogs(run_uri: str, source_path: str) -> None:
"""Move VM log files over to a GCS bucket (`vm_log_bucket` flag).
All failures in the process of log collection are suppressed to avoid causing
a run to fail unnecessarily.
Args:
run_uri: The run URI of the benchmark run.
source_path: The path to the log file.
"""
if VM_LOG_BUCKET.value:
source_filename = source_path.split('/')[-1]
gcs_directory_path = GetLogCloudPath(VM_LOG_BUCKET.value, run_uri)
gcs_path = f'{gcs_directory_path}/{source_filename}'
# The gsutil top-level flag '-h' is not supported by 'gcloud storage mv'.
vm_util.IssueCommand(
[
'gcloud',
'storage',
'mv',
'--gzip-local-all',
source_path,
gcs_path,
],
raise_on_failure=False,
raise_on_timeout=False,
)
def GetLogCloudPath(log_bucket: str, path_suffix: str) -> str:
"""Returns the GCS path, to where the logs should be saved.
Args:
log_bucket: The GCS bucket to save the logs to.
path_suffix: The suffix to append to the GCS path.
Returns:
The GCS path to where the logs should be saved.
"""
run_date = datetime.date.today()
gcs_path_prefix = _GetGcsPathPrefix(log_bucket)
return (
f'{gcs_path_prefix}/'
+ f'{run_date.year:04d}/{run_date.month:02d}/'
+ f'{run_date.day:02d}/'
+ path_suffix
)
def _GetGcsPathPrefix(bucket: str) -> str:
"""Returns the GCS path prefix, to where the logs should be saved.
Args:
bucket: The GCS bucket to save the logs to.
Returns:
The GCS path prefix, to where the logs should be saved. If
`relative_gcs_log_path` is specified, the prefix will be
gs://<bucket>/<relative_gcs_log_path>. Otherwise, it will be gs://<bucket>.
"""
if _RELATIVE_GCS_LOG_PATH.value:
return f'gs://{bucket}/{_RELATIVE_GCS_LOG_PATH.value}'
return f'gs://{bucket}'