Repository navigation
Expand file tree
/
Copy pathclient.go
More file actions
69 lines (60 loc) · 1.78 KB
/
Copy pathclient.go
File metadata and controls
69 lines (60 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package gush
import (
"fmt"
"log/slog"
"os"
"sync"
"go.temporal.io/sdk/client"
temporallog "go.temporal.io/sdk/log"
)
// Options configures the connection to Temporal, including its address, namespace,
// TLS and credentials. It is the Temporal SDK's client.Options.
type Options = client.Options
// Client is a connection to a Temporal server.
type Client struct {
temporalClient client.Client
logShutdown func()
closeOnce sync.Once
}
// Dial connects to Temporal and starts OTLP log export when it is configured; see
// InitLogExport.
//
// An empty HostPort falls back to TEMPORAL_ADDRESS, then the SDK's default of
// localhost:7233. An empty Namespace falls back to TEMPORAL_NAMESPACE, then the SDK's
// default namespace.
func Dial(options Options) (*Client, error) {
if options.HostPort == "" {
options.HostPort = os.Getenv(envTemporalAddress)
}
if options.Namespace == "" {
options.Namespace = os.Getenv(envTemporalNamespace)
}
if options.Logger == nil {
handlerOptions := &slog.HandlerOptions{Level: slog.LevelError}
handler := slog.NewTextHandler(os.Stderr, handlerOptions)
options.Logger = temporallog.NewStructuredLogger(slog.New(handler))
}
temporalClient, err := client.Dial(options)
if err != nil {
return nil, fmt.Errorf("gush: connect to Temporal: %w", err)
}
logShutdown, err := InitLogExport()
if err != nil {
temporalClient.Close()
return nil, err
}
connected := &Client{temporalClient: temporalClient, logShutdown: logShutdown}
return connected, nil
}
// Close closes the connection and flushes the logs this client started exporting.
// It is safe to call more than once.
func (c *Client) Close() {
c.closeOnce.Do(func() {
if c.temporalClient != nil {
c.temporalClient.Close()
}
if c.logShutdown != nil {
c.logShutdown()
}
})
}