github.com/asynkron/protoactor-go@v0.0.0-20240308120642-ef91a6abee75/actor/config.go (about)

     1  package actor
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/lmittmann/tint"
     6  	"log/slog"
     7  	"net/http"
     8  	"os"
     9  	"time"
    10  
    11  	"go.opentelemetry.io/otel"
    12  	"go.opentelemetry.io/otel/exporters/prometheus"
    13  	"go.opentelemetry.io/otel/metric"
    14  	sdkmetric "go.opentelemetry.io/otel/sdk/metric"
    15  
    16  	"github.com/prometheus/client_golang/prometheus/promhttp"
    17  )
    18  
    19  type Config struct {
    20  	DeadLetterThrottleInterval  time.Duration      // throttle deadletter logging after this interval
    21  	DeadLetterThrottleCount     int32              // throttle deadletter logging after this count
    22  	DeadLetterRequestLogging    bool               // do not log dead-letters with sender
    23  	DeveloperSupervisionLogging bool               // console log and promote supervision logs to Warning level
    24  	DiagnosticsSerializer       func(Actor) string // extract diagnostics from actor and return as string
    25  	MetricsProvider             metric.MeterProvider
    26  	LoggerFactory               func(system *ActorSystem) *slog.Logger
    27  }
    28  
    29  func defaultConfig() *Config {
    30  	return &Config{
    31  		MetricsProvider:             nil,
    32  		DeadLetterThrottleInterval:  1 * time.Second,
    33  		DeadLetterThrottleCount:     3,
    34  		DeadLetterRequestLogging:    true,
    35  		DeveloperSupervisionLogging: false,
    36  		DiagnosticsSerializer: func(actor Actor) string {
    37  			return ""
    38  		},
    39  		LoggerFactory: func(system *ActorSystem) *slog.Logger {
    40  			w := os.Stderr
    41  
    42  			// create a new logger
    43  			return slog.New(tint.NewHandler(w, &tint.Options{
    44  				Level:      slog.LevelInfo,
    45  				TimeFormat: time.Kitchen,
    46  			})).With("lib", "Proto.Actor").
    47  				With("system", system.ID)
    48  		},
    49  	}
    50  }
    51  
    52  func defaultPrometheusProvider(port int) metric.MeterProvider {
    53  	exporter, err := prometheus.New()
    54  	if err != nil {
    55  		err = fmt.Errorf("failed to initialize prometheus exporter: %w", err)
    56  		//TODO: fix
    57  		//plog.Error(err.Error(), log.Error(err))
    58  
    59  		return nil
    60  	}
    61  
    62  	provider := sdkmetric.NewMeterProvider(sdkmetric.WithReader(exporter.Reader))
    63  	otel.SetMeterProvider(provider)
    64  
    65  	http.Handle("/", promhttp.Handler())
    66  	_port := fmt.Sprintf(":%d", port)
    67  
    68  	go func() {
    69  		_ = http.ListenAndServe(_port, nil)
    70  	}()
    71  
    72  	//TODO: fix
    73  	//plog.Debug(fmt.Sprintf("Prometheus server running on %s", _port))
    74  
    75  	return provider
    76  }
    77  
    78  func NewConfig() *Config {
    79  	return defaultConfig()
    80  }