Skip to content

Commit

Permalink
Add a Datadog sample (#319)
Browse files Browse the repository at this point in the history
Add Datadog sample
  • Loading branch information
Quinn-With-Two-Ns committed Mar 13, 2024
1 parent 5c63363 commit ea23602
Show file tree
Hide file tree
Showing 7 changed files with 340 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ errcheck:

workflowcheck:
@printf $(COLOR) "Run workflow check..."
@go install go.temporal.io/sdk/contrib/tools/workflowcheck
@go install go.temporal.io/sdk/contrib/tools/workflowcheck@latest
@workflowcheck -show-pos ./...

update-sdk:
Expand Down
42 changes: 42 additions & 0 deletions datadog/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
This sample shows how to use Temporal's Datadog interceptor to add tracing to your worker and clients.

### Setup

To run this sample make sure you have an active datadog agent reachable. This sample assume you have the agent running at `localhost:8126` if not adjust the sample accordingly.

https://docs.datadoghq.com/getting_started/agent/

#### Metrics

Starting with version 6.5.0 the Datadog agent is capable of scraping prometheus endpoints.

See more details here:
https://docs.datadoghq.com/integrations/guide/prometheus-host-collection/

Example `openmetrics.d/conf.yaml` to collect metrics from this sample and emit them to datadog under "myapp" namespace.

```
instances:
- prometheus_url: http://localhost:9090/metrics
namespace: "myapp"
metrics:
- temporal_datadog*
```

#### Logging

When using the Datadog interceptor all user created loggers will also include the trace and span ID. This makes
it possible to correlate logs with traces.

For documentation on how to configure your Datadog agent to upload logs see:
https://docs.datadoghq.com/logs/log_collection/go/

### 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 datadog/worker/main.go
```
3) Run the following command to start the example
```
go run datadog/starter/main.go
46 changes: 46 additions & 0 deletions datadog/starter/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"context"
"log"

"github.com/temporalio/samples-go/datadog"
"go.temporal.io/sdk/client"
"go.temporal.io/sdk/contrib/datadog/tracing"
"go.temporal.io/sdk/interceptor"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)

