Skip to content

Commit

Permalink
Datadog sample
Browse files Browse the repository at this point in the history
  • Loading branch information
Quinn-With-Two-Ns committed Dec 27, 2023
1 parent 359c36a commit 604064f
Show file tree
Hide file tree
Showing 6 changed files with 789 additions and 6 deletions.
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
}
17 changes: 15 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,38 @@ require (
go.uber.org/multierr v1.7.0
go.uber.org/zap v1.20.0
google.golang.org/grpc v1.57.0
gopkg.in/DataDog/dd-trace-go.v1 v1.42.0
gopkg.in/square/go-jose.v2 v2.6.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/DataDog/datadog-agent/pkg/obfuscate v0.0.0-20211129110424-6491aa3bf583 // indirect
github.com/DataDog/datadog-go v4.8.2+incompatible // indirect
github.com/DataDog/datadog-go/v5 v5.0.2 // indirect
github.com/DataDog/sketches-go v1.2.1 // indirect
github.com/HdrHistogram/hdrhistogram-go v1.1.2 // indirect
github.com/Microsoft/go-winio v0.5.1 // indirect
github.com/aws/aws-sdk-go v1.42.44 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/bmizerany/perks v0.0.0-20230307044200-03f9df79da1e // indirect
github.com/cactus/go-statsd-client/statsd v0.0.0-20200423205355-cb0885a1018c // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgraph-io/ristretto v0.1.0 // indirect
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/go-logr/logr v1.2.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gocql/gocql v0.0.0-20211222173705-d73e6b1002a7 // indirect
github.com/gocql/gocql v0.0.0-20220224095938-0eacd3183625 // indirect
github.com/gogo/googleapis v1.4.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/gogo/status v1.1.1 // indirect
github.com/golang-jwt/jwt/v4 v4.2.0 // indirect
github.com/golang/glog v1.1.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
Expand All @@ -64,16 +73,18 @@ require (
github.com/oklog/run v1.1.0 // indirect
github.com/olivere/elastic v6.2.37+incompatible // indirect
github.com/olivere/elastic/v7 v7.0.31 // indirect
github.com/philhofer/fwd v1.1.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prashantv/protectmem v0.0.0-20171002184600-e20412882b3a // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/common v0.34.0 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/robfig/cron v1.2.0 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/streadway/quantile v0.0.0-20220407130108-4246515d968d // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/tinylib/msgp v1.1.2 // indirect
github.com/twmb/murmur3 v1.1.6 // indirect
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
github.com/uber/tchannel-go v1.22.2 // indirect
Expand All @@ -85,6 +96,7 @@ require (
go.opentelemetry.io/otel/sdk/export/metric v0.26.0 // indirect
go.opentelemetry.io/otel/sdk/metric v0.26.0 // indirect
go.opentelemetry.io/otel/trace v1.3.0 // indirect
go.temporal.io/sdk/contrib/datadog v0.1.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/dig v1.13.0 // indirect
go.uber.org/fx v1.16.0 // indirect
Expand All @@ -94,6 +106,7 @@ require (
golang.org/x/text v0.12.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.9.3 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/genproto v0.0.0-20230815205213-6bfd019c3878 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230815205213-6bfd019c3878 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230815205213-6bfd019c3878 // indirect
Expand Down
Loading

0 comments on commit 604064f

Please sign in to comment.