Copyright 2016 Google LLC 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.
Package errorreporting is a Google Stackdriver Error Reporting library. Any provided stacktraces must match the format produced by https://golang.org/pkg/runtime/#Stack or as per https://cloud.google.com/error-reporting/reference/rest/v1beta1/projects.events/report#ReportedErrorEvent for language specific stacktrace formats. This package is still experimental and subject to change. See https://cloud.google.com/error-reporting/ for more information.
package errorreporting // import "cloud.google.com/go/errorreporting"

import (
	
	
	
	
	
	
	

	vkit 
	
	
	gax 
	
	
	pb 
)
Config is additional configuration for Client.
ServiceName identifies the running program and is included in the error reports. Optional.
ServiceVersion identifies the version of the running program and is included in the error reports. Optional.
OnError is the function to call if any background tasks errored. By default, errors are logged.
	OnError func(err error)
}
Entry holds information about the reported error.
type Entry struct {
	Error error
	Req   *http.Request // if error is associated with a request.
	User  string        // an identifier for the user affected by the error
Stack specifies the stacktrace and call sequence correlated with the error. Stack's content must match the format specified by https://cloud.google.com/error-reporting/reference/rest/v1beta1/projects.events/report#ReportedErrorEvent.message or at least for Go programs, it must match the format produced by https://golang.org/pkg/runtime/debug/#Stack. If Stack is blank, the result of runtime.Stack will be used instead.
	Stack []byte
}
Client represents a Google Cloud Error Reporting client.
type Client struct {
	projectName    string
	apiClient      client
	serviceContext *pb.ServiceContext
	bundler        *bundler.Bundler

	onErrorFn func(err error)
}

var newClient = func( context.Context,  ...option.ClientOption) (client, error) {
	,  := vkit.NewReportErrorsClient(, ...)
	if  != nil {
		return nil, 
	}
	.SetGoogleClientInfo("gccl", version.Repo)
	return , nil
}
NewClient returns a new error reporting client. Generally you will want to create a client on program initialization and use it through the lifetime of the process.
func ( context.Context,  string,  Config,  ...option.ClientOption) (*Client, error) {
	if .ServiceName == "" {
		.ServiceName = "goapp"
	}
	,  := newClient(, ...)
	if  != nil {
		return nil, fmt.Errorf("creating client: %v", )
	}

	 := &Client{
		apiClient:   ,
		projectName: "projects/" + ,
		serviceContext: &pb.ServiceContext{
			Service: .ServiceName,
			Version: .ServiceVersion,
		},
		onErrorFn: .OnError,
	}
	 := bundler.NewBundler((*pb.ReportErrorEventRequest)(nil), func( interface{}) {
		 := .([]*pb.ReportErrorEventRequest)
		for ,  := range  {
			_,  = .apiClient.ReportErrorEvent(, )
			if  != nil {
				.onError()
			}
		}
TODO(jbd): Optimize bundler limits.
	.DelayThreshold = 2 * time.Second
	.BundleCountThreshold = 100
	.BundleByteThreshold = 1000
	.BundleByteLimit = 1000
	.BufferedByteLimit = 10000
	.bundler = 
	return , nil
}

func ( *Client) ( error) {
	if .onErrorFn != nil {
		.onErrorFn()
		return
	}
	log.Println()
}
Close calls Flush, then closes any resources held by the client. Close should be called when the client is no longer needed.
func ( *Client) () error {
	.Flush()
	return .apiClient.Close()
}
Report writes an error report. It doesn't block. Errors in writing the error report can be handled via Config.OnError.
func ( *Client) ( Entry) {
	.bundler.Add(.newRequest(), 1)
}
ReportSync writes an error report. It blocks until the entry is written.
func ( *Client) ( context.Context,  Entry) error {
	,  := .apiClient.ReportErrorEvent(, .newRequest())
	return 
}
Flush blocks until all currently buffered error reports are sent. If any errors occurred since the last call to Flush, or the creation of the client if this is the first call, then Flush reports the error via the Config.OnError handler.
func ( *Client) () {
	.bundler.Flush()
}

func ( *Client) ( Entry) *pb.ReportErrorEventRequest {
	var  string
	if .Stack != nil {
		 = string(.Stack)
limit the stack trace to 16k.
		var  [16 * 1024]byte
		 = chopStack([0:runtime.Stack([:], false)])
	}
	 := .Error.Error() + "\n" + 

	var  *pb.ErrorContext
	if  := .Req;  != nil {
		 = &pb.ErrorContext{
			HttpRequest: &pb.HttpRequestContext{
				Method:    .Method,
				Url:       .Host + .RequestURI,
				UserAgent: .UserAgent(),
				Referrer:  .Referer(),
				RemoteIp:  .RemoteAddr,
			},
		}
	}
	if .User != "" {
		if  == nil {
			 = &pb.ErrorContext{}
		}
		.User = .User
	}
	return &pb.ReportErrorEventRequest{
		ProjectName: .projectName,
		Event: &pb.ReportedErrorEvent{
			EventTime:      ptypes.TimestampNow(),
			ServiceContext: .serviceContext,
			Message:        ,
			Context:        ,
		},
	}
}
chopStack trims a stack trace so that the function which panics or calls Report is first.
func ( []byte) string {
	 := []byte("cloud.google.com/go/errorreporting.(*Client).Report")

	 := bytes.IndexByte(, '\n')
	if  == -1 {
		return string()
	}
	 := [:]
	 := bytes.Index(, )
	if  == -1 {
		return string()
	}
	 = [+1:]
	for  := 0;  < 2; ++ {
		 := bytes.IndexByte(, '\n')
		if  == -1 {
			return string()
		}
		 = [+1:]
	}
	return string([:+1]) + string()
}

type client interface {
	ReportErrorEvent(ctx context.Context, req *pb.ReportErrorEventRequest, opts ...gax.CallOption) (*pb.ReportErrorEventResponse, error)
	Close() error