Skip to content

Commit 0070dcd

Browse files
committed
Making changes to required files to add in maxBillableBytes.
1 parent aa3d02d commit 0070dcd

File tree

5 files changed

+48
-1
lines changed

5 files changed

+48
-1
lines changed

pkg/bigquery/driver/conn.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ func (c *Conn) execContext(ctx context.Context, query string, args []driver.Valu
124124
q := c.client.Query(query)
125125

126126
q.QueryConfig.Labels = c.headersAsLabels()
127+
128+
if c.cfg.MaxBillableBytes > 0 {
129+
q.QueryConfig.MaxBytesBilled = c.cfg.MaxBillableBytes
130+
}
131+
127132
// q.DefaultProjectID = c.cfg.Project // allows omitting project in table reference
128133
// q.DefaultDatasetID = c.cfg.Dataset // allows omitting dataset in table reference
129134

@@ -228,6 +233,10 @@ func (c *Conn) queryContext(ctx context.Context, query string, args []driver.Val
228233

229234
q.QueryConfig.Labels = c.headersAsLabels()
230235

236+
if c.cfg.MaxBillableBytes > 0 {
237+
q.QueryConfig.MaxBytesBilled = c.cfg.MaxBillableBytes
238+
}
239+
231240
rowsIterator, err := q.Read(ctx)
232241
if err != nil {
233242
return nil, err

pkg/bigquery/settings.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func getConnectionSettings(settings types.BigQuerySettings, queryArgs *Connectio
4343
Project: settings.DefaultProject,
4444
Location: settings.ProcessingLocation,
4545
AuthenticationType: settings.AuthenticationType,
46+
MaxBillableBytes: settings.MaxBillableBytes,
4647
}
4748

4849
// We want to set the location to empty string only if query args are set

pkg/bigquery/types/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type BigQuerySettings struct {
1414
TokenUri string `json:"tokenUri"`
1515
QueryPriority string `json:"queryPriority"`
1616
ProcessingLocation string `json:"processingLocation"`
17+
MaxBillableBytes int64 `json:"maxBillableBytes,omitempty"`
1718
Updated time.Time
1819
AuthenticationType string `json:"authenticationType"`
1920
PrivateKeyPath string `json:"privateKeyPath"`
@@ -28,6 +29,7 @@ type ConnectionSettings struct {
2829
Project string
2930
Dataset string
3031
Headers map[string][]string
32+
MaxBillableBytes int64
3133
}
3234
type TableFieldSchema struct {
3335
Name string `json:"name"`

src/components/ConfigEditor.tsx

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { DataSourcePluginOptionsEditorProps, onUpdateDatasourceJsonDataOptionSelect } from '@grafana/data';
22
import { AuthConfig, GOOGLE_AUTH_TYPE_OPTIONS } from '@grafana/google-sdk';
33
import { config } from '@grafana/runtime';
4-
import { Field, SecureSocksProxySettings, Select } from '@grafana/ui';
4+
import { Field, Input, SecureSocksProxySettings, Select } from '@grafana/ui';
55
import React from 'react';
66
import { PROCESSING_LOCATIONS } from '../constants';
77
import { BigQueryOptions, BigQuerySecureJsonData } from '../types';
@@ -15,6 +15,16 @@ export const BigQueryConfigEditor: React.FC<BigQueryConfigEditorProps> = (props)
1515
const { options, onOptionsChange } = props;
1616
const { jsonData } = options;
1717

18+
const onMaxBillableBytesChange = (event: React.ChangeEvent<HTMLInputElement>) => {
19+
onOptionsChange({
20+
...options,
21+
jsonData: {
22+
...jsonData,
23+
maxBillableBytes: Number(event.target.value),
24+
},
25+
});
26+
};
27+
1828
return (
1929
<>
2030
<DataSourceDescription
@@ -59,6 +69,30 @@ export const BigQueryConfigEditor: React.FC<BigQueryConfigEditorProps> = (props)
5969
menuShouldPortal={true}
6070
/>
6171
</Field>
72+
<Field
73+
label="Max billable bytes"
74+
description={
75+
<span>
76+
Prevent queries that would process more than this amount of bytes. Read more about max billable bytes{' '}
77+
<a
78+
href="https://cloud.google.com/bigquery/docs/best-practices-costs"
79+
rel="noreferrer"
80+
className="external-link"
81+
target="_blank"
82+
>
83+
here
84+
</a>
85+
</span>
86+
}
87+
>
88+
<Input
89+
className="width-30"
90+
placeholder="Optional, example 5242880"
91+
type={'number'}
92+
value={jsonData.maxBillableBytes || ''}
93+
onChange={onMaxBillableBytesChange}
94+
/>
95+
</Field>
6296

6397
{config.secureSocksDSProxyEnabled && (
6498
<SecureSocksProxySettings options={options} onOptionsChange={onOptionsChange} />

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export interface BigQueryOptions extends DataSourceOptions {
2727
processingLocation?: string;
2828
queryPriority?: QueryPriority;
2929
enableSecureSocksProxy?: boolean;
30+
maxBillableBytes?: number;
3031
}
3132

3233
export interface BigQuerySecureJsonData extends DataSourceSecureJsonData {}

0 commit comments

Comments
 (0)