gcp

package module
v0.3.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 2, 2024 License: Apache-2.0 Imports: 21 Imported by: 0

README

xk6-gcp

This is a k6 extension using the xk6 system.

Thanks to deejiw for the repo you've made!

Build

To build a k6 binary with this extension, first ensure you have the prerequisites:

Then:

  1. Install xk6:
go install go.k6.io/xk6/cmd/xk6@latest
  1. Build the binary:
xk6 build --with github.com/rmscoal/xk6-gcp@latest

If you're developing, you can do

xk6 build --with github.com/rmscoal/xk6-gcp=.

Example

import { Gcp } from 'k6/x/gcp';

const jsonKey = JSON.parse(open('credentials.json'))

const gcp = new Gcp({
  key: jsonKey
  scope: ['https://www.googleapis.com/auth/cloud-platform'] // Default value
})
export default function() {
  const accessToken = gcp.getOAuth2AccessToken()
  console.log(accessToken['AccessToken'])

  const query = `fetch k8s_container
| metric 'kubernetes.io/container/cpu/limit_utilization'
| filter (resource.cluster_name == 'CLUSTER_NAME' &&
          resource.namespace_name == 'NAMESPACE_NAME' &&
          resource.pod_name =~ 'POD_NAME')
| group_by 1m, [value_limit_utilization_max: max(value.limit_utilization)]
| {
    top 2 | value [is_default_value: false()]
  ;
    ident
  }
| outer_join true(), _
| filter is_default_value
| value drop [is_default_value]
| every 1m
| condition val(0) > 0.73 '1'
`

  const result = gcp.queryTimeSeries('my-project-id', query)
  console.log(result)

}

Command

k6 run script.js

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Gcp

type Gcp struct {
	// contains filtered or unexported fields
}

func (*Gcp) GetOAuth2AccessToken

func (g *Gcp) GetOAuth2AccessToken(scope []string) (*oauth2.Token, error)

This function is a method of the `Gcp` struct and is used to obtain an OAuth2 access token for a given set of scopes. It takes in a variable number of scope strings as arguments and returns an `oauth2.Token` and an error.

func (*Gcp) GetOAuth2IdToken

func (g *Gcp) GetOAuth2IdToken(scope []string) (*oauth2.Token, error)

This is a method of the `Gcp` struct that is used to obtain an OAuth2 ID token for a given set of scopes. It takes in a variable number of scope strings as arguments and returns an `oauth2.Token` and an error. It first checks if the `scope` argument is nil, and if so, it sets it to the default `scope` value of the `Gcp` struct. It then calls the `getTokenSource` function to obtain a token source with the specified scopes and uses it to obtain the ID token by calling the `Token` method on the token source. If there is an error obtaining the token source or the token itself, an error is returned.

func (*Gcp) PubsubPublish

func (g *Gcp) PubsubPublish(t *pubsub.Topic, message map[string]interface{}) (string, error)

func (*Gcp) PubsubPublishBinary added in v0.3.8

func (g *Gcp) PubsubPublishBinary(t *pubsub.Topic, b []byte) (string, error)

func (*Gcp) PubsubReceive

func (g *Gcp) PubsubReceive(s *pubsub.Subscription, limit int, timeout int) ([]map[string]interface{}, error)

func (*Gcp) PubsubSubscription

func (g *Gcp) PubsubSubscription(subscription string) *pubsub.Subscription

func (*Gcp) PubsubTopic

func (g *Gcp) PubsubTopic(topic string) *pubsub.Topic

func (*Gcp) QueryTimeSeries

func (g *Gcp) QueryTimeSeries(projectId string, query string) ([]*monitoringpb.TimeSeriesData, error)

This function is querying time series data from Google Cloud Monitoring API. It takes in a project ID and a query string as parameters, and returns a slice of `monitoringpb.TimeSeriesData` and an error.

func (*Gcp) SpreadsheetAppend

func (g *Gcp) SpreadsheetAppend(spreadsheetId string, sheetName string, valueRange []interface{}) (string, error)