func main() {
// Start the tracer and defer the Stop method.
tracer.Start(tracer.WithAgentAddr("localhost:8126"))
defer tracer.Stop()

// The client is a heavyweight object that should be created once per process.
c, err := client.Dial(client.Options{
Interceptors: []interceptor.ClientInterceptor{tracing.NewTracingInterceptor(tracing.TracerOptions{})},
})
if err != nil {
log.Fatalln("Unable to create client", err)
}
defer c.Close()

workflowOptions := client.StartWorkflowOptions{
ID: "datadog_workflow_id",
TaskQueue: "datadog",
}

we, err := c.ExecuteWorkflow(context.Background(), workflowOptions, datadog.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.
err = we.Get(context.Background(), nil)
if err != nil {
log.Fatalln("Unable get workflow result", err)
}
log.Println("Workflow completed. Check worker logs.")
}
94 changes: 94 additions & 0 deletions datadog/worker/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package main

import (
"io"
"log"
"log/slog"
"os"
"time"

prom "github.com/prometheus/client_golang/prometheus"
"github.com/temporalio/samples-go/datadog"
"github.com/uber-go/tally/v4"
"github.com/uber-go/tally/v4/prometheus"
"go.temporal.io/sdk/client"
"go.temporal.io/sdk/contrib/datadog/tracing"
sdktally "go.temporal.io/sdk/contrib/tally"
"go.temporal.io/sdk/interceptor"
tlog "go.temporal.io/sdk/log"
"go.temporal.io/sdk/worker"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)

func main() {
// Start the tracer and defer the Stop method.
tracer.Start(tracer.WithAgentAddr("localhost:8126"))
defer tracer.Stop()

// Setup logging
f, err := os.OpenFile("worker.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
defer func() {
err = f.Close()
if err != nil {
log.Fatalf("error closing file: %v", err)
}
}()
wrt := io.MultiWriter(os.Stdout, f)

logger := tlog.NewStructuredLogger(
slog.New(slog.NewJSONHandler(wrt, &slog.HandlerOptions{
Level: slog.LevelInfo,
})))

c, err := client.Dial(client.Options{
Logger: logger,
Interceptors: []interceptor.ClientInterceptor{tracing.NewTracingInterceptor(tracing.TracerOptions{})},
MetricsHandler: sdktally.NewMetricsHandler(newPrometheusScope(prometheus.Configuration{
ListenAddress: "localhost:9090",
TimerType: "histogram",
})),
})
if err != nil {
log.Fatalln("Unable to create client", err)
}
defer c.Close()

w := worker.New(c, "datadog", worker.Options{})

w.RegisterWorkflow(datadog.Workflow)
w.RegisterWorkflow(datadog.ChildWorkflow)
w.RegisterActivity(datadog.Activity)

err = w.Run(worker.InterruptCh())
if err != nil {
log.Fatalln("Unable to start worker", err)
}
}

func newPrometheusScope(c prometheus.Configuration) tally.Scope {
reporter, err := c.NewReporter(
prometheus.ConfigurationOptions{
Registry: prom.NewRegistry(),
OnError: func(err error) {
log.Println("error in prometheus reporter", err)
},
},
)
if err != nil {
log.Fatalln("error creating prometheus reporter", err)
}
scopeOpts := tally.ScopeOptions{
CachedReporter: reporter,
Separator: prometheus.DefaultSeparator,
SanitizeOptions: &sdktally.PrometheusSanitizeOptions,
Prefix: "temporal_datadog",
}
scope, _ := tally.NewRootScope(scopeOpts, time.Second)
scope = sdktally.NewPrometheusNamingScope(scope)

log.Println("prometheus metrics scope created")
return scope
}
33 changes: 33 additions & 0 deletions datadog/workflow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package datadog

import (
"context"
"time"

"go.temporal.io/sdk/activity"
"go.temporal.io/sdk/workflow"
)

func Workflow(ctx workflow.Context, name string) error {
workflow.GetLogger(ctx).Info("Executing Workflow.", "name", name)
cwo := workflow.ChildWorkflowOptions{
WorkflowID: "DATADOG-CHILD-WORKFLOW-ID",
}
ctx = workflow.WithChildOptions(ctx, cwo)
return workflow.ExecuteChildWorkflow(ctx, ChildWorkflow, name).Get(ctx, nil)
}

func ChildWorkflow(ctx workflow.Context, name string) error {
workflow.GetLogger(ctx).Info("Executing ChildWorkflow.", "name", name)
ao := workflow.ActivityOptions{
StartToCloseTimeout: 10 * time.Second,
}
ctx = workflow.WithActivityOptions(ctx, ao)
return workflow.ExecuteActivity(ctx, Activity, name).Get(ctx, nil)
}

func Activity(ctx context.Context, name string) error {
activity.GetLogger(ctx).Info("Executing Activity.", "name", name)
time.Sleep(time.Second)
return nil
}
36 changes: 29 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,35 @@ require (
go.opentelemetry.io/otel/trace v1.22.0
go.temporal.io/api v1.29.1
go.temporal.io/sdk v1.26.0
go.temporal.io/sdk/contrib/datadog v0.2.0
go.temporal.io/sdk/contrib/opentelemetry v0.3.0
go.temporal.io/sdk/contrib/opentracing v0.1.0
go.temporal.io/sdk/contrib/tally v0.2.0
go.temporal.io/sdk/contrib/tools/workflowcheck v0.0.0-20230612164027-11c2cb9e7d2d
go.temporal.io/sdk/contrib/tools/workflowcheck v0.2.0
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.24.0
google.golang.org/protobuf v1.33.0
gopkg.in/DataDog/dd-trace-go.v1 v1.59.0
gopkg.in/square/go-jose.v2 v2.6.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/DataDog/appsec-internal-go v1.4.0 // indirect
github.com/DataDog/datadog-agent/pkg/obfuscate v0.48.0 // indirect
github.com/DataDog/datadog-agent/pkg/remoteconfig/state v0.48.1 // indirect
github.com/DataDog/datadog-go/v5 v5.3.0 // indirect
github.com/DataDog/go-libddwaf/v2 v2.2.3 // indirect
github.com/DataDog/go-tuf v1.0.2-0.5.2 // indirect
github.com/DataDog/sketches-go v1.4.2 // indirect
github.com/HdrHistogram/hdrhistogram-go v1.1.2 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/benbjohnson/clock v1.3.5 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/ebitengine/purego v0.5.0 // indirect
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
Expand All @@ -47,33 +59,43 @@ require (
github.com/golang/protobuf v1.5.4 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 // indirect
github.com/hashicorp/go-hclog v1.3.1 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-hclog v1.5.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/oklog/run v1.1.0 // indirect
github.com/outcaste-io/ristretto v0.2.3 // indirect
github.com/philhofer/fwd v1.1.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/robfig/cron v1.2.0 // indirect
github.com/secure-systems-lab/go-securesystemslib v0.7.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/tinylib/msgp v1.1.8 // indirect
github.com/twmb/murmur3 v1.1.8 // indirect
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
go.opentelemetry.io/otel/metric v1.22.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go4.org/intern v0.0.0-20230525184215-6c62f75575cb // indirect
go4.org/unsafe/assume-no-moving-gc v0.0.0-20231121144256-b99613f794b6 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/exp v0.0.0-20231127185646-65229373498e // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.16.0 // indirect
golang.org/x/tools v0.17.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240304212257-790db918fca8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240304212257-790db918fca8 // indirect
google.golang.org/grpc v1.62.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
inet.af/netaddr v0.0.0-20230525184311-b8eac61e914a // indirect
)
Loading

0 comments on commit ea23602

Please sign in to comment.