-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
11 changed files
with
448 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
### Steps to run this sample: | ||
1) Run a [Temporal service](https://github.com/temporalio/samples-go/tree/main/#how-to-use). | ||
2) Run the following command to start the worker | ||
``` | ||
go run slogadapter/worker/main.go | ||
``` | ||
3) Run the following command to start the example | ||
``` | ||
go run slogadapter/starter/main.go | ||
``` | ||
4) Check worker logs in colorful JSON format: | ||
```json | ||
{"time":"2023-10-06T14:16:30.511716-07:00","level":"INFO","source":{"function":"go.temporal.io/sdk/internal.(*AggregatedWorker).Start","file":"/Users/user/go/pkg/mod/go.temporal.io/[email protected]/internal/internal_worker.go","line":987},"msg":"Started Worker","Namespace":"default","TaskQueue":"slog-logger","WorkerID":"11177@worker@"} | ||
{"time":"2023-10-06T14:16:32.679875-07:00","level":"INFO","source":{"function":"github.com/temporalio/samples-go/slogadapter.Workflow","file":"/Users/user/Documents/Code/samples-go/slogadapter/workflow.go","line":22},"msg":"Logging from workflow","Namespace":"default","TaskQueue":"slog-logger","WorkerID":"11177@worker@","WorkflowType":"Workflow","WorkflowID":"slog_logger_workflow_id","RunID":"7666eddc-a124-445a-a085-2fd0e9a719e3","Attempt":1,"name":"<param to log>"} | ||
{"time":"2023-10-06T14:16:32.67998-07:00","level":"DEBUG","source":{"function":"go.temporal.io/sdk/internal.(*workflowEnvironmentImpl).ExecuteActivity","file":"/Users/user/go/pkg/mod/go.temporal.io/[email protected]/internal/internal_event_handlers.go","line":676},"msg":"ExecuteActivity","Namespace":"default","TaskQueue":"slog-logger","WorkerID":"11177@worker@","WorkflowType":"Workflow","WorkflowID":"slog_logger_workflow_id","RunID":"7666eddc-a124-445a-a085-2fd0e9a719e3","Attempt":1,"ActivityID":"5","ActivityType":"LoggingActivity"} | ||
{"time":"2023-10-06T14:16:32.689103-07:00","level":"INFO","source":{"function":"github.com/temporalio/samples-go/slogadapter.LoggingActivity","file":"/Users/user/Documents/Code/samples-go/slogadapter/workflow.go","line":45},"msg":"Executing LoggingActivity.","Namespace":"default","TaskQueue":"slog-logger","WorkerID":"11177@worker@","ActivityID":"5","ActivityType":"LoggingActivity","Attempt":1,"WorkflowType":"Workflow","WorkflowID":"slog_logger_workflow_id","RunID":"7666eddc-a124-445a-a085-2fd0e9a719e3","activity":"LoggingActivity","name":"<param to log>"} | ||
{"time":"2023-10-06T14:16:32.689144-07:00","level":"DEBUG","source":{"function":"github.com/temporalio/samples-go/slogadapter.LoggingActivity","file":"/Users/user/Documents/Code/samples-go/slogadapter/workflow.go","line":46},"msg":"Debugging LoggingActivity.","Namespace":"default","TaskQueue":"slog-logger","WorkerID":"11177@worker@","ActivityID":"5","ActivityType":"LoggingActivity","Attempt":1,"WorkflowType":"Workflow","WorkflowID":"slog_logger_workflow_id","RunID":"7666eddc-a124-445a-a085-2fd0e9a719e3","activity":"LoggingActivity","value":"important debug data"} | ||
{"time":"2023-10-06T14:16:32.703167-07:00","level":"DEBUG","source":{"function":"go.temporal.io/sdk/internal.(*workflowEnvironmentImpl).ExecuteActivity","file":"/Users/user/go/pkg/mod/go.temporal.io/[email protected]/internal/internal_event_handlers.go","line":676},"msg":"ExecuteActivity","Namespace":"default","TaskQueue":"slog-logger","WorkerID":"11177@worker@","WorkflowType":"Workflow","WorkflowID":"slog_logger_workflow_id","RunID":"7666eddc-a124-445a-a085-2fd0e9a719e3","Attempt":1,"ActivityID":"11","ActivityType":"LoggingErrorAcctivity"} | ||
{"time":"2023-10-06T14:16:32.70982-07:00","level":"WARN","source":{"function":"github.com/temporalio/samples-go/slogadapter.LoggingErrorAcctivity","file":"/Users/user/Documents/Code/samples-go/slogadapter/workflow.go","line":52},"msg":"Ignore next error message. It is just for demo purpose.","Namespace":"default","TaskQueue":"slog-logger","WorkerID":"11177@worker@","ActivityID":"11","ActivityType":"LoggingErrorAcctivity","Attempt":1,"WorkflowType":"Workflow","WorkflowID":"slog_logger_workflow_id","RunID":"7666eddc-a124-445a-a085-2fd0e9a719e3"} | ||
{"time":"2023-10-06T14:16:32.709847-07:00","level":"ERROR","source":{"function":"github.com/temporalio/samples-go/slogadapter.LoggingErrorAcctivity","file":"/Users/user/Documents/Code/samples-go/slogadapter/workflow.go","line":53},"msg":"Unable to execute LoggingErrorAcctivity.","Namespace":"default","TaskQueue":"slog-logger","WorkerID":"11177@worker@","ActivityID":"11","ActivityType":"LoggingErrorAcctivity","Attempt":1,"WorkflowType":"Workflow","WorkflowID":"slog_logger_workflow_id","RunID":"7666eddc-a124-445a-a085-2fd0e9a719e3","error":"random error"} | ||
{"time":"2023-10-06T14:16:32.720006-07:00","level":"INFO","source":{"function":"github.com/temporalio/samples-go/slogadapter.Workflow","file":"/Users/user/Documents/Code/samples-go/slogadapter/workflow.go","line":37},"msg":"Workflow completed.","Namespace":"default","TaskQueue":"slog-logger","WorkerID":"11177@worker@","WorkflowType":"Workflow","WorkflowID":"slog_logger_workflow_id","RunID":"7666eddc-a124-445a-a085-2fd0e9a719e3","Attempt":1} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
"github.com/temporalio/samples-go/slogadapter" | ||
"go.temporal.io/sdk/client" | ||
) | ||
|
||
func main() { | ||
// The client is a heavyweight object that should be created once per process. | ||
c, err := client.Dial(client.Options{}) | ||
if err != nil { | ||
log.Fatalln("Unable to create client", err) | ||
} | ||
defer c.Close() | ||
|
||
workflowOptions := client.StartWorkflowOptions{ | ||
ID: "slog_logger_workflow_id", | ||
TaskQueue: "slog-logger", | ||
} | ||
|
||
we, err := c.ExecuteWorkflow(context.Background(), workflowOptions, slogadapter.Workflow, "<param to log>") | ||
if err != nil { | ||
log.Fatalln("Unable to execute workflow", err) | ||
} | ||
|
||
log.Println("Started workflow", "WorkflowID", we.GetID(), "RunID", we.GetRunID()) | ||
|
||
// Synchronously wait for the workflow completion. | ||
var result interface{} | ||
err = we.Get(context.Background(), &result) | ||
if err != nil { | ||
log.Fatalln("Unable get workflow result", err) | ||
} | ||
log.Println("Workflow completed. Check worker logs.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
|
||
"log/slog" | ||
|
||
"github.com/temporalio/samples-go/slogadapter" | ||
"go.temporal.io/sdk/client" | ||
tlog "go.temporal.io/sdk/log" | ||
"go.temporal.io/sdk/worker" | ||
) | ||
|
||
func main() { | ||
c, err := client.Dial(client.Options{ | ||
Logger: tlog.NewStructuredLogger( | ||
slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ | ||
AddSource: true, | ||
Level: slog.LevelDebug, | ||
}))), | ||
}) | ||
if err != nil { | ||
log.Fatalln("Unable to create client", err) | ||
} | ||
defer c.Close() | ||
|
||
w := worker.New(c, "slog-logger", worker.Options{}) | ||
|
||
w.RegisterWorkflow(slogadapter.Workflow) | ||
w.RegisterActivity(slogadapter.LoggingActivity) | ||
w.RegisterActivity(slogadapter.LoggingErrorAcctivity) | ||
|
||
err = w.Run(worker.InterruptCh()) | ||
if err != nil { | ||
log.Fatalln("Unable to start worker", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package slogadapter | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"time" | ||
|
||
"go.temporal.io/sdk/activity" | ||
"go.temporal.io/sdk/log" | ||
"go.temporal.io/sdk/workflow" | ||
) | ||
|
||
// Workflow is a workflow function which does some logging. | ||
// Important note: workflow logger is replay aware and it won't log during replay. | ||
func Workflow(ctx workflow.Context, name string) error { | ||
ao := workflow.ActivityOptions{ | ||
StartToCloseTimeout: 10 * time.Second, | ||
} | ||
ctx = workflow.WithActivityOptions(ctx, ao) | ||
|
||
logger := workflow.GetLogger(ctx) | ||
logger.Info("Logging from workflow", "name", name) | ||
|
||
var result interface{} | ||
err := workflow.ExecuteActivity(ctx, LoggingActivity, name).Get(ctx, &result) | ||
if err != nil { | ||
logger.Error("LoggingActivity failed.", "Error", err) | ||
return err | ||
} | ||
|
||
err = workflow.ExecuteActivity(ctx, LoggingErrorAcctivity).Get(ctx, &result) | ||
if err != nil { | ||
logger.Error("LoggingActivity failed.", "Error", err) | ||
return err | ||
} | ||
|
||
logger.Info("Workflow completed.") | ||
return nil | ||
} | ||
|
||
func LoggingActivity(ctx context.Context, name string) error { | ||
logger := activity.GetLogger(ctx) | ||
withLogger := logger.(log.WithLogger).With("activity", "LoggingActivity") | ||
|
||
withLogger.Info("Executing LoggingActivity.", "name", name) | ||
withLogger.Debug("Debugging LoggingActivity.", "value", "important debug data") | ||
return nil | ||
} | ||
|
||
func LoggingErrorAcctivity(ctx context.Context) error { | ||
logger := activity.GetLogger(ctx) | ||
logger.Warn("Ignore next error message. It is just for demo purpose.") | ||
logger.Error("Unable to execute LoggingErrorAcctivity.", "error", errors.New("random error")) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters