otel

package
v4.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 14, 2024 License: MIT Imports: 6 Imported by: 0

README

OTel Middleware

The otel is a middleware for that provides observability with OpenTelemetry.

Usage

package main

import (
	"context"
	"fmt"
	"time"

	"github.com/flc1125/go-cron/v4"
	"github.com/flc1125/go-cron/v4/middleware/otel"
	"go.opentelemetry.io/otel/sdk/trace"
	"go.opentelemetry.io/otel/sdk/trace/tracetest"
)

type basicJob struct {
}

var (
	_ cron.Job         = (*basicJob)(nil)
	_ otel.JobWithName = (*basicJob)(nil)
)

func (b *basicJob) Name() string {
	return "basic:job"
}

func (b *basicJob) Run(ctx context.Context) error {
	// do something
	return nil
}

func main() {
	// configure otel, the following is just a demonstration provider.
	imsb := tracetest.NewInMemoryExporter()
	tp := trace.NewTracerProvider(trace.WithSyncer(imsb))

	// cron
	c := cron.New(cron.WithSeconds())
	c.Use(otel.New(
		otel.WithTracerProvider(tp), // custom otel.TracerProvider
	))

	_, _ = c.AddJob("* * * * * *", &basicJob{})

	c.Start()
	defer c.Stop()

	time.Sleep(10 * time.Second)
	fmt.Println("spans:", len(imsb.GetSpans()))
}

output:

spans: 10

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(opts ...Option) cron.Middleware

Types

type JobWithName

type JobWithName interface {
	cron.Job

	// Name returns the name of the job.
	Name() string
}

type Option

type Option func(*options)

func WithTracerProvider

func WithTracerProvider(tp trace.TracerProvider) Option

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL