Coder Social home page Coder Social logo

Comments (9)

cretz avatar cretz commented on August 17, 2024 1

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.

cretz avatar cretz commented on August 17, 2024 1

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.

Hades32 avatar Hades32 commented on August 17, 2024

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.

jjkoh95 avatar jjkoh95 commented on August 17, 2024

Hi, the above code works
is there a way to pull trace from workflow context?

from samples-go.

jjkoh95 avatar jjkoh95 commented on August 17, 2024

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.

emanuelef avatar emanuelef commented on August 17, 2024

@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.

Hades32 avatar Hades32 commented on August 17, 2024

@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.

emanuelef avatar emanuelef commented on August 17, 2024

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.

Quinn-With-Two-Ns avatar Quinn-With-Two-Ns commented on August 17, 2024

Closed by #296

from samples-go.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.