logadmin

package
v1.5.0-jsonlog-preview Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2021 License: Apache-2.0 Imports: 22 Imported by: 107

Documentation

Overview

Package logadmin contains a Cloud Logging client that can be used for reading logs and working with sinks, metrics and monitored resources. For a client that can write logs, see package cloud.google.com/go/logging.

The client uses Logging API v2. See https://cloud.google.com/logging/docs/api/v2/ for an introduction to the API.

Note: This package is in beta. Some backwards-incompatible changes may occur.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is a Logging client. A Client is associated with a single Cloud project.

func NewClient

func NewClient(ctx context.Context, parent string, opts ...option.ClientOption) (*Client, error)

NewClient returns a new logging client associated with the provided project ID.

By default NewClient uses AdminScope. To use a different scope, call NewClient using a WithScopes option (see https://godoc.org/google.golang.org/api/option#WithScopes).

Example
package main

import (
	"context"

	"cloud.google.com/go/logging/logadmin"
)

func main() {
	ctx := context.Background()
	client, err := logadmin.NewClient(ctx, "my-project")
	if err != nil {
		// TODO: Handle error.
	}
	// Use client to manage logs, metrics and sinks.
	// Close the client when finished.
	if err := client.Close(); err != nil {
		// TODO: Handle error.
	}
}
Output:

func (*Client) Close

func (c *Client) Close() error

Close closes the client.

func (*Client) CreateMetric

func (c *Client) CreateMetric(ctx context.Context, m *Metric) error

CreateMetric creates a logs-based metric.

Example
package main

import (
	"context"

	"cloud.google.com/go/logging/logadmin"
)

func main() {
	ctx := context.Background()
	client, err := logadmin.NewClient(ctx, "my-project")
	if err != nil {
		// TODO: Handle error.
	}
	err = client.CreateMetric(ctx, &logadmin.Metric{
		ID:          "severe-errors",
		Description: "entries at ERROR or higher severities",
		Filter:      "severity >= ERROR",
	})
	if err != nil {
		// TODO: Handle error.
	}
}
Output:

func (*Client) CreateSink

func (c *Client) CreateSink(ctx context.Context, sink *Sink) (*Sink, error)

CreateSink creates a Sink. It returns an error if the Sink already exists. Requires AdminScope.

Example
package main

import (
	"context"
	"fmt"

	"cloud.google.com/go/logging/logadmin"
)

func main() {
	ctx := context.Background()
	client, err := logadmin.NewClient(ctx, "my-project")
	if err != nil {
		// TODO: Handle error.
	}
	sink, err := client.CreateSink(ctx, &logadmin.Sink{
		ID:          "severe-errors-to-gcs",
		Destination: "storage.googleapis.com/my-bucket",
		Filter:      "severity >= ERROR",
	})
	if err != nil {
		// TODO: Handle error.
	}
	fmt.Println(sink)
}
Output:

func (*Client) CreateSinkOpt

func (c *Client) CreateSinkOpt(ctx context.Context, sink *Sink, opts SinkOptions) (*Sink, error)

CreateSinkOpt creates a Sink using the provided options. It returns an error if the Sink already exists. Requires AdminScope.

func (*Client) DeleteLog

func (c *Client) DeleteLog(ctx context.Context, logID string) error

DeleteLog deletes a log and all its log entries. The log will reappear if it receives new entries. logID identifies the log within the project. An example log ID is "syslog". Requires AdminScope.

Example
package main

import (
	"context"

	"cloud.google.com/go/logging/logadmin"
)

func main() {
	ctx := context.Background()
	client, err := logadmin.NewClient(ctx, "my-project")
	if err != nil {
		// TODO: Handle error.
	}
	err = client.DeleteLog(ctx, "my-log")
	if err != nil {
		// TODO: Handle error.
	}
}
Output:

func (*Client) DeleteMetric

func (c *Client) DeleteMetric(ctx context.Context, metricID string) error

DeleteMetric deletes a log-based metric. The provided metric ID is the metric identifier. For example, "severe_errors".

Example
package main

import (
	"context"

	"cloud.google.com/go/logging/logadmin"
)

func main() {
	ctx := context.Background()
	client, err := logadmin.NewClient(ctx, "my-project")
	if err != nil {
		// TODO: Handle error.
	}
	if err := client.DeleteMetric(ctx, "severe-errors"); err != nil {
		// TODO: Handle error.
	}
}
Output:

func (*Client) DeleteSink

func (c *Client) DeleteSink(ctx context.Context, sinkID string) error

DeleteSink deletes a sink. The provided sinkID is the sink's identifier, such as "my-severe-errors-to-pubsub". Requires AdminScope.

Example
package main

import (
	"context"

	"cloud.google.com/go/logging/logadmin"
)

func main() {
	ctx := context.Background()
	client, err := logadmin.NewClient(ctx, "my-project")
	if err != nil {
		// TODO: Handle error.
	}
	if err := client.DeleteSink(ctx, "severe-errors-to-gcs"); err != nil {
		// TODO: Handle error.
	}
}
Output:

func (*Client) Entries

func (c *Client) Entries(ctx context.Context, opts ...EntriesOption) *EntryIterator

Entries returns an EntryIterator for iterating over log entries. By default, the log entries will be restricted to those from the project passed to NewClient. This may be overridden by passing a ProjectIDs option. Requires ReadScope or AdminScope.

Example
package main

import (
	"context"

	"cloud.google.com/go/logging/logadmin"
)

func main() {
	ctx := context.Background()
	client, err := logadmin.NewClient(ctx, "my-project")
	if err != nil {
		// TODO: Handle error.
	}
	it := client.Entries(ctx, logadmin.Filter(`logName = "projects/my-project/logs/my-log"`))
	_ = it // TODO: iterate using Next or iterator.Pager.
}
Output:

Example (Pagination)
package main

import (
	"bytes"
	"context"
	"flag"
	"fmt"
	"html/template"
	"log"
	"net/http"

	"cloud.google.com/go/logging"
	"cloud.google.com/go/logging/logadmin"
	"google.golang.org/api/iterator"
)

var (
	client    *logadmin.Client
	projectID = flag.String("project-id", "", "ID of the project to use")
)

func main() {
	// This example demonstrates how to iterate through items a page at a time
	// even if each successive page is fetched by a different process. It is a
	// complete web server that displays pages of log entries. To run it as a
	// standalone program, rename both the package and this function to "main".
	ctx := context.Background()
	flag.Parse()
	if *projectID == "" {
		log.Fatal("-project-id missing")
	}
	var err error
	client, err = logadmin.NewClient(ctx, *projectID)
	if err != nil {
		log.Fatalf("creating logging client: %v", err)
	}

	http.HandleFunc("/entries", handleEntries)
	log.Print("listening on 8080")
	log.Fatal(http.ListenAndServe(":8080", nil))
}

var pageTemplate = template.Must(template.New("").Parse(`
<table>
  {{range .Entries}}
    <tr><td>{{.}}</td></tr>
  {{end}}
</table>
{{if .Next}}
  <a href="/entries?pageToken={{.Next}}">Next Page</a>
{{end}}
`))

func handleEntries(w http.ResponseWriter, r *http.Request) {
	ctx := context.Background()
	filter := fmt.Sprintf(`logName = "projects/%s/logs/testlog"`, *projectID)
	it := client.Entries(ctx, logadmin.Filter(filter))
	var entries []*logging.Entry
	nextTok, err := iterator.NewPager(it, 5, r.URL.Query().Get("pageToken")).NextPage(&entries)
	if err != nil {
		http.Error(w, fmt.Sprintf("problem getting the next page: %v", err), http.StatusInternalServerError)
		return
	}
	data := struct {
		Entries []*logging.Entry
		Next    string
	}{
		entries,
		nextTok,
	}
	var buf bytes.Buffer
	if err := pageTemplate.Execute(&buf, data); err != nil {
		http.Error(w, fmt.Sprintf("problem executing page template: %v", err), http.StatusInternalServerError)
	}
	if _, err := buf.WriteTo(w); err != nil {
		log.Printf("writing response: %v", err)
	}
}
Output:

func (*Client) Logs

func (c *Client) Logs(ctx context.Context) *LogIterator

Logs lists the logs owned by the parent resource of the client.

func (*Client) Metric

func (c *Client) Metric(ctx context.Context, metricID string) (*Metric, error)

Metric gets a logs-based metric. The provided metric ID is the metric identifier. For example, "severe_errors". Requires ReadScope or AdminScope.

Example
package main

import (
	"context"
	"fmt"

	"cloud.google.com/go/logging/logadmin"
)

func main() {
	ctx := context.Background()
	client, err := logadmin.NewClient(ctx, "my-project")
	if err != nil {
		// TODO: Handle error.
	}
	m, err := client.Metric(ctx, "severe-errors")
	if err != nil {
		// TODO: Handle error.
	}
	fmt.Println(m)
}
Output:

func (*Client) Metrics

func (c *Client) Metrics(ctx context.Context) *MetricIterator

Metrics returns a MetricIterator for iterating over all Metrics in the Client's project. Requires ReadScope or AdminScope.

Example
package main

import (
	"context"

	"cloud.google.com/go/logging/logadmin"
)

func main() {
	ctx := context.Background()
	client, err := logadmin.NewClient(ctx, "my-project")
	if err != nil {
		// TODO: Handle error.
	}
	it := client.Metrics(ctx)
	_ = it // TODO: iterate using Next or iterator.Pager.
}
Output:

func (*Client) ResourceDescriptors

func (c *Client) ResourceDescriptors(ctx context.Context) *ResourceDescriptorIterator

ResourceDescriptors returns a ResourceDescriptorIterator for iterating over MonitoredResourceDescriptors. Requires ReadScope or AdminScope. See https://cloud.google.com/logging/docs/api/v2/#monitored-resources for an explanation of monitored resources. See https://cloud.google.com/logging/docs/api/v2/resource-list for a list of monitored resources.

Example
package main

import (
	"context"

	"cloud.google.com/go/logging/logadmin"
)

func main() {
	ctx := context.Background()
	client, err := logadmin.NewClient(ctx, "my-project")
	if err != nil {
		// TODO: Handle error.
	}
	it := client.ResourceDescriptors(ctx)
	_ = it // TODO: iterate using Next or iterator.Pager.
}
Output:

func (*Client) Sink

func (c *Client) Sink(ctx context.Context, sinkID string) (*Sink, error)

Sink gets a sink. The provided sinkID is the sink's identifier, such as "my-severe-errors-to-pubsub". Requires ReadScope or AdminScope.

Example
package main

import (
	"context"
	"fmt"

	"cloud.google.com/go/logging/logadmin"
)

func main() {
	ctx := context.Background()
	client, err := logadmin.NewClient(ctx, "my-project")
	if err != nil {
		// TODO: Handle error.
	}
	s, err := client.Sink(ctx, "severe-errors-to-gcs")
	if err != nil {
		// TODO: Handle error.
	}
	fmt.Println(s)
}
Output:

func (*Client) Sinks

func (c *Client) Sinks(ctx context.Context) *SinkIterator

Sinks returns a SinkIterator for iterating over all Sinks in the Client's project. Requires ReadScope or AdminScope.

Example
package main

import (
	"context"

	"cloud.google.com/go/logging/logadmin"
)

func main() {
	ctx := context.Background()
	client, err := logadmin.NewClient(ctx, "my-project")
	if err != nil {
		// TODO: Handle error.
	}
	it := client.Sinks(ctx)
	_ = it // TODO: iterate using Next or iterator.Pager.
}
Output:

func (*Client) UpdateMetric

func (c *Client) UpdateMetric(ctx context.Context, m *Metric) error

UpdateMetric creates a logs-based metric if it does not exist, or updates an existing one.

Example
package main

import (
	"context"

	"cloud.google.com/go/logging/logadmin"
)

func main() {
	ctx := context.Background()
	client, err := logadmin.NewClient(ctx, "my-project")
	if err != nil {
		// TODO: Handle error.
	}
	err = client.UpdateMetric(ctx, &logadmin.Metric{
		ID:          "severe-errors",
		Description: "entries at high severities",
		Filter:      "severity > ERROR",
	})
	if err != nil {
		// TODO: Handle error.
	}
}
Output:

func (*Client) UpdateSink

func (c *Client) UpdateSink(ctx context.Context, sink *Sink) (*Sink, error)

UpdateSink updates an existing Sink. Requires AdminScope.

WARNING: UpdateSink will always update the Destination, Filter and IncludeChildren fields of the sink, even if they have their zero values. Use UpdateSinkOpt for more control over which fields to update.

Example
package main

import (
	"context"
	"fmt"

	"cloud.google.com/go/logging/logadmin"
)

func main() {
	ctx := context.Background()
	client, err := logadmin.NewClient(ctx, "my-project")
	if err != nil {
		// TODO: Handle error.
	}
	sink, err := client.UpdateSink(ctx, &logadmin.Sink{
		ID:          "severe-errors-to-gcs",
		Destination: "storage.googleapis.com/my-other-bucket",
		Filter:      "severity >= ERROR",
	})
	if err != nil {
		// TODO: Handle error.
	}
	fmt.Println(sink)
}
Output:

func (*Client) UpdateSinkOpt

func (c *Client) UpdateSinkOpt(ctx context.Context, sink *Sink, opts SinkOptions) (*Sink, error)

UpdateSinkOpt updates an existing Sink, using the provided options. Requires AdminScope.

To change a sink's writer identity to a service account unique to the sink, set opts.UniqueWriterIdentity to true. It is not possible to change a sink's writer identity from a unique service account to a non-unique writer identity.

type EntriesOption

type EntriesOption interface {
	// contains filtered or unexported methods
}

An EntriesOption is an option for listing log entries.

func Filter

func Filter(f string) EntriesOption

Filter sets an advanced logs filter for listing log entries (see https://cloud.google.com/logging/docs/view/advanced_filters). The filter is compared against all log entries in the projects specified by ProjectIDs. Only entries that match the filter are retrieved. An empty filter (the default) matches all log entries.

In the filter string, log names must be written in their full form, as "projects/PROJECT-ID/logs/LOG-ID". Forward slashes in LOG-ID must be replaced by %2F before calling Filter.

Timestamps in the filter string must be written in RFC 3339 format. By default, timestamp filters for the past 24 hours.

Example (Timestamp)
package main

import (
	"context"
	"fmt"
	"time"

	"cloud.google.com/go/logging/logadmin"
)

func main() {
	// This example demonstrates how to list the last 24 hours of log entries.
	ctx := context.Background()
	client, err := logadmin.NewClient(ctx, "my-project")
	if err != nil {
		// TODO: Handle error.
	}
	oneDayAgo := time.Now().Add(-24 * time.Hour)
	t := oneDayAgo.Format(time.RFC3339) // Logging API wants timestamps in RFC 3339 format.
	it := client.Entries(ctx, logadmin.Filter(fmt.Sprintf(`timestamp > "%s"`, t)))
	_ = it // TODO: iterate using Next or iterator.Pager.
}
Output:

func NewestFirst

func NewestFirst() EntriesOption

NewestFirst causes log entries to be listed from most recent (newest) to least recent (oldest). By default, they are listed from oldest to newest.

func ProjectIDs

func ProjectIDs(pids []string) EntriesOption

ProjectIDs sets the project IDs or project numbers from which to retrieve log entries. Examples of a project ID: "my-project-1A", "1234567890".

func ResourceNames

func ResourceNames(rns []string) EntriesOption

ResourceNames sets the resource names from which to retrieve log entries. Examples: "projects/my-project-1A", "organizations/my-org".

type EntryIterator

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

An EntryIterator iterates over log entries.

func (*EntryIterator) Next

func (it *EntryIterator) Next() (*logging.Entry, error)

Next returns the next result. Its second return value is iterator.Done (https://godoc.org/google.golang.org/api/iterator) if there are no more results. Once Next returns Done, all subsequent calls will return Done.

Example
package main

import (
	"context"
	"fmt"

	"cloud.google.com/go/logging/logadmin"
	"google.golang.org/api/iterator"
)

func main() {
	ctx := context.Background()
	client, err := logadmin.NewClient(ctx, "my-project")
	if err != nil {
		// TODO: Handle error.
	}
	it := client.Entries(ctx)
	for {
		entry, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			// TODO: Handle error.
		}
		fmt.Println(entry)
	}
}
Output:

func (*EntryIterator) PageInfo

func (it *EntryIterator) PageInfo() *iterator.PageInfo

PageInfo supports pagination. See https://godoc.org/google.golang.org/api/iterator package for details.

type LogIterator

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

A LogIterator iterates over logs.

func (*LogIterator) Next

func (it *LogIterator) Next() (string, error)

Next returns the next result. Its second return value is iterator.Done (https://godoc.org/google.golang.org/api/iterator) if there are no more results. Once Next returns Done, all subsequent calls will return Done.

func (*LogIterator) PageInfo

func (it *LogIterator) PageInfo() *iterator.PageInfo

PageInfo supports pagination. See https://godoc.org/google.golang.org/api/iterator package for details.

type Metric

type Metric struct {
	// ID is a client-assigned metric identifier. Example:
	// "severe_errors".  Metric identifiers are limited to 1000
	// characters and can include only the following characters: A-Z,
	// a-z, 0-9, and the special characters _-.,+!*',()%/\.  The
	// forward-slash character (/) denotes a hierarchy of name pieces,
	// and it cannot be the first character of the name.
	ID string

	// Description describes this metric. It is used in documentation.
	Description string

	// Filter is an advanced logs filter (see
	// https://cloud.google.com/logging/docs/view/advanced_filters).
	// Example: "logName:syslog AND severity>=ERROR".
	Filter string
}

Metric describes a logs-based metric. The value of the metric is the number of log entries that match a logs filter.

Metrics are a feature of Cloud Monitoring. See https://cloud.google.com/monitoring/api/v3/metrics for more about them.

type MetricIterator

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

A MetricIterator iterates over Metrics.

func (*MetricIterator) Next

func (it *MetricIterator) Next() (*Metric, error)

Next returns the next result. Its second return value is Done if there are no more results. Once Next returns Done, all subsequent calls will return Done.

Example
package main

import (
	"context"
	"fmt"

	"cloud.google.com/go/logging/logadmin"
	"google.golang.org/api/iterator"
)

func main() {
	ctx := context.Background()
	client, err := logadmin.NewClient(ctx, "my-project")
	if err != nil {
		// TODO: Handle error.
	}
	it := client.Metrics(ctx)
	for {
		metric, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			// TODO: Handle error.
		}
		fmt.Println(metric)
	}
}
Output:

func (*MetricIterator) PageInfo

func (it *MetricIterator) PageInfo() *iterator.PageInfo

PageInfo supports pagination. See the google.golang.org/api/iterator package for details.

type ResourceDescriptorIterator

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

ResourceDescriptorIterator is an iterator over MonitoredResourceDescriptors.

func (*ResourceDescriptorIterator) Next

Next returns the next result. Its second return value is Done if there are no more results. Once Next returns Done, all subsequent calls will return Done.

Example
package main

import (
	"context"
	"fmt"

	"cloud.google.com/go/logging/logadmin"
	"google.golang.org/api/iterator"
)

func main() {
	ctx := context.Background()
	client, err := logadmin.NewClient(ctx, "my-project")
	if err != nil {
		// TODO: Handle error.
	}
	it := client.ResourceDescriptors(ctx)
	for {
		rdesc, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			// TODO: Handle error.
		}
		fmt.Println(rdesc)
	}
}
Output:

func (*ResourceDescriptorIterator) PageInfo

PageInfo supports pagination. See the google.golang.org/api/iterator package for details.

type Sink

type Sink struct {
	// ID is a client-assigned sink identifier. Example:
	// "my-severe-errors-to-pubsub".
	// Sink identifiers are limited to 1000 characters
	// and can include only the following characters: A-Z, a-z,
	// 0-9, and the special characters "_-.".
	ID string

	// Destination is the export destination. See
	// https://cloud.google.com/logging/docs/api/tasks/exporting-logs.
	// Examples: "storage.googleapis.com/a-bucket",
	// "bigquery.googleapis.com/projects/a-project-id/datasets/a-dataset".
	Destination string

	// Filter optionally specifies an advanced logs filter (see
	// https://cloud.google.com/logging/docs/view/advanced_filters) that
	// defines the log entries to be exported. Example: "logName:syslog AND
	// severity>=ERROR". If omitted, all entries are returned.
	Filter string

	// WriterIdentity must be a service account name. When exporting logs, Logging
	// adopts this identity for authorization. The export destination's owner must
	// give this service account permission to write to the export destination.
	WriterIdentity string

	// IncludeChildren, when set to true, allows the sink to export log entries from
	// the organization or folder, plus (recursively) from any contained folders, billing
	// accounts, or projects. IncludeChildren is false by default. You can use the sink's
	// filter to choose log entries from specific projects, specific resource types, or
	// specific named logs.
	//
	// Caution: If you enable this feature, your aggregated export sink might export
	// a very large number of log entries. To avoid exporting too many log entries,
	// design your aggregated export sink filter carefully, as described on
	// https://cloud.google.com/logging/docs/export/aggregated_exports.
	IncludeChildren bool
}

Sink describes a sink used to export log entries outside Cloud Logging. Incoming log entries matching a filter are exported to a destination (a Cloud Storage bucket, BigQuery dataset or Cloud Pub/Sub topic).

For more information, see https://cloud.google.com/logging/docs/export/using_exported_logs. (The Sinks in this package are what the documentation refers to as "project sinks".)

type SinkIterator

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

A SinkIterator iterates over Sinks.

func (*SinkIterator) Next

func (it *SinkIterator) Next() (*Sink, error)

Next returns the next result. Its second return value is Done if there are no more results. Once Next returns Done, all subsequent calls will return Done.

Example
package main

import (
	"context"
	"fmt"

	"cloud.google.com/go/logging/logadmin"
	"google.golang.org/api/iterator"
)

func main() {
	ctx := context.Background()
	client, err := logadmin.NewClient(ctx, "my-project")
	if err != nil {
		// TODO: Handle error.
	}
	it := client.Sinks(ctx)
	for {
		sink, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			// TODO: Handle error.
		}
		fmt.Println(sink)
	}
}
Output:

func (*SinkIterator) PageInfo

func (it *SinkIterator) PageInfo() *iterator.PageInfo

PageInfo supports pagination. See the google.golang.org/api/iterator package for details.

type SinkOptions

type SinkOptions struct {
	// Determines the kind of IAM identity returned as WriterIdentity in the new
	// sink. If this value is omitted or set to false, and if the sink's parent is a
	// project, then the value returned as WriterIdentity is the same group or
	// service account used by Cloud Logging before the addition of writer
	// identities to the API. The sink's destination must be in the same project as
	// the sink itself.
	//
	// If this field is set to true, or if the sink is owned by a non-project
	// resource such as an organization, then the value of WriterIdentity will
	// be a unique service account used only for exports from the new sink.
	UniqueWriterIdentity bool

	// These fields apply only to UpdateSinkOpt calls. The corresponding sink field
	// is updated if and only if the Update field is true.
	UpdateDestination     bool
	UpdateFilter          bool
	UpdateIncludeChildren bool
}

SinkOptions define options to be used when creating or updating a sink.

Jump to

Keyboard shortcuts

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