-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
pshapovalov
committed
Apr 4, 2024
1 parent
37583a3
commit ddef536
Showing
6 changed files
with
290 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package logger | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/rs/zerolog" | ||
) | ||
|
||
var logger zerolog.Logger | ||
|
||
// Init initializes logger | ||
func Init(app string, lvl string) error { | ||
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix | ||
l, err := zerolog.ParseLevel(lvl) | ||
if err != nil { | ||
return err | ||
} | ||
logger = zerolog.New(os.Stderr).With().Timestamp().Str("app", app).Logger().Level(l) | ||
|
||
return nil | ||
} | ||
|
||
// Info returns zerolog event | ||
func Info() *zerolog.Event { | ||
return logger.Info() | ||
} | ||
|
||
// Warn returns zerolog event | ||
func Warn() *zerolog.Event { | ||
return logger.Warn() | ||
} | ||
|
||
// Error returns zerolog event | ||
func Error() *zerolog.Event { | ||
return logger.Error() | ||
} | ||
|
||
// Debug returns zerolog event | ||
func Debug() *zerolog.Event { | ||
return logger.Debug() | ||
} | ||
|
||
// Fatal returns zerolog event | ||
func Fatal() *zerolog.Event { | ||
return logger.Fatal() | ||
} | ||
|
||
// Panic returns zerolog event | ||
func Panic() *zerolog.Event { | ||
return logger.Panic() | ||
} | ||
|
||
// Trace returns zerolog event | ||
func Trace() *zerolog.Event { | ||
return logger.Trace() | ||
} | ||
|
||
// FatalOnError logs error and exits | ||
func FatalOnError(msg string, err error) { | ||
if err != nil { | ||
logger.Fatal().Err(err).Msg(msg) | ||
} | ||
} | ||
|
||
// Err logs error | ||
func Err(err error) *zerolog.Event { | ||
return logger.Err(err) | ||
} | ||
|
||
// Printf prints formatted message | ||
func Printf(format string, v ...interface{}) { | ||
logger.Info().Msgf(format, v...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package metric | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
) | ||
|
||
type Metrics struct { | ||
requestCounter prometheus.Counter | ||
responseCounter *prometheus.CounterVec | ||
histogramResponseTime *prometheus.HistogramVec | ||
} | ||
|
||
var metrics *Metrics | ||
|
||
func Init(_ context.Context, namespace string, app string) error { | ||
metrics = &Metrics{ | ||
requestCounter: promauto.NewCounter( | ||
prometheus.CounterOpts{ | ||
Namespace: namespace, | ||
Subsystem: "grpc", | ||
Name: app + "_requests_total", | ||
Help: "Количество запросов к серверу", | ||
}, | ||
), | ||
responseCounter: promauto.NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Namespace: namespace, | ||
Subsystem: "grpc", | ||
Name: app + "_responses_total", | ||
Help: "Количество ответов от сервера", | ||
}, | ||
[]string{"status", "method"}, | ||
), | ||
histogramResponseTime: promauto.NewHistogramVec( | ||
prometheus.HistogramOpts{ | ||
Namespace: namespace, | ||
Subsystem: "grpc", | ||
Name: app + "_histogram_response_time_seconds", | ||
Help: "Время ответа от сервера", | ||
Buckets: prometheus.ExponentialBuckets(0.0001, 2, 16), | ||
}, | ||
[]string{"status"}, | ||
), | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func IncRequestCounter() { | ||
metrics.requestCounter.Inc() | ||
} | ||
|
||
func IncResponseCounter(status string, method string) { | ||
metrics.responseCounter.WithLabelValues(status, method).Inc() | ||
} | ||
|
||
func HistogramResponseTimeObserve(status string, time float64) { | ||
metrics.histogramResponseTime.WithLabelValues(status).Observe(time) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package tracer | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" | ||
"go.opentelemetry.io/otel/sdk/resource" | ||
"go.opentelemetry.io/otel/sdk/trace" | ||
semconv "go.opentelemetry.io/otel/semconv/v1.24.0" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials/insecure" | ||
) | ||
|
||
// NewTraceExporter creates new trace exporter | ||
func NewTraceExporter(ctx context.Context, jaegerAddress string) (trace.SpanExporter, error) { | ||
ctx, cancel := context.WithTimeout(ctx, time.Second) | ||
defer cancel() | ||
conn, err := grpc.DialContext(ctx, jaegerAddress, | ||
grpc.WithTransportCredentials(insecure.NewCredentials()), | ||
grpc.WithBlock(), | ||
) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create gRPC connection to collector: %w", err) | ||
} | ||
|
||
// Set up a trace exporter | ||
traceExporter, err := otlptracegrpc.New(ctx, otlptracegrpc.WithGRPCConn(conn)) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create trace exporter: %w", err) | ||
} | ||
|
||
return traceExporter, nil | ||
} | ||
|
||
func NewTraceProvider(ctx context.Context, exporter trace.SpanExporter, servicename string) (*trace.TracerProvider, error) { | ||
res, err := resource.New(ctx, | ||
resource.WithAttributes( | ||
semconv.ServiceName(servicename), | ||
), | ||
) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create resource: %w", err) | ||
} | ||
|
||
bsp := trace.NewBatchSpanProcessor(exporter) | ||
tracerProvider := trace.NewTracerProvider( | ||
trace.WithSampler(trace.AlwaysSample()), | ||
trace.WithResource(res), | ||
trace.WithSpanProcessor(bsp), | ||
) | ||
|
||
return tracerProvider, nil | ||
} |