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

support to post in thread #6

Merged
merged 3 commits into from
May 5, 2024
Merged

support to post in thread #6

merged 3 commits into from
May 5, 2024

Conversation

goro9
Copy link
Contributor

@goro9 goro9 commented May 3, 2024

Hello,

I added an option to post in the form of a thread reply, which I thought might be useful in an environment where multiple batches run in parallel.

Please review the changes and let me know if you have any questions or suggestions. Looking forward to your feedback.

handler.go Outdated
@@ -35,6 +35,12 @@ type Option struct {
// optional: see slog.HandlerOptions
AddSource bool
ReplaceAttr func(groups []string, a slog.Attr) slog.Attr

// optional: post in thread for the specified timestamp
ThreadTimestamp string
Copy link
Owner

Choose a reason for hiding this comment

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

Can you explain the choice of setting this at the handler level, instead of record context ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For example, if tasks a and b are executed at approximately the same time, I would like to separate the threads for each task and log output as follows.

image

In this case, I would like to specify the same ThreadTimestamp throughout the tasks, so I thought it would be better to specify it in the handler option.
(On the other hand, I couldn't think of a situation where we would want to switch between thread replies and direct posting to a channel on a record-by-record basis.)
However, it may be an unusual use, so I assume that I may use the fork version.

Copy link
Owner

@samber samber May 3, 2024

Choose a reason for hiding this comment

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

I really like the idea, but starting a new logger for every thread seems very odd to me.

What about something like this ?

ctx := context.WithValue(context.Background(), "threadId", time.Now())

logger.InfoContext(ctx, "start task a")
logger.ErrorContext(ctx, "task failed")
func (SlackHandler) Handler(ctx context.Context, r *slog.Record) {
    threadId, ok := ctx.Value("threadId").(time.Time)
    // ...
}

We need to check if using a threadId that does not exist is ok for slack API, but it makes the code simpler IMO.

This is very similar to the traceId+spanId proposed by OTEL.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Certainly could be odd given the logger usage.
How about this fix? c598687
I checked the operation like this

Copy link
Owner

Choose a reason for hiding this comment

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

Thanks, this is much better.

Unfortunately, I made a test myself and Slack does not allow thread creation when pushing the first message with a not existing thread timestamp.

A few notes:

  • Slack does not return the thread timestamp in the response to the "incoming webhook" (only in response to the REST API).
  • Having to call the Slack API first, to generate that thread id seems a bad developer experience.
  • It does not work if your app is replicated in production, as you cannot guess the thread id generated in another instance

I'm starting to think your first idea was much better: a hardcoded thread id, at the logger option level. No thread creation at runtime. Just push a message to the end. It would be a very limited use case, but still valid.

Any opinion on this ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As you say, the timestamp of the root post must exist in order for a thread to be replied to.
Even when multiple replying to the same thread, there is only one timestamp to specify, the root one.
There seems to have been a discrepancy in this understanding. My apologies.

Slack does not return the thread timestamp in the response to the "incoming webhook" (only in response to the REST API).

As you say, we cannot get the timestamp in the webhook response, so we need to GET it separately.
Since it is difficult to take care of this in this package, I thought it is better to make it the responsibility of the main application to obtain the timestamp.

Having to call the Slack API first, to generate that thread id seems a bad developer experience.

As mentioned earlier, the main application must call the Slack API at least once to obtain a timestamp.
Personally, I consider it acceptable if it is only once when generating loggers.

It does not work if your app is replicated in production, as you cannot guess the thread id generated in another instance

I have no problem with the behavior of threads being generated per replicated application for the use case I'm envisioning.

I wrote my use case in code on how to specify the thread timestamp in the handler options
I would like to output generic logs such as start, end, etc. during task execution using middleware, etc., and then output logs to slack as needed in the main process.
goro9/zatta-cmd@c45b340
image

Copy link
Owner

@samber samber May 5, 2024

Choose a reason for hiding this comment

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

Ok, let's merge your last commit and see how people use it in the future!

I'm going to add a comment for feedback collection.

Copy link
Owner

Choose a reason for hiding this comment

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

I just created the issue: #7

Copy link
Owner

Choose a reason for hiding this comment

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

...and added an example to the readme.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK.
I will try to find a better way on my end too.
Thank you for the feedback!

@goro9 goro9 requested a review from samber May 3, 2024 15:41
@goro9 goro9 force-pushed the post-in-thread branch from 3ffb8c7 to c598687 Compare May 4, 2024 05:29
@samber samber merged commit 62f5ebe into samber:main May 5, 2024
1 of 2 checks passed
@goro9 goro9 deleted the post-in-thread branch May 6, 2024 08:07
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.

2 participants