Skip to content

Commit

Permalink
fix json unmarshal and remove connection test event from untestable d…
Browse files Browse the repository at this point in the history
…atastores (#2324)

* fix json unmarshal

* fix test

* split TraceDB and TestableTraceDB

* add TraceDB as part of TestableTraceDB
  • Loading branch information
mathnogueira committed Apr 6, 2023
1 parent dd8be94 commit 2cfc88a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 13 deletions.
14 changes: 9 additions & 5 deletions server/executor/poller_executor.go
Expand Up @@ -109,11 +109,13 @@ func (pe DefaultPollerExecutor) ExecuteRequest(request *PollingRequest) (bool, s
}

if request.IsFirstRequest() {
connectionResult := traceDB.TestConnection(request.ctx)
if testableTraceDB, ok := traceDB.(tracedb.TestableTraceDB); ok {
connectionResult := testableTraceDB.TestConnection(request.ctx)

err = pe.eventEmitter.Emit(request.ctx, events.TraceDataStoreConnectionInfo(request.test.ID, request.run.ID, connectionResult))
if err != nil {
log.Printf("[PollerExecutor] Test %s Run %d: failed to emit TraceDataStoreConnectionInfo event: error: %s\n", request.test.ID, request.run.ID, err.Error())
err = pe.eventEmitter.Emit(request.ctx, events.TraceDataStoreConnectionInfo(request.test.ID, request.run.ID, connectionResult))
if err != nil {
log.Printf("[PollerExecutor] Test %s Run %d: failed to emit TraceDataStoreConnectionInfo event: error: %s\n", request.test.ID, request.run.ID, err.Error())
}
}
}

Expand All @@ -129,7 +131,9 @@ func (pe DefaultPollerExecutor) ExecuteRequest(request *PollingRequest) (bool, s

if !errors.Is(err, connection.ErrTraceNotFound) {
// run test connection to give a diagnostic when an unknown error happens
connectionResult = traceDB.TestConnection(request.ctx)
if testableTraceDB, ok := traceDB.(tracedb.TestableTraceDB); ok {
connectionResult = testableTraceDB.TestConnection(request.ctx)
}
}

anotherErr := pe.eventEmitter.Emit(request.ctx, events.TraceFetchingError(request.test.ID, request.run.ID, connectionResult, err))
Expand Down
11 changes: 8 additions & 3 deletions server/http/controller.go
Expand Up @@ -1274,10 +1274,15 @@ func (c *controller) TestConnection(ctx context.Context, dataStore openapi.DataS
return openapi.Response(http.StatusBadRequest, err.Error()), err
}

testResult := tdb.TestConnection(ctx)
testResult := model.ConnectionResult{}
statusCode := http.StatusOK
if !testResult.HasSucceed() {
statusCode = http.StatusUnprocessableEntity

if testableTraceDB, ok := tdb.(tracedb.TestableTraceDB); ok {
testResult = testableTraceDB.TestConnection(ctx)
statusCode = http.StatusOK
if !testResult.HasSucceed() {
statusCode = http.StatusUnprocessableEntity
}
}

return openapi.Response(statusCode, c.mappers.Out.ConnectionTestResult(testResult)), nil
Expand Down
28 changes: 28 additions & 0 deletions server/model/connection_test_result.go
@@ -1,5 +1,10 @@
package model

import (
"encoding/json"
"errors"
)

type ConnectionResult struct {
PortCheck ConnectionTestStep
Connectivity ConnectionTestStep
Expand All @@ -18,6 +23,29 @@ type ConnectionTestStep struct {
Error error
}

func (s *ConnectionTestStep) UnmarshalJSON(bytes []byte) error {
var step struct {
Passed bool
Status Status
Message string
ErrorMessage string
}

err := json.Unmarshal(bytes, &step)
if err != nil {
return err
}

s.Passed = step.Passed
s.Status = step.Status
s.Message = step.Message
if step.ErrorMessage != "" {
s.Error = errors.New(step.ErrorMessage)
}

return nil
}

func (r *ConnectionTestStep) HasSucceed() bool {
if r == nil {
return true
Expand Down
4 changes: 0 additions & 4 deletions server/tracedb/otlp.go
Expand Up @@ -33,10 +33,6 @@ func (tdb *OTLPTraceDB) Close() error {
return nil
}

func (jtd *OTLPTraceDB) TestConnection(ctx context.Context) model.ConnectionResult {
return model.ConnectionResult{}
}

// GetTraceByID implements TraceDB
func (tdb *OTLPTraceDB) GetTraceByID(ctx context.Context, id string) (model.Trace, error) {
run, err := tdb.db.GetRunByTraceID(ctx, traces.DecodeTraceID(id))
Expand Down
6 changes: 5 additions & 1 deletion server/tracedb/tracedb.go
Expand Up @@ -17,10 +17,14 @@ type TraceDB interface {
ShouldRetry() bool
GetTraceID() trace.TraceID
GetTraceByID(ctx context.Context, traceID string) (model.Trace, error)
TestConnection(ctx context.Context) model.ConnectionResult
Close() error
}

type TestableTraceDB interface {
TraceDB
TestConnection(ctx context.Context) model.ConnectionResult
}

type noopTraceDB struct{}

func (db *noopTraceDB) GetTraceByID(ctx context.Context, traceID string) (t model.Trace, err error) {
Expand Down

0 comments on commit 2cfc88a

Please sign in to comment.