github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/events/interface.go (about)

     1  package events
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  	"time"
     7  
     8  	"github.com/kyma-project/kyma-environment-broker/common/events"
     9  )
    10  
    11  type Config struct {
    12  	Enabled       bool          `envconfig:"default=false"`
    13  	Retention     time.Duration `envconfig:"default=336h"` // two weeks: 24*14 = 336
    14  	PollingPeriod time.Duration `envconfig:"default=1h"`
    15  }
    16  
    17  var (
    18  	ev       Interface
    19  	initLock sync.Mutex
    20  )
    21  
    22  type Interface interface {
    23  	ListEvents(filter events.EventFilter) ([]events.EventDTO, error)
    24  	InsertEvent(eventLevel events.EventLevel, message, instanceID, operationID string)
    25  	RunGarbageCollection(pollingPeriod, retention time.Duration)
    26  }
    27  
    28  func New(cfg Config, events Interface) Interface {
    29  	if !cfg.Enabled {
    30  		return nil
    31  	}
    32  	initLock.Lock()
    33  	defer initLock.Unlock()
    34  	if ev == nil {
    35  		ev = events
    36  		go ev.RunGarbageCollection(cfg.PollingPeriod, cfg.Retention)
    37  	}
    38  	return ev
    39  }
    40  
    41  func Infof(instanceID, operationID, format string, args ...any) {
    42  	insertEvent(events.InfoEventLevel, fmt.Sprintf(format, args...), instanceID, operationID)
    43  }
    44  
    45  func Errorf(instanceID, operationID string, err error, format string, args ...any) {
    46  	insertEvent(events.ErrorEventLevel, fmt.Sprintf("%v: %v", fmt.Sprintf(format, args...), err), instanceID, operationID)
    47  }
    48  
    49  func insertEvent(eventLevel events.EventLevel, msg, instanceID, operationID string) {
    50  	if ev != nil {
    51  		ev.InsertEvent(eventLevel, msg, instanceID, operationID)
    52  	}
    53  }