Comments (9)
We usually do not recommend doing this because it can be non-deterministic assuming a worker is setup with OTel. Having said that, you can likely set the SpanContextKey
option in the opentelemetry.TracerOptions
and check that key in the workflow context. (also, feel free to use forums or Slack for general questions if that's easier)
from samples-go.
Here's a sample I wrote for a user:
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/google/uuid"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
"go.temporal.io/sdk/client"
"go.temporal.io/sdk/contrib/opentelemetry"
"go.temporal.io/sdk/interceptor"
"go.temporal.io/sdk/testsuite"
"go.temporal.io/sdk/worker"
"go.temporal.io/sdk/workflow"
)
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}
func run() error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Initialize tracer
exp, err := stdouttrace.New(stdouttrace.WithPrettyPrint())
if err != nil {
return fmt.Errorf("failed to initialize stdouttrace exporter: %w", err)
}
prov := sdktrace.NewTracerProvider(
sdktrace.WithBatcher(exp),
sdktrace.WithResource(resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceName("temporal-example"),
semconv.ServiceVersion("0.0.1"),
)),
)
defer prov.Shutdown(ctx)
otel.SetTracerProvider(prov)
// Start dev server
tracingInterceptor, err := opentelemetry.NewTracingInterceptor(opentelemetry.TracerOptions{})
if err != nil {
return err
}
srv, err := testsuite.StartDevServer(ctx, testsuite.DevServerOptions{
ClientOptions: &client.Options{
Interceptors: []interceptor.ClientInterceptor{tracingInterceptor},
},
LogLevel: "error",
})
if err != nil {
return err
}
defer srv.Stop()
// Start worker
log.Print("Running worker")
taskQueue := uuid.NewString()
w := worker.New(srv.Client(), taskQueue, worker.Options{})
w.RegisterWorkflow(MyWorkflow)
w.RegisterActivity(MyActivity)
if err := w.Start(); err != nil {
return err
}
defer w.Stop()
// Run workflow
log.Print("Running workflow")
run, err := srv.Client().ExecuteWorkflow(
ctx,
client.StartWorkflowOptions{TaskQueue: taskQueue},
MyWorkflow,
)
if err != nil {
return err
} else if err = run.Get(ctx, nil); err != nil {
return err
}
log.Print("Workflow complete")
return nil
}
func MyWorkflow(ctx workflow.Context) error {
ctx = workflow.WithActivityOptions(ctx, workflow.ActivityOptions{
StartToCloseTimeout: 10 * time.Second,
})
return workflow.ExecuteActivity(ctx, MyActivity).Get(ctx, nil)
}
func MyActivity(context.Context) error {
log.Print("Executing activity")
return nil
}
from samples-go.
for reference, this is what we're currently doing and it seems to work fine
import (
"go.temporal.io/sdk/contrib/opentelemetry"
"go.temporal.io/sdk/interceptor"
"go.temporal.io/sdk/client"
)
func init() {
tracingInterceptor, err := opentelemetry.NewTracingInterceptor(opentelemetry.TracerOptions{})
options := client.Options{
Interceptors: []interceptor.ClientInterceptor{tracingInterceptor},
}
c, err := client.Dial(options)
from samples-go.
Hi, the above code works
is there a way to pull trace from workflow context?
from samples-go.
Thanks, I need it to better correlate logs from workflow for other log aggregator instead of temporal. (let me know if there's a better way to do this)
I managed to get it work by changing a custom SpanContextKey
.
from samples-go.
@Hades32 I guess this is what you do on the client side, did you have to add the Otel stanza in the Temporal deploy as documented here ?
https://github.com/temporalio/temporal/blob/master/develop/docs/tracing.md#configuring
from samples-go.
@emanuelef no, I didn't change the Temporal deployment. I guess that would only be needed if you want to get detailed information on what's going on inside Temporal. I only cared about knowing which signals were sent, activities and workflows started, etc, and we get all that
from samples-go.
Thanks @Hades32, I'll try to do the same
Where did you add the collector endpoint ? just add OTEL_EXPORTER_OTLP_ENDPOINT ?
And didn't you need to instrument the custom Temporal worker with your workflows and activities ?
from samples-go.
Closed by #296
from samples-go.
Related Issues (20)
- go.temporal.io/sdk/contrib/tools/workflowcheck-v0.0.0-20230413153418-eaa3802876de: 3 vulnerabilities (highest severity is: 6.1) - autoclosed HOT 1
- Update sample should early exit if update is not enabled and provide instruction to enable. HOT 1
- [Bug] DevContainer doesn't start the temporal server image HOT 2
- Go Worker versioning samples
- go.temporal.io/sdk/contrib/tools/workflowcheck-v0.0.0-20230612164027-11c2cb9e7d2d: 3 vulnerabilities (highest severity is: 6.1) - autoclosed HOT 1
- [Bug] serverjwtauth sample doesn't work with newer servers
- CreateSession doesn't work. HOT 2
- [Bug] devcontainer usage HOT 2
- [Bug] reqrespactivity/reqrespquery samples have wrong assumpion about Selector.HasPending
- Add Sample for Typed equivalents for workflow.* functions/interfaces using generics HOT 1
- Sample request: Rate limiting or semaphore implemented as a workflow
- [Feature Request] Temporal server release with otel upgrade HOT 1
- [Feature Request] Sentry sample
- [Feature Request] Add Eager Workflow Start sample
- [Feature Request] In metrics/prometheus sample, demonstrate millisecond histograms
- [Feature Request] Sample Update: Robust session retries HOT 2
- Allow users to switch between cloud and localhost when running samples
- Failed status HOT 2
- add real time app sample
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from samples-go.