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

[Wf-Diagnostics] introduce emitter interface in w/f diagnostics #6309

Merged
merged 2 commits into from
Sep 26, 2024
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
74 changes: 74 additions & 0 deletions service/worker/diagnostics/analytics/emitter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// The MIT License (MIT)

// Copyright (c) 2017-2020 Uber Technologies Inc.

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package analytics

import (
"context"
"encoding/json"

"github.com/uber/cadence/.gen/go/indexer"
"github.com/uber/cadence/common"
"github.com/uber/cadence/common/messaging"
)

type Emitter DataEmitter

type emitter struct {
producer messaging.Producer
}

type EmitterParams struct {
Producer messaging.Producer
}

func NewEmitter(p EmitterParams) DataEmitter {
return &emitter{
producer: p.Producer,
}
}

func (et *emitter) EmitUsageData(ctx context.Context, data WfDiagnosticsUsageData) error {
msg := make(map[string]interface{})
msg[Domain] = data.Domain
msg[WorkflowID] = data.WorkflowID
msg[RunID] = data.RunID
msg[Identity] = data.Identity
msg[SatisfactionFeedback] = data.SatisfactionFeedback
msg[IssueType] = data.IssueType
msg[DiagnosticsWfID] = data.DiagnosticsWorkflowID
msg[DiagnosticsWfRunID] = data.DiagnosticsRunID
msg[Environment] = data.Environment
msg[DiagnosticsStartTime] = data.DiagnosticsStartTime
msg[DiagnosticsEndTime] = data.DiagnosticsEndTime

serializedMsg, err := json.Marshal(msg)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just serialize WfDiagnosticsUsageData? It that to have some compatibility?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

am following whats done in visibility. will double check with Neil too

if err != nil {
return err
}

pinotMsg := &indexer.PinotMessage{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this layer be visibility-store agnostic?

WorkflowID: common.StringPtr(data.DiagnosticsWorkflowID),
Payload: serializedMsg,
}
return et.producer.Publish(ctx, pinotMsg)
}
97 changes: 97 additions & 0 deletions service/worker/diagnostics/analytics/emitter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// The MIT License (MIT)

// Copyright (c) 2017-2020 Uber Technologies Inc.

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package analytics

import (
"context"
"errors"
"testing"
"time"

"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

"github.com/uber/cadence/.gen/go/indexer"
"github.com/uber/cadence/common/mocks"
)

func Test__EmitUsageData(t *testing.T) {

testdata := WfDiagnosticsUsageData{
Domain: "test-domain",
WorkflowID: "wid",
RunID: "rid",
Identity: "test@uber.com",
IssueType: "timeouts",
DiagnosticsWorkflowID: "diagnostics-wid",
DiagnosticsRunID: "diagnostics-rid",
Environment: "test-env",
DiagnosticsStartTime: time.Now(),
DiagnosticsEndTime: time.Now().Add(1 * time.Minute),
SatisfactionFeedback: false,
}
mockErr := errors.New("mockErr")
tests := map[string]struct {
data WfDiagnosticsUsageData
producerMockAffordance func(mockProducer *mocks.KafkaProducer)
expectedError error
}{
"Case1: normal case": {
data: testdata,
producerMockAffordance: func(mockProducer *mocks.KafkaProducer) {
mockProducer.On("Publish", mock.Anything, mock.MatchedBy(func(input *indexer.PinotMessage) bool {
require.Equal(t, testdata.DiagnosticsWorkflowID, input.GetWorkflowID())
return true
})).Return(nil).Once()
},
expectedError: nil,
},
"Case1: error case": {
data: testdata,
producerMockAffordance: func(mockProducer *mocks.KafkaProducer) {
mockProducer.On("Publish", mock.Anything, mock.MatchedBy(func(input *indexer.PinotMessage) bool {
require.Equal(t, testdata.DiagnosticsWorkflowID, input.GetWorkflowID())
return true
})).Return(mockErr).Once()
},
expectedError: mockErr,
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
mockProducer := &mocks.KafkaProducer{}
emitter := NewEmitter(EmitterParams{
Producer: mockProducer,
})
test.producerMockAffordance(mockProducer)

err := emitter.EmitUsageData(context.Background(), test.data)
if test.expectedError != nil {
require.Error(t, err)
} else {
require.NoError(t, err)
}
})
}
}
47 changes: 47 additions & 0 deletions service/worker/diagnostics/analytics/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// The MIT License (MIT)

// Copyright (c) 2017-2020 Uber Technologies Inc.

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package analytics

import (
"context"
"time"
)

type WfDiagnosticsUsageData struct {
Domain string
WorkflowID string
RunID string
Identity string
IssueType string
DiagnosticsWorkflowID string
DiagnosticsRunID string
Environment string
DiagnosticsStartTime time.Time
DiagnosticsEndTime time.Time
SatisfactionFeedback bool
}

// DataEmitter is the interface to emit workflow diagnostics data
type DataEmitter interface {
EmitUsageData(context.Context, WfDiagnosticsUsageData) error
}
37 changes: 37 additions & 0 deletions service/worker/diagnostics/analytics/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// The MIT License (MIT)

// Copyright (c) 2017-2020 Uber Technologies Inc.

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package analytics

const (
Domain = "domain"
WorkflowID = "workflowID"
RunID = "runID"
Identity = "identity"
IssueType = "issue_type"
DiagnosticsWfID = "diagnostics_workflowID"
DiagnosticsWfRunID = "diagnostics_workflow_runID"
SatisfactionFeedback = "satisfaction_feedback"
Environment = "environment"
DiagnosticsStartTime = "diagnostics_start_time"
DiagnosticsEndTime = "diagnostics_end_time"
)
Loading