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

Add initial code instrumentation with honeycomb #15

Merged
merged 1 commit into from
Mar 7, 2024
Merged

Add initial code instrumentation with honeycomb #15

merged 1 commit into from
Mar 7, 2024

Conversation

vaijab
Copy link
Contributor

@vaijab vaijab commented Mar 4, 2024

Description

Changes proposed in this pull request:

  • Add initial code instrumentation with honeycomb

Related issue(s)

@vaijab vaijab force-pushed the otel branch 2 times, most recently from f81c9ff to 3853ec1 Compare March 4, 2024 12:28
@vaijab vaijab marked this pull request as ready for review March 4, 2024 13:06
@vaijab vaijab requested review from a team and pkosiec March 4, 2024 13:06
Copy link
Collaborator

@pkosiec pkosiec left a comment

Choose a reason for hiding this comment

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

overall it is OK, but please create a missing PR and test it E2E 👍 Thanks!

internal/source/ai-brain/config_schema.json Outdated Show resolved Hide resolved
internal/source/ai-brain/config_schema.json Outdated Show resolved Hide resolved
@@ -83,6 +88,14 @@ func (s *Source) Stream(ctx context.Context, in source.StreamInput) (source.Stre

sourceName := in.Context.SourceName

// Set up opentelemetry with honeycomb if enabled.
if cfg.HoneycombAPIKey != "" {
otelServiceName := "plugins-source-" + sourceName
Copy link
Collaborator

@pkosiec pkosiec Mar 4, 2024

Choose a reason for hiding this comment

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

We cannot assume this will be unique in the whole Botkube Cloud. Probably there could be some duplication between orgs as we randomize just 4-5 chars AFAIR. Is it good enough for our use case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What would you suggest we change it to?

Copy link
Collaborator

Choose a reason for hiding this comment

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

i just wanted to double check if that's OK - personally I think that should be good enough. If not, then we'd probably need to randomize it or use kube-system ns uid, like with telemetry 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

service name property is used as a dataset name in honeycomb, so it's quite important I'd say. We don't want to have a gazillion datasets in honeycomb as that would be quite tricky to query. Maybe botkube-plugins-source-ai-brain?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Gotcha, so the goal is to group them all under a single static service name. Then yes, botkube-plugins-source-ai-brain or plugins-source-ai-brain sound good 👍

@vaijab vaijab force-pushed the otel branch 4 times, most recently from a822b58 to 15797c4 Compare March 7, 2024 12:32
@@ -99,14 +101,14 @@ func (s *Source) Stream(ctx context.Context, in source.StreamInput) (source.Stre
}

// HandleExternalRequest handles incoming payload and returns an event based on it.
func (s *Source) HandleExternalRequest(_ context.Context, in source.ExternalRequestInput) (source.ExternalRequestOutput, error) {
func (s *Source) HandleExternalRequest(ctx context.Context, in source.ExternalRequestInput) (source.ExternalRequestOutput, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
func (s *Source) HandleExternalRequest(ctx context.Context, in source.ExternalRequestInput) (source.ExternalRequestOutput, error) {
func (s *Source) HandleExternalRequest(- context.Context, in source.ExternalRequestInput) (source.ExternalRequestOutput, error) {

Comment on lines 130 to 140
func (i *assistant) handleThread(ctx context.Context, p *Payload) error {
ctx, span := i.tracer.Start(ctx, "aibrain.assistant.handleThread")
defer span.End()

var thread openai.Thread
var err error

defer func() {
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
}
}()
Copy link
Collaborator

Choose a reason for hiding this comment

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

in order to make it work in all cases, you need to introduce named return param instead of var 👍

Suggested change
func (i *assistant) handleThread(ctx context.Context, p *Payload) error {
ctx, span := i.tracer.Start(ctx, "aibrain.assistant.handleThread")
defer span.End()
var thread openai.Thread
var err error
defer func() {
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
}
}()
func (i *assistant) handleThread(ctx context.Context, p *Payload) (err error) {
ctx, span := i.tracer.Start(ctx, "aibrain.assistant.handleThread")
defer span.End()
var thread openai.Thread
defer func() {
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
}
}()

I double-checked that with this unit-test:

Details

func TestNamedParams(t *testing.T) {
	t.Log("here we will catch the error as it's assigned to the 'var err' before returning")
	_ = handleThread(t, false)
	t.Log("here we won't catch the error as it's directly returned")
	_ = handleThread(t, true)

	t.Log("named return params - this will work")
	_ = handleThreadV2(t, true)
}

func handleThread(t *testing.T, directFnReturn bool) error {
	var err error

	defer func() {
		if err != nil {
			t.Log("Error on defer:", err)
		}
	}()

	if !directFnReturn {
		err = errors.New("some error, directFnReturn = false")
		if err != nil {
			return fmt.Errorf("while creating a new message on a thread: %w", err)
		}
	}

	return returnError()
}

func handleThreadV2(t *testing.T, directFnReturn bool) (err error) {
	defer func() {
		if err != nil {
			t.Log("Error on defer:", err)
		}
	}()

	if !directFnReturn {
		err = errors.New("some error, directFnReturn = false")
		if err != nil {
			return fmt.Errorf("while creating a new message on a thread: %w", err)
		}
	}

	return returnError()
}

func returnError() error {
	return errors.New("some error, directFnReturn = true")
}

@vaijab vaijab merged commit c666eab into main Mar 7, 2024
4 checks passed
@vaijab vaijab deleted the otel branch March 7, 2024 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants