Skip to content

Commit

Permalink
Add Datadog tracing to HTTP server and transformation functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhargav Dodla committed Mar 13, 2024
1 parent be6b236 commit 9bcdcfc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
5 changes: 5 additions & 0 deletions go/internal/feast/featurestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/apache/arrow/go/v8/arrow/memory"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"

"github.com/feast-dev/feast/go/internal/feast/model"
"github.com/feast-dev/feast/go/internal/feast/onlineserving"
Expand Down Expand Up @@ -314,6 +315,10 @@ func (fs *FeatureStore) readFromOnlineStore(ctx context.Context, entityRows []*p
requestedFeatureViewNames []string,
requestedFeatureNames []string,
) ([][]onlinestore.FeatureData, error) {
// Create a Datadog span from context
span, _ := tracer.StartSpanFromContext(ctx, "fs.readFromOnlineStore")
defer span.Finish()

numRows := len(entityRows)
entityRowsValue := make([]*prototypes.EntityKey, numRows)
for index, entityKey := range entityRows {
Expand Down
41 changes: 30 additions & 11 deletions go/internal/feast/server/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,37 +147,45 @@ func NewHttpServer(fs *feast.FeatureStore, loggingService *logging.LoggingServic
}

func (s *httpServer) getOnlineFeatures(w http.ResponseWriter, r *http.Request) {
var err error

// if strings.ToLower(os.Getenv("ENABLE_DATADOG_TRACING")) == "true" {
span, ctx := tracer.StartSpanFromContext(r.Context(), "getOnlineFeatures", tracer.ResourceName("/get-online-features"))
defer span.Finish(tracer.WithError(err))
// }

if r.Method != "POST" {
http.NotFound(w, r)
return
}

statusQuery := r.URL.Query().Get("status")

status := false
if statusQuery != "" {
var err error
status, err = strconv.ParseBool(statusQuery)
if err != nil {
log.Error().Err(err).Msg("Error parsing status query parameter")
http.Error(w, fmt.Sprintf("Error parsing status query parameter: %+v", err), http.StatusBadRequest)
writeJSONError(w, fmt.Errorf("Error parsing status query parameter: %+v", err), http.StatusBadRequest)
return
}
}

decoder := json.NewDecoder(r.Body)
var request getOnlineFeaturesRequest
err := decoder.Decode(&request)
err = decoder.Decode(&request)
if err != nil {
log.Error().Err(err).Msg("Error decoding JSON request data")
http.Error(w, fmt.Sprintf("Error decoding JSON request data: %+v", err), http.StatusInternalServerError)
writeJSONError(w, fmt.Errorf("Error decoding JSON request data: %+v", err), http.StatusInternalServerError)
return
}
var featureService *model.FeatureService
if request.FeatureService != nil {
featureService, err = s.fs.GetFeatureService(*request.FeatureService)
if err != nil {
log.Error().Err(err).Msg("Error getting feature service from registry")
http.Error(w, fmt.Sprintf("Error getting feature service from registry: %+v", err), http.StatusInternalServerError)
writeJSONError(w, fmt.Errorf("Error getting feature service from registry: %+v", err), http.StatusInternalServerError)
return
}
}
Expand All @@ -191,7 +199,7 @@ func (s *httpServer) getOnlineFeatures(w http.ResponseWriter, r *http.Request) {
}

featureVectors, err := s.fs.GetOnlineFeatures(
r.Context(),
ctx,
request.Features,
featureService,
entitiesProto,
Expand All @@ -200,7 +208,7 @@ func (s *httpServer) getOnlineFeatures(w http.ResponseWriter, r *http.Request) {

if err != nil {
log.Error().Err(err).Msg("Error getting feature vector")
http.Error(w, fmt.Sprintf("Error getting feature vector: %+v", err), http.StatusInternalServerError)
writeJSONError(w, fmt.Errorf("Error getting feature vector: %+v", err), http.StatusInternalServerError)
return
}

Expand Down Expand Up @@ -242,15 +250,15 @@ func (s *httpServer) getOnlineFeatures(w http.ResponseWriter, r *http.Request) {

if err != nil {
log.Error().Err(err).Msg("Error encoding response")
http.Error(w, fmt.Sprintf("Error encoding response: %+v", err), http.StatusInternalServerError)
writeJSONError(w, fmt.Errorf("Error encoding response: %+v", err), http.StatusInternalServerError)
return
}

if featureService != nil && featureService.LoggingConfig != nil && s.loggingService != nil {
logger, err := s.loggingService.GetOrCreateLogger(featureService)
if err != nil {
log.Error().Err(err).Msgf("Couldn't instantiate logger for feature service %s", featureService.Name)
http.Error(w, fmt.Sprintf("Couldn't instantiate logger for feature service %s: %+v", featureService.Name, err), http.StatusInternalServerError)
writeJSONError(w, fmt.Errorf("Couldn't instantiate logger for feature service %s: %+v", featureService.Name, err), http.StatusInternalServerError)
return
}

Expand All @@ -263,7 +271,7 @@ func (s *httpServer) getOnlineFeatures(w http.ResponseWriter, r *http.Request) {
values, err := types.ArrowValuesToProtoValues(vector.Values)
if err != nil {
log.Error().Err(err).Msg("Couldn't convert arrow values into protobuf")
http.Error(w, fmt.Sprintf("Couldn't convert arrow values into protobuf: %+v", err), http.StatusInternalServerError)
writeJSONError(w, fmt.Errorf("Couldn't convert arrow values into protobuf: %+v", err), http.StatusInternalServerError)
return
}
featureVectorProtos = append(featureVectorProtos, &serving.GetOnlineFeaturesResponse_FeatureVector{
Expand All @@ -275,7 +283,7 @@ func (s *httpServer) getOnlineFeatures(w http.ResponseWriter, r *http.Request) {

err = logger.Log(entitiesProto, featureVectorProtos, featureNames[len(request.Entities):], requestContextProto, requestId)
if err != nil {
http.Error(w, fmt.Sprintf("LoggerImpl error[%s]: %+v", featureService.Name, err), http.StatusInternalServerError)
writeJSONError(w, fmt.Errorf("LoggerImpl error[%s]: %+v", featureService.Name, err), http.StatusInternalServerError)
return
}
}
Expand Down Expand Up @@ -304,6 +312,18 @@ func logStackTrace() {
}
}

func writeJSONError(w http.ResponseWriter, err error, statusCode int) {
errMap := map[string]interface{}{
"error": fmt.Sprintf("%+v", err),
"status": statusCode,
}
errJSON, _ := json.Marshal(errMap)

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
w.Write(errJSON)
}

func recoverMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
Expand All @@ -321,7 +341,6 @@ func recoverMiddleware(next http.Handler) http.Handler {

func (s *httpServer) Serve(host string, port int) error {
if strings.ToLower(os.Getenv("ENABLE_DATADOG_TRACING")) == "true" {

tracer.Start(tracer.WithRuntimeMetrics())
defer tracer.Stop()
}
Expand Down
4 changes: 4 additions & 0 deletions go/internal/feast/transformation/transformation.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/apache/arrow/go/v8/arrow/memory"
"github.com/rs/zerolog/log"
"google.golang.org/protobuf/types/known/timestamppb"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"

"github.com/feast-dev/feast/go/internal/feast/model"
"github.com/feast-dev/feast/go/internal/feast/onlineserving"
Expand Down Expand Up @@ -45,6 +46,9 @@ func AugmentResponseWithOnDemandTransforms(
fullFeatureNames bool,

) ([]*onlineserving.FeatureVector, error) {
span, _ := tracer.StartSpanFromContext(ctx, "transformation.AugmentResponseWithOnDemandTransforms")
defer span.Finish()

result := make([]*onlineserving.FeatureVector, 0)
var err error

Expand Down

0 comments on commit 9bcdcfc

Please sign in to comment.