github.com/newrelic/newrelic-client-go@v1.1.0/pkg/logs/logs.go (about)

     1  package logs
     2  
     3  import (
     4  	"errors"
     5  	"time"
     6  
     7  	"github.com/newrelic/newrelic-client-go/internal/http"
     8  	"github.com/newrelic/newrelic-client-go/pkg/config"
     9  	"github.com/newrelic/newrelic-client-go/pkg/logging"
    10  )
    11  
    12  // Logs is used to send log data to the New Relic Log API
    13  
    14  const (
    15  	DefaultBatchWorkers = 1
    16  	DefaultBatchSize    = 900
    17  	DefaultBatchTimeout = 60 * time.Second
    18  )
    19  
    20  type Logs struct {
    21  	client http.Client
    22  	config config.Config
    23  	logger logging.Logger
    24  
    25  	// For queue based log handling
    26  	accountID  int
    27  	logQueue   chan interface{}
    28  	logTimer   *time.Timer
    29  	flushQueue []chan bool
    30  
    31  	// These have defaults
    32  	batchWorkers int
    33  	batchSize    int
    34  	batchTimeout time.Duration
    35  }
    36  
    37  // New is used to create a new Logs client instance.
    38  func New(cfg config.Config) Logs {
    39  	cfg.Compression = config.Compression.Gzip
    40  
    41  	client := http.NewClient(cfg)
    42  	if cfg.InsightsInsertKey != "" {
    43  		client.SetAuthStrategy(&http.LogsInsertKeyAuthorizer{})
    44  	} else {
    45  		client.SetAuthStrategy(&http.LicenseKeyAuthorizer{})
    46  	}
    47  
    48  	pkg := Logs{
    49  		client:       client,
    50  		config:       cfg,
    51  		logger:       cfg.GetLogger(),
    52  		batchWorkers: DefaultBatchWorkers,
    53  		batchSize:    DefaultBatchSize,
    54  		batchTimeout: DefaultBatchTimeout,
    55  	}
    56  
    57  	return pkg
    58  }
    59  
    60  // CreateLogEntry reports a log entry to New Relic.
    61  // It's up to the caller to send a valid Log API payload, no checking done here
    62  func (l *Logs) CreateLogEntry(logEntry interface{}) error {
    63  	if logEntry == nil {
    64  		return errors.New("logs: CreateLogEntry: logEntry is nil, nothing to do")
    65  	}
    66  	_, err := l.client.Post(l.config.Region().LogsURL(), nil, logEntry, nil)
    67  
    68  	// If no error is returned then the call succeeded
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	return nil
    74  }