Appends a row of data to a Google Sheet. Parameters: - spreadsheetId: the ID of the Google Sheet. - sheetName: the name of the sheet to append data to. - valueRange: a slice of interface{} values representing the data to append. Returns: - string: an empty string. - error: an error if one occurred, otherwise nil.

func (*Gcp) SpreadsheetAppendWithUniqueId

func (g *Gcp) SpreadsheetAppendWithUniqueId(spreadsheetId string, sheetName string, values map[string]interface{}) (int64, error)

This function appends a row of data to a Google Sheet with a unique ID. Parameters: - spreadsheetId: the ID of the Google Sheet. - sheetName: the name of the sheet to append data to. - values: a slice of interface{} values representing the data to append. Returns: - string: the unique ID of the appended row. - error: an error if one occurred, otherwise nil.

func (*Gcp) SpreadsheetGet

func (g *Gcp) SpreadsheetGet(spreadsheetId string, sheetName string, cellRange string) ([][]interface{}, error)

This function retrieves data from a Google Sheet. Parameters: - spreadsheetId: the ID of the Google Sheet. - sheetName: the name of the sheet to retrieve data from. - cellRange: the range of cells to retrieve data from. Returns: - [][]interface{}: a 2D slice of interface{} values representing the retrieved data. - error: an error if one occurred, otherwise nil.

func (*Gcp) SpreadsheetGetRowByFilters

func (g *Gcp) SpreadsheetGetRowByFilters(spreadsheetId string, sheetName string, filters map[string]string) (map[string]interface{}, error)

Similar to https://pkg.go.dev/google.golang.org/api/sheets/v4#SpreadsheetsService.GetByDataFilter Get a row from a Google Sheet based on filters. Parameters: - spreadsheetId: the ID of the Google Sheet. - sheetName: the name of the sheet to search data in. - filters: a map of column names to values to search for in the specified column. Returns: - map[string]interface{}: a map of the row data if a match is found. - error: an error if one occurred, otherwise nil.

func (*Gcp) SpreadsheetGetUniqueIdByFiltersAndAppendIfNotExist

func (g *Gcp) SpreadsheetGetUniqueIdByFiltersAndAppendIfNotExist(spreadsheetId string, sheetName string, filters map[string]string, values map[string]interface{}) (int64, error)

func (*Gcp) SpreadsheetUpdate

func (g *Gcp) SpreadsheetUpdate(spreadsheetId string, sheetName string, cellRange string, valueRange []interface{}) (string, error)

Updates a range of cells in a Google Sheet. Parameters: - spreadsheetId: the ID of the Google Sheet. - sheetName: the name of the sheet to update data in. - cellRange: the range of cells to update data in. - valueRange: a slice of interface{} values representing the data to update. Returns: - string: an empty string. - error: an error if one occurred, otherwise nil.

type GcpConfig

type GcpConfig struct {
	// All gcloud emulator has to set XXX_EMULATOR_HOST environment variable
	EmulatorHost string
	Key          ServiceAccountKey
	Scope        []string
	ProjectId    string
}

type ModuleInstance

type ModuleInstance struct {
	// contains filtered or unexported fields
}

ModuleInstance represents an instance of the JS module.

func (*ModuleInstance) Exports

func (mi *ModuleInstance) Exports() modules.Exports

type Option

type Option func(*Gcp) error

type RootModule

type RootModule struct{}

RootModule is the global module instance that will create module instances for each VU.

func New

func New() *RootModule

func (*RootModule) NewModuleInstance

func (*RootModule) NewModuleInstance(vu modules.VU) modules.Instance

type ServiceAccountKey

type ServiceAccountKey struct {
	AuthProviderX509CertUrl string `json:"auth_provider_x509_cert_url"`
	AuthURL                 string `json:"auth_uri"`
	ClientEmail             string `json:"client_email"`
	ClientID                string `json:"client_id"`
	ClientSecret            string `json:"client_secret"`
	ClientX509CertUrl       string `json:"client_x509_cert_url"`
	PrivateKey              string `json:"private_key"`
	PrivateKeyID            string `json:"private_key_id"`
	ProjectID               string `json:"project_id"`
	TokenURL                string `json:"token_uri"`
	Type                    string `json:"type"`
	UniverseDomain          string `json:"universe_domain"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL