Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Actualize Prometheus metrics collecting #632

Merged
merged 7 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG_DEV.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 0.95.0 - 2023-02-07
- Added collecting new Prometheus metrics for AcraServer/AcraTranslator;

# 0.95.0 - 2023-02-02
- Improve processing int4 values from PostgreSQL with binary format of values

Expand Down
5 changes: 4 additions & 1 deletion cmd/acra-server/common/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ package common
import (
"sync"

"github.com/prometheus/client_golang/prometheus"

"github.com/cossacklabs/acra/cmd"
"github.com/cossacklabs/acra/decryptor/base"
"github.com/cossacklabs/acra/network"
"github.com/cossacklabs/acra/utils"
"github.com/prometheus/client_golang/prometheus"
)

const (
Expand Down Expand Up @@ -54,6 +55,8 @@ func RegisterMetrics(serviceName string, version *utils.Version, edition utils.P
prometheus.MustRegister(connectionCounter)
prometheus.MustRegister(connectionProcessingTimeHistogram)
base.RegisterAcraStructProcessingMetrics()
base.RegisterEncryptionDecryptionProcessingMetrics()
base.RegisterTokenizationProcessingMetrics()
base.RegisterDbProcessingMetrics()
cmd.RegisterVersionMetrics(serviceName, version)
cmd.RegisterBuildInfoMetrics(serviceName, edition)
Expand Down
91 changes: 79 additions & 12 deletions cmd/acra-translator/common/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ package common
import (
"context"
"errors"
"sync"

"github.com/prometheus/client_golang/prometheus"

"github.com/cossacklabs/acra/cmd"
"github.com/cossacklabs/acra/decryptor/base"
tokenCommon "github.com/cossacklabs/acra/pseudonymization/common"
"github.com/cossacklabs/acra/utils"
"github.com/prometheus/client_golang/prometheus"
"sync"
)

const (
Expand Down Expand Up @@ -92,6 +94,8 @@ func RegisterMetrics(serviceName string) {
prometheus.MustRegister(connectionProcessingTimeHistogram)
prometheus.MustRegister(RequestProcessingTimeHistogram)
base.RegisterAcraStructProcessingMetrics()
base.RegisterEncryptionDecryptionProcessingMetrics()
base.RegisterTokenizationProcessingMetrics()
version, err := utils.GetParsedVersion()
if err != nil {
panic(err)
Expand Down Expand Up @@ -122,28 +126,52 @@ func NewPrometheusServiceWrapper(service ITranslatorService, metricType string)
func (wrapper *prometheusWrapper) Decrypt(ctx context.Context, acraStruct, clientID, additionalContext []byte) ([]byte, error) {
timer := prometheus.NewTimer(prometheus.ObserverFunc(RequestProcessingTimeHistogram.WithLabelValues(wrapper.metricType, decryptOperation).Observe))
defer timer.ObserveDuration()
return wrapper.ITranslatorService.Decrypt(ctx, acraStruct, clientID, additionalContext)
decrypted, err := wrapper.ITranslatorService.Decrypt(ctx, acraStruct, clientID, additionalContext)
if err != nil {
base.AcraDecryptionCounter.WithLabelValues(base.LabelStatusFail, base.LabelTypeAcraStruct).Inc()
return nil, err
}
base.AcraDecryptionCounter.WithLabelValues(base.LabelStatusSuccess, base.LabelTypeAcraStruct).Inc()
return decrypted, nil
}

// Encrypt AcraStruct using ClientID
func (wrapper *prometheusWrapper) Encrypt(ctx context.Context, data, clientID, additionalContext []byte) ([]byte, error) {
timer := prometheus.NewTimer(prometheus.ObserverFunc(RequestProcessingTimeHistogram.WithLabelValues(wrapper.metricType, encryptOperation).Observe))
defer timer.ObserveDuration()
return wrapper.ITranslatorService.Encrypt(ctx, data, clientID, additionalContext)
encrypted, err := wrapper.ITranslatorService.Encrypt(ctx, data, clientID, additionalContext)
if err != nil {
base.AcraEncryptionCounter.WithLabelValues(base.LabelStatusFail, base.LabelTypeAcraStruct).Inc()
return nil, err
}
base.AcraEncryptionCounter.WithLabelValues(base.LabelStatusSuccess, base.LabelTypeAcraStruct).Inc()
return encrypted, nil
}

// EncryptSearchable generate AcraStruct using ClientID and searchable hash
func (wrapper *prometheusWrapper) EncryptSearchable(ctx context.Context, data, clientID, additionalContext []byte) (SearchableResponse, error) {
timer := prometheus.NewTimer(prometheus.ObserverFunc(RequestProcessingTimeHistogram.WithLabelValues(wrapper.metricType, encryptSearchableOperation).Observe))
defer timer.ObserveDuration()
return wrapper.ITranslatorService.EncryptSearchable(ctx, data, clientID, additionalContext)
encrypted, err := wrapper.ITranslatorService.EncryptSearchable(ctx, data, clientID, additionalContext)
if err != nil {
base.AcraEncryptionCounter.WithLabelValues(base.LabelStatusFail, base.LabelTypeAcraStructSearch).Inc()
return SearchableResponse{}, err
}
base.AcraEncryptionCounter.WithLabelValues(base.LabelStatusSuccess, base.LabelTypeAcraStructSearch).Inc()
return encrypted, nil
}

// DecryptSearchable decrypt AcraStruct using ClientID and then verify hash
func (wrapper *prometheusWrapper) DecryptSearchable(ctx context.Context, data, hash, clientID, additionalContext []byte) ([]byte, error) {
timer := prometheus.NewTimer(prometheus.ObserverFunc(RequestProcessingTimeHistogram.WithLabelValues(wrapper.metricType, decryptSearchableOperation).Observe))
defer timer.ObserveDuration()
return wrapper.ITranslatorService.DecryptSearchable(ctx, data, hash, clientID, additionalContext)
decrypted, err := wrapper.ITranslatorService.DecryptSearchable(ctx, data, hash, clientID, additionalContext)
if err != nil {
base.AcraDecryptionCounter.WithLabelValues(base.LabelStatusFail, base.LabelTypeAcraStructSearch).Inc()
return nil, err
}
base.AcraDecryptionCounter.WithLabelValues(base.LabelStatusSuccess, base.LabelTypeAcraStructSearch).Inc()
return decrypted, nil
}

// GenerateQueryHash generates searchable hash for data
Expand All @@ -157,40 +185,79 @@ func (wrapper *prometheusWrapper) GenerateQueryHash(ctx context.Context, data, c
func (wrapper *prometheusWrapper) Tokenize(ctx context.Context, data interface{}, dataType tokenCommon.TokenType, clientID, additionalContext []byte) (interface{}, error) {
timer := prometheus.NewTimer(prometheus.ObserverFunc(RequestProcessingTimeHistogram.WithLabelValues(wrapper.metricType, tokenizeOperation).Observe))
defer timer.ObserveDuration()
return wrapper.ITranslatorService.Tokenize(ctx, data, dataType, clientID, additionalContext)
tokenized, err := wrapper.ITranslatorService.Tokenize(ctx, data, dataType, clientID, additionalContext)
if err != nil {
base.AcraTokenizationCounter.WithLabelValues(base.LabelStatusFail, dataType.String()).Inc()
return nil, err
}

base.AcraTokenizationCounter.WithLabelValues(base.LabelStatusSuccess, dataType.String()).Inc()
return tokenized, nil
}

// Detokenize data from request according to TokenType using ClientID
func (wrapper *prometheusWrapper) Detokenize(ctx context.Context, data interface{}, dataType tokenCommon.TokenType, clientID, additionalContext []byte) (interface{}, error) {
timer := prometheus.NewTimer(prometheus.ObserverFunc(RequestProcessingTimeHistogram.WithLabelValues(wrapper.metricType, detokenizeOperation).Observe))
defer timer.ObserveDuration()
return wrapper.ITranslatorService.Detokenize(ctx, data, dataType, clientID, additionalContext)
detokenized, err := wrapper.ITranslatorService.Detokenize(ctx, data, dataType, clientID, additionalContext)
if err != nil {
base.AcraDetokenizationCounter.WithLabelValues(base.LabelStatusFail, dataType.String()).Inc()
return nil, err
}

base.AcraDetokenizationCounter.WithLabelValues(base.LabelStatusSuccess, dataType.String()).Inc()
return detokenized, nil
}

// EncryptSymSearchable encrypts data with AcraBlock using ClientID and searchable hash
func (wrapper *prometheusWrapper) EncryptSymSearchable(ctx context.Context, data, clientID, additionalContext []byte) (SearchableResponse, error) {
timer := prometheus.NewTimer(prometheus.ObserverFunc(RequestProcessingTimeHistogram.WithLabelValues(wrapper.metricType, encryptSymSearchableOperation).Observe))
defer timer.ObserveDuration()
return wrapper.ITranslatorService.EncryptSymSearchable(ctx, data, clientID, additionalContext)
encrypted, err := wrapper.ITranslatorService.EncryptSymSearchable(ctx, data, clientID, additionalContext)
if err != nil {
base.AcraEncryptionCounter.WithLabelValues(base.LabelStatusFail, base.LabelTypeAcraBlockSearch).Inc()
return SearchableResponse{}, err
}
base.AcraEncryptionCounter.WithLabelValues(base.LabelStatusSuccess, base.LabelTypeAcraBlockSearch).Inc()
return encrypted, nil
}

// DecryptSymSearchable decrypt AcraBlock using ClientID and verify hash
func (wrapper *prometheusWrapper) DecryptSymSearchable(ctx context.Context, data, hash, clientID, additionalContext []byte) ([]byte, error) {
timer := prometheus.NewTimer(prometheus.ObserverFunc(RequestProcessingTimeHistogram.WithLabelValues(wrapper.metricType, decryptSymSearchableOperation).Observe))
defer timer.ObserveDuration()
return wrapper.ITranslatorService.DecryptSymSearchable(ctx, data, hash, clientID, additionalContext)
decrypted, err := wrapper.ITranslatorService.DecryptSymSearchable(ctx, data, hash, clientID, additionalContext)
if err != nil {
base.AcraDecryptionCounter.WithLabelValues(base.LabelStatusFail, base.LabelTypeAcraBlockSearch).Inc()
return nil, err
}
base.AcraDecryptionCounter.WithLabelValues(base.LabelStatusSuccess, base.LabelTypeAcraBlockSearch).Inc()
return decrypted, nil
}

// EncryptSym encrypts data with AcraBlock using ClientID
func (wrapper *prometheusWrapper) EncryptSym(ctx context.Context, data, clientID, additionalContext []byte) ([]byte, error) {
timer := prometheus.NewTimer(prometheus.ObserverFunc(RequestProcessingTimeHistogram.WithLabelValues(wrapper.metricType, encryptSymOperation).Observe))
defer timer.ObserveDuration()
return wrapper.ITranslatorService.EncryptSym(ctx, data, clientID, additionalContext)
encrypted, err := wrapper.ITranslatorService.EncryptSym(ctx, data, clientID, additionalContext)
if err != nil {
base.AcraEncryptionCounter.WithLabelValues(base.LabelStatusFail, base.LabelTypeAcraBlock).Inc()
return nil, err
}
base.AcraEncryptionCounter.WithLabelValues(base.LabelStatusSuccess, base.LabelTypeAcraBlock).Inc()
return encrypted, nil
}

// DecryptSym decrypts AcraBlock using ClientID
func (wrapper *prometheusWrapper) DecryptSym(ctx context.Context, acraBlock, clientID, additionalContext []byte) ([]byte, error) {
timer := prometheus.NewTimer(prometheus.ObserverFunc(RequestProcessingTimeHistogram.WithLabelValues(wrapper.metricType, decryptSymOperation).Observe))
defer timer.ObserveDuration()
return wrapper.ITranslatorService.DecryptSym(ctx, acraBlock, clientID, additionalContext)
decrypted, err := wrapper.ITranslatorService.DecryptSym(ctx, acraBlock, clientID, additionalContext)
if err != nil {
base.AcraDecryptionCounter.WithLabelValues(base.LabelStatusFail, base.LabelTypeAcraBlock).Inc()
return nil, err
}

base.AcraDecryptionCounter.WithLabelValues(base.LabelStatusSuccess, base.LabelTypeAcraBlock).Inc()
return decrypted, nil
}
18 changes: 11 additions & 7 deletions cmd/acra-translator/common/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package common
import (
"context"
"errors"

"github.com/sirupsen/logrus"

"github.com/cossacklabs/acra/crypto"
"github.com/cossacklabs/acra/decryptor/base"
"github.com/cossacklabs/acra/hmac"
"github.com/cossacklabs/acra/logging"
tokenCommon "github.com/cossacklabs/acra/pseudonymization/common"
"github.com/sirupsen/logrus"
)

// ITranslatorService interface introduce all supported methods by Acra-Translator
Expand Down Expand Up @@ -81,7 +83,8 @@ func (service *TranslatorService) Decrypt(ctx context.Context, acraStruct, clien

data, decryptErr := service.handler.DecryptWithHandler(handler, acraStruct, dataContext)
if decryptErr != nil {
base.AcrastructDecryptionCounter.WithLabelValues(base.DecryptionTypeFail).Inc()
//TODO: remove deprecated metrics in 1-2 versions
base.AcrastructDecryptionCounter.WithLabelValues(base.LabelStatusFail).Inc()
Zhaars marked this conversation as resolved.
Show resolved Hide resolved
logger.WithField(logging.FieldKeyEventCode, logging.EventCodeErrorTranslatorCantDecryptAcraStruct).WithError(decryptErr).Errorln("Can't decrypt AcraStruct")
_, _, err = service.poisonDetector.OnColumn(dataCtx, acraStruct)
if err != nil {
Expand All @@ -91,7 +94,8 @@ func (service *TranslatorService) Decrypt(ctx context.Context, acraStruct, clien
// don't show users that we found poison record
return nil, ErrCantDecrypt
}
base.AcrastructDecryptionCounter.WithLabelValues(base.DecryptionTypeSuccess).Inc()
//TODO: remove deprecated metrics in 1-2 versions
base.AcrastructDecryptionCounter.WithLabelValues(base.LabelStatusSuccess).Inc()
return data, nil
}

Expand All @@ -113,7 +117,6 @@ func (service *TranslatorService) Encrypt(ctx context.Context, data, clientID, a
id := clientID
handler, err := crypto.GetHandlerByEnvelopeID(crypto.AcraStructEnvelopeID)
if err != nil {
base.AcrastructDecryptionCounter.WithLabelValues(base.EncryptionTypeFail).Inc()
logger.
WithField(logging.FieldKeyEventCode, logging.EventCodeErrorTranslatorCantDecryptAcraStruct).
WithError(err).
Expand All @@ -125,12 +128,10 @@ func (service *TranslatorService) Encrypt(ctx context.Context, data, clientID, a

data, encryptErr := service.handler.EncryptWithHandler(handler, id, data)
if encryptErr != nil {
base.AcrastructDecryptionCounter.WithLabelValues(base.EncryptionTypeFail).Inc()
logger.WithField(logging.FieldKeyEventCode, logging.EventCodeErrorTranslatorCantDecryptAcraStruct).WithError(encryptErr).Errorln("Can't encrypt data")
// don't show users that we found poison record
return nil, ErrCantEncrypt
}
base.AcrastructDecryptionCounter.WithLabelValues(base.EncryptionTypeSuccess).Inc()
return data, nil
}

Expand Down Expand Up @@ -462,7 +463,8 @@ func (service *TranslatorService) DecryptSym(ctx context.Context, acraBlock, cli

decrypted, err := service.handler.DecryptWithHandler(handler, acraBlock, dataContext)
if err != nil {
base.AcrastructDecryptionCounter.WithLabelValues(base.DecryptionTypeFail).Inc()
//TODO: remove deprecated metrics in 1-2 versions
base.AcrastructDecryptionCounter.WithLabelValues(base.LabelStatusFail).Inc()
logger.WithField(logging.FieldKeyEventCode, logging.EventCodeErrorTranslatorCantDecryptAcraBlock).WithError(err).Errorln("Can't decrypt AcraBlock")
_, _, poisonErr := service.poisonDetector.OnColumn(dataCtx, acraBlock)
if poisonErr != nil {
Expand All @@ -471,5 +473,7 @@ func (service *TranslatorService) DecryptSym(ctx context.Context, acraBlock, cli
}
return acraBlock, ErrCantDecrypt
}
//TODO: remove deprecated metrics in 1-2 versions
base.AcrastructDecryptionCounter.WithLabelValues(base.LabelStatusSuccess).Inc()
return decrypted, nil
}
10 changes: 7 additions & 3 deletions cmd/acra-translator/grpc_api/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ package grpc_api

import (
"errors"

"github.com/sirupsen/logrus"
"golang.org/x/net/context"

"github.com/cossacklabs/acra/acrablock"
"github.com/cossacklabs/acra/cmd/acra-translator/common"
"github.com/cossacklabs/acra/decryptor/base"
"github.com/cossacklabs/acra/hmac"
"github.com/cossacklabs/acra/logging"
tokenCommon "github.com/cossacklabs/acra/pseudonymization/common"
"github.com/sirupsen/logrus"
"golang.org/x/net/context"
)

// ErrEmptyClientID error used if ClientID required in request but not provided
Expand Down Expand Up @@ -71,11 +73,13 @@ func (service *TranslatorService) Encrypt(ctx context.Context, request *EncryptR

response, err := service.service.Encrypt(ctx, request.Data, request.ClientId, nil)
if err != nil {
base.APIEncryptionCounter.WithLabelValues(base.EncryptionTypeFail).Inc()
Zhaars marked this conversation as resolved.
Show resolved Hide resolved
base.APIEncryptionCounter.WithLabelValues(base.LabelStatusFail).Inc()
msg := "Unexpected error with AcraStruct generation"
logger.WithError(err).WithField(logging.FieldKeyEventCode, logging.EventCodeErrorCantEncryptData).Warningln(msg)
return nil, err
}

base.APIEncryptionCounter.WithLabelValues(base.LabelStatusSuccess).Inc()
return &EncryptResponse{Acrastruct: response}, nil
}

Expand Down
Loading