Skip to content

Commit 76ed63a

Browse files
kardiff18Jacob Ferriero
authored andcommitted
Adding Time-Series Info to GCE usage logs (#339)
* Upload of audio solution * mp3 not supported in solution * formatting * Update README.md * Update tools/ml-audio-content-moderation/README.md Co-Authored-By: Jacob Ferriero <[email protected]> * Update tools/ml-audio-content-moderation/README.md Co-Authored-By: Jacob Ferriero <[email protected]> * Update tools/ml-audio-content-moderation/README.md Co-Authored-By: Jacob Ferriero <[email protected]> * Update tools/ml-audio-content-moderation/README.md Co-Authored-By: Jacob Ferriero <[email protected]> * Update tools/ml-audio-content-moderation/README.md Co-Authored-By: Jacob Ferriero <[email protected]> * Updated fstring * Started to incorporate some changes requested from PR * Move from tool to example * pypi versioning of packages; some typehinting * type hinting * Formatting/fixing tests * fixed bugs on deploying to gcf * updated to use five buckets * updates for five buckets missing full upload * updates to UI that were requested * Update #2 to UI code * Two class descriptions * Updated docs to use Cloud Shell as an option * Updating changes for terraform * Change exception to be NotFound * Terraform edits and added license * Readded compiled angular folders * Updating README with new wording * Fixed ordering of logs to capture errrs from JSON * Removed blank line * python 3 comment * Initial upload * Notes on BQ scheduler * Updated with more documentation on new view * Added screenshot * Updating gce-usage-log image * Added in more instructions; interval view contains all historical VM info; scheduled query updated to daily instead of hourly * Added sample query to interval description * removing angular source * Update tools/gce-usage-log/README.md Co-Authored-By: Jacob Ferriero <[email protected]> * Update tools/gce-usage-log/README.md Co-Authored-By: Jacob Ferriero <[email protected]> * Update tools/gce-usage-log/gce_interval_view.sql Co-Authored-By: Jacob Ferriero <[email protected]> * Update tools/gce-usage-log/README.md Co-Authored-By: Jacob Ferriero <[email protected]> * Update tools/gce-usage-log/README.md Co-Authored-By: Jacob Ferriero <[email protected]> * Updating nits and datastudio copy ability
1 parent b4c5f30 commit 76ed63a

14 files changed

+154
-41
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Common solutions and tools developed by Google Cloud's Professional Services tea
66
## Examples
77
The examples folder contains example solutions across a variety of Google Cloud Platform products. Use these solutions as a reference for your own or extend them to fit your particular use case.
88

9+
910
* [Audio Content Profiling](examples/ml-audio-content-profiling) - A tool that builds a pipeline to scale the process of moderating audio files for inappropriate content using machine learning APIs.
1011
* [BigQuery Audit Log Dashboard](examples/bigquery-audit-log) - Solution to help audit BigQuery usage using Data Studio for visualization and a sample SQL script to query the back-end data source consisting of audit logs.
1112
* [BigQuery Billing Dashboard](examples/bigquery-billing-dashboard) - Solution to help displaying billing info using Data Studio for visualization and a sample SQL script to query the back-end billing export table in BigQuery.

examples/ml-audio-content-profiling/app/angular/README.md

Lines changed: 0 additions & 19 deletions
This file was deleted.

tools/gce-usage-log/README.md

Lines changed: 122 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ This project is designed to provide you with tools to capture an ongoing record
44

55
As your GCP organization grows, you may want to understand the business context of your overall GCE instance footprint. An accounting of your GCE resource usage can be analyzed to optimize autoscaling strategies, to aid in capacity forecasting, and to assist in your internal resource accounting. Further insights can be drawn by segmenting your fleet based on labels or network tags (to represent entities such as production environment or team).
66

7+
8+
<b>Pre-requisites: The schema from audit logs requires your VMs to include both the labels and tags fields when creating the individual resource.
9+
</b>
10+
711
## 1. Overview
812

913
This project will capture events relevant to your GCE instance usage and log them in BigQuery in way that surfaces your GCE vCPUs (cores), RAM, and attached persistent (standard and SSD) or scratch (local SSD) disks, sliceable by zone, project, network tags, labels, and whether the instance was preemptible.
@@ -20,13 +24,24 @@ The audit logs will have separate entries for the creation and deletion of an in
2024

2125
A process is run to capture your existing footprint into a table (`_initial_vm_inventory`) in the same BigQuery dataset. This is required to capture the state of running instances for which a `create` instance event has not already been logged.
2226

23-
### 1.3 BigQuery View
27+
### 1.3.1 BigQuery Base View
2428

2529
A view is created which joins the audit log and initial VM inventory tables to provide a more user-friendly view (`_gce_usage_log`), calculating cores and RAM from the machine type listed in the audit log events.
2630

2731
The resulting schema of the view looks like this:
2832

29-
![view schema](images/view-schema.png)
33+
![view schema](images/base-view-schema.png)
34+
35+
36+
### 1.3.2 BigQuery Interval View
37+
38+
An additional view can be created to also visualize point-in-time VM inventory (`_gce_usage_log_interval`). This displays
39+
the inventory on a specified time-interval, such as aggregating all VMs in hourly increments.
40+
41+
The resulting schema of this interval view looks like this:
42+
43+
![view interval schema](images/interval-view-schema.png)
44+
3045

3146
### 1.4 Component Architecture
3247

@@ -157,11 +172,11 @@ This process will run and create a new BigQuery table called _`initial_vm_invent
157172

158173
If you’ve decided not to run in the cloud shell, you may need to install `maven` yourself.
159174

160-
### 2.5 Creating the BigQuery view
175+
### 2.5 Creating the BigQuery Views
161176

162177
Now we have the data required to calculate an aggregate view of our GCE usage. Let’s create a BigQuery view to make the data more friendly.
163178

164-
#### 2.5.1 Create the BigQuery view
179+
#### 2.5.1 Create the Initial BigQuery View
165180

166181
Let’s create the view from the `gce_usage_view.sql` file in the repository.
167182

@@ -182,23 +197,124 @@ bq mk \
182197
gce_usage_log._gce_usage_log
183198
```
184199

200+
#### 2.5.2 Create a Time-Series View
201+
202+
First, enable the Data Transfer API.
203+
204+
````
205+
gcloud services enable bigquerydatatransfer.googleapis.com
206+
````
207+
208+
Next, configure in your relevant variables:
209+
````
210+
export DESTINATION_TABLE=_gce_usage_log_interval
211+
export TIME_INTERVAL_UNIT=your_interval_unit
212+
export TIME_INTERVAL_AMOUNT=your_interval_amount
213+
````
214+
where:
215+
216+
* `your_table` is the desired name for where the result will live
217+
* `your_interval_unit` is DAY, HOUR, MINUTE, SECOND or any [supported date format](https://cloud.google.com/bigquery/docs/reference/standard-sql/timestamp_functions#timestamp_trunc)
218+
* `your_interval_amount` is any integer representing the frequency of intervals to calculate.
219+
For example, if you wanted to create a time-series dataset looking at the inventory of VMs on an
220+
hourly cadence, you would choose `HOUR` for `TIME_INTERVAL_UNIT` and `1` for `TIME_INTERVAL_AMOUNT`.
221+
If you wanted to see the inventory at 5-minute increments, you would choose `MINUTE` and 5, respectively.
222+
223+
224+
Next, create a reference to the interval view:
225+
226+
```
227+
export INTERVAL_VIEW=$(cat gce_interval_view.sql | sed -e "s/_PROJECT_/$PROJECT_ID/g" -e "s/_TIME_INTERVAL_UNIT_/$TIME_INTERVAL_UNIT/g" -e "s/_TIME_INTERVAL_AMOUNT_/$TIME_INTERVAL_AMOUNT/g")
228+
```
229+
230+
Now, create the view.
231+
```bash
232+
bq query \
233+
--project $PROJECT_ID \
234+
--use_legacy_sql=false \
235+
--destination_table=gce_usage_log.$DESTINATION_TABLE \
236+
--display_name="Interval usage of GCE Usage Logs" \
237+
--replace=true \
238+
--schedule='every 24 hours' "$INTERVAL_VIEW"
239+
Note: As a default, it is configured to run every 24 hours, but you can scheduled it more/less frequently as needed.
240+
There is also no default expiration set, but this can be added if you only need historical data from a certain timeframe.
241+
242+
185243
## 3. Using the dataset
186244

245+
### 3.2.1 How to Use the Interval View
246+
187247
Now that your dataset is ready, how do you query it?
188248

249+
The interval view allows to see aggregated point-in-time statistics. To find resource usage for a specific time-frame, you can query for the aggregate usage information for the month of
250+
September by using the sample query below as an example. You can adjust the timeframe by altering the WHERE clause
251+
or choose to only select certain fields depending on what you are trying to predict.
252+
253+
254+
```sql
255+
SELECT
256+
custom_interval as hour,
257+
count(instance_id) as num_instances,
258+
SUM(cores) as total_cores,
259+
SUM(memory_mb) as total_memory_mb,
260+
SUM(pd_standard_size_gb) as total_pd_standard_size_gb,
261+
SUM(pd_ssd_size_gb) as total_pd_ssd_size_gb,
262+
SUM(local_ssd_size_gb) as total_local_ssd_size_gb
263+
264+
FROM `gce_usage_log._gce_usage_log_interval`
265+
266+
WHERE
267+
custom_interval >= "2019-09-01" AND custom_interval < "2019-10-01"
268+
269+
GROUP BY 1
270+
```
271+
272+
The results will look something like this (Note that the aggregated statistics will most likely
273+
vary for workloads as VM resources change over hour, but they do not in this example.)
274+
275+
![interval_query](images/interval-query-results.png)
276+
277+
### 3.2.2 How to Create a Time-Series Graph on the Interval View
278+
279+
If you want to see the same data in a time-series graph rather than a data table, you can do this
280+
in Data Studio. This allows you to create time-series graphs to monitor changes and spikes of inventory
281+
over time. This can be done on whichever metrics that your team would like to use for capacity planning,
282+
such as looking at the total count of instances over time, cores, memory, etc.
283+
284+
285+
1. Open up DataStudio and create a copy of [this data source](https://datastudio.google.com/datasources/c4ed9ce7-50a2-4045-8de1-859ed5aaac6f)
286+
by selecting the copy button.
287+
![copy data source](images/copy-data-source.png)
288+
2. Rename the Data Source to the name that you'd like. Click on 'Edit Connection'.
289+
3. If this is your first time using Data Studio, click 'Authorize'.
290+
4. Fill in your project name.
291+
5. Select your dataset `gce_usage_log`.
292+
6. Select `gce_usage_log_interval`, or the corresponding name if you named the view something differently.
293+
7. Click 'Reconnect' in the upper right-hand corner.
294+
8. Make a copy of the [report](https://datastudio.google.com/open/1mpyXSxvkuu3PWXf1j0rzzyhiYP0qC_jR).
295+
![copy report](images/copy-report.png)
296+
9. When prompted to choose your data source, select your newly created data source.
297+
10. Click on 'Create Report' and name yours accordingly, including any other metrics to analyze.
298+
11. View the graph
299+
![graph](images/usage-graph.png)
300+
301+
### 3.3 How to Query Base View
302+
303+
189304
To find resource usage at a point in time `t`, query the view for records that were inserted before `t`, and deleted after `t` (or not deleted yet).
190305
191306
Here we also group by project.
192307
193-
```bash
308+
```sql
194309
SELECT
195310
project_id,
196311
count(instance_id) as num_instances,
197312
SUM(cores) as total_cores,
198313
SUM(memory_mb) as total_memory_mb,
199314
SUM(pd_standard_size_gb) as total_pd_standard_size_gb,
200-
SUM(pd_ssd_size_gb) as total_pd_ssd_size_gb
315+
SUM(pd_ssd_size_gb) as total_pd_ssd_size_gb,
201316
SUM(local_ssd_size_gb) as total_local_ssd_size_gb
317+
202318
FROM `gce_usage_log._gce_usage_log`
203319
204320
WHERE inserted < '2019-08-23 08:00:00.000 UTC'
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
WITH
2+
timestamp_interval_table AS (
3+
SELECT
4+
instance_id,
5+
GENERATE_TIMESTAMP_ARRAY(TIMESTAMP_TRUNC(inserted, _TIME_INTERVAL_UNIT_),
6+
TIMESTAMP_TRUNC(IFNULL(deleted,
7+
CURRENT_TIMESTAMP()), _TIME_INTERVAL_UNIT_),
8+
INTERVAL _TIME_INTERVAL_AMOUNT_ _TIME_INTERVAL_UNIT_) AS custom_interval_array
9+
FROM
10+
`_PROJECT_.gce_usage_log._gce_usage_log`)
11+
SELECT
12+
timestamp_interval_table.instance_id,
13+
custom_interval,
14+
preemptible,
15+
project_id,
16+
zone,
17+
machine_type,
18+
cores,
19+
memory_mb,
20+
pd_standard_size_gb,
21+
pd_ssd_size_gb,
22+
tags,
23+
labels
24+
FROM
25+
timestamp_interval_table,
26+
UNNEST(custom_interval_array) AS custom_interval
27+
JOIN
28+
`_PROJECT_.gce_usage_log._gce_usage_log` usage_view
29+
ON
30+
usage_view.instance_id = timestamp_interval_table.instance_id
31+
ORDER BY custom_interval ASC

tools/gce-usage-log/gce_usage_view.sql

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,3 @@
1-
/*
2-
* Copyright 2019 Google LLC
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
16-
171
#standardSQL
182
SELECT
193
inserted,
80.1 KB
Loading
68.7 KB
Loading
Loading
-15.9 KB
Loading

0 commit comments

Comments
 (0)