func()

in galog_cloudlogging.go [177:238]


func (cb *CloudBackend) InitClient(ctx context.Context, opts *CloudOptions) error {
	if cb.client != nil {
		return errCloudLoggingAlreadyInitialized
	}

	var clientOptions []option.ClientOption

	if opts.UserAgent != "" {
		clientOptions = append(clientOptions, option.WithUserAgent(opts.UserAgent))
	}

	if opts.WithoutAuthentication {
		clientOptions = append(clientOptions, option.WithoutAuthentication())
	}

	// Set the default flush timeout if not provided.
	if opts.PingTimeout == 0 {
		opts.PingTimeout = DefaultCloudLoggingPingTimeout
	}

	cb.disableClientErrorLogging = opts.DisableClientErrorLogging

	errTimeout := opts.ClientErrorInterval
	if errTimeout == 0 {
		errTimeout = DefaultClientErrorInterval
	}

	cb.periodicLogger = &periodicLogger{
		interval: errTimeout,
	}

	client, err := logging.NewClient(ctx, opts.Project, clientOptions...)
	if err != nil {
		return fmt.Errorf("failed to initialize cloud logging client: %+v", err)
	}

	client.OnError = func(err error) {
		if opts.DisableClientErrorLogging {
			return
		}
		cb.periodicLogger.log(err)
	}

	labels := make(map[string]string)
	for k, v := range opts.ExtraLabels {
		labels[k] = v
	}
	if opts.Instance != "" {
		labels["instance_name"] = opts.Instance
	}

	var loggerOptions []logging.LoggerOption
	loggerOptions = append(loggerOptions, logging.CommonLabels(labels))
	loggerOptions = append(loggerOptions, logging.DelayThreshold(opts.FlushCadence))
	logger := client.Logger(opts.Ident, loggerOptions...)

	cb.client = client
	cb.logger = logger
	cb.opts = opts

	return nil
}