langfuse

package
v0.0.0-...-74ad9c3 Latest Latest
Warning

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

Go to latest
Published: May 19, 2025 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrBatchFailed = errors.New("langfuse: batch ingestion failed")

Functions

This section is empty.

Types

type BatchError

type BatchError struct {
	Errors []api.IngestionError `json:"errors"`
}

func (*BatchError) Error

func (e *BatchError) Error() string

func (*BatchError) Unwrap

func (e *BatchError) Unwrap() error

Unwrap allows the error to be checked with errors.Is without type assertion

type Client

type Client struct {
	API *api.Client
	// contains filtered or unexported fields
}

Client provides ergonomic methods for ingesting observations and other APIs.

func New

func New(opts *ClientOptions) (*Client, error)

New creates a client from code-generated API client implementation.

Note: the `API` field takes care of Basic Auth.

func (*Client) Batch

func (c *Client) Batch(ctx context.Context, events []Ingestible) error

Batch submits a series of ingestibles to the upstream API.

func (*Client) Flush

func (c *Client) Flush(ctx context.Context) error

Flush will batch all ingestibles remaining in the client's buffer.

You would typically `defer client.Flush()`.

func (*Client) Ingest

func (c *Client) Ingest(event Ingestible)

Ingest adds an ingestible to the client's buffer, after populating it.

func (*Client) Populate

func (c *Client) Populate(event Ingestible)

Populate populates the traces and spans with the client.

You will typically use this on spans after unmarshalling them from the database metadata column, or for inputs that didn't originate from the client directly.

func (*Client) Trace

func (c *Client) Trace(t *Trace) *Trace

Trace populates the provided trace, ingests and returns it.

type ClientOptions

type ClientOptions struct {
	Host       string
	PrivateKey string
	PublicKey  string

	HTTPClient *http.Client
}

ClientOptions are the determining the API line for this package.

The client will inherit the provided HTTP client, or otherwise use sane defaults for transport, dialer, and timeouts. If you wish to control these variables, you absolutely should provide your own client.

type Event

type Event struct {
	Id                  string    `json:"id"`
	TraceId             string    `json:"traceId,omitempty"`
	Name                string    `json:"name,omitempty"`
	StartTime           time.Time `json:"startTime"`
	Metadata            any       `json:"metadata,omitempty"`
	Input               any       `json:"input,omitempty"`
	Output              any       `json:"output,omitempty"`
	Level               string    `json:"level,omitempty"`
	StatusMessage       string    `json:"statusMessage,omitempty"`
	ParentObservationId string    `json:"parentObservationId,omitempty"`
	Version             string    `json:"version,omitempty"`
	Environment         string    `json:"environment,omitempty"`
}

func (*Event) EventId

func (e *Event) EventId() string

func (*Event) EventTime

func (e *Event) EventTime() time.Time

func (*Event) EventType

func (e *Event) EventType() EventType

type EventType

type EventType string

EventType corresponds to various events supported by Langfuse.

Events are distinct from observations: for example, a single observation may appear as multiple events: first, when it is created, and then when it is updated.

const (
	TRACE_CREATE       EventType = "trace-create"
	SCORE_CREATE       EventType = "score-create"
	SPAN_CREATE        EventType = "span-create"
	SPAN_UPDATE        EventType = "span-update"
	GENERATION_CREATE  EventType = "generation-create"
	GENERATION_UPDATE  EventType = "generation-update"
	EVENT_CREATE       EventType = "event-create"
	SDK_LOG            EventType = "sdk-log"
	OBSERVATION_CREATE EventType = "observation-create"
	OBSERVATION_UPDATE EventType = "observation-update"
)

type Generation

type Generation struct {
	Id                  string             `json:"id"`
	TraceId             string             `json:"traceId,omitempty"`
	Name                string             `json:"name,omitempty"`
	StartedAt           time.Time          `json:"startTime"`
	EndedAt             *time.Time         `json:"endTime,omitempty"`
	CompletionAt        *time.Time         `json:"completionStartTime,omitempty"`
	Model               string             `json:"model,omitempty"`
	ModelParameters     map[string]any     `json:"modelParameters,omitempty"`
	Usage               any                `json:"usage,omitempty"`
	UsageDetails        any                `json:"usageDetails,omitempty"`
	CostDetails         map[string]float64 `json:"costDetails,omitempty"`
	PromptName          string             `json:"promptName,omitempty"`
	PromptVersion       *int               `json:"promptVersion,omitempty"`
	Metadata            any                `json:"metadata,omitempty"`
	Input               any                `json:"input,omitempty"`
	Output              any                `json:"output,omitempty"`
	Level               string             `json:"level,omitempty"`
	StatusMessage       string             `json:"statusMessage,omitempty"`
	ParentObservationId string             `json:"parentObservationId,omitempty"`
	Version             string             `json:"version,omitempty"`
	Environment         string             `json:"environment,omitempty"`
}

func (*Generation) End

func (g *Generation) End()

func (*Generation) EventId

func (g *Generation) EventId() string

func (*Generation) EventTime

func (g *Generation) EventTime() time.Time

func (*Generation) EventType

func (g *Generation) EventType() EventType

type Ingestible

type Ingestible interface {
	EventId() string
	EventType() EventType
	EventTime() time.Time
}

Ingestible represents an observation, or update thereof, that Langfuse will ingest using Batched ingestion API. The schema is a bit awkward; to make it more ergonomic, the observation types (Span, Generation, Event) themselves will determine whether they are created or updated.

For example, calling End() on a span will set its EndedAt time, and ingest an update. Therefore, if defer sequence is client.Flush, span.End, then the span will first be ingested as not having endTime, then again as an update with endTime set, and finally flushed.

https://api.reference.langfuse.com/#tag/ingestion/POST/api/public/ingestion

type Span

type Span struct {
	Id                  string     `json:"id"`
	TraceId             string     `json:"traceId,omitempty"`
	Name                string     `json:"name,omitempty"`
	StartedAt           time.Time  `json:"startTime"`
	EndedAt             *time.Time `json:"endTime,omitempty"`
	Metadata            any        `json:"metadata,omitempty"`
	Input               any        `json:"input,omitempty"`
	Output              any        `json:"output,omitempty"`
	Level               string     `json:"level,omitempty"`
	StatusMessage       string     `json:"statusMessage,omitempty"`
	ParentObservationId string     `json:"parentObservationId,omitempty"`
	Version             string     `json:"version,omitempty"`
	Environment         string     `json:"environment,omitempty"`
	// contains filtered or unexported fields
}

func (*Span) End

func (s *Span) End()

func (*Span) Event

func (s *Span) Event(e *Event) *Event

func (*Span) EventId

func (s *Span) EventId() string

func (*Span) EventTime

func (s *Span) EventTime() time.Time

func (*Span) EventType

func (s *Span) EventType() EventType

func (*Span) Generation

func (s *Span) Generation(g *Generation) *Generation

func (*Span) Span

func (s *Span) Span(childSpan *Span) *Span

type Trace

type Trace struct {
	Id          string    `json:"id"`
	Name        string    `json:"name"`
	SessionId   string    `json:"sessionId,omitempty"`
	UserId      string    `json:"userId,omitempty"`
	Environment string    `json:"environment,omitempty"`
	Release     string    `json:"release,omitempty"`
	Version     string    `json:"version,omitempty"`
	Tags        []string  `json:"tags,omitempty"`
	Input       any       `json:"input,omitempty"`
	Metadata    any       `json:"metadata,omitempty"`
	Output      any       `json:"output,omitempty"`
	Public      bool      `json:"public,omitempty"`
	Timestamp   time.Time `json:"timestamp"`
	// contains filtered or unexported fields
}

func (*Trace) Event

func (t *Trace) Event(e *Event) *Event

func (*Trace) EventId

func (t *Trace) EventId() string

func (*Trace) EventTime

func (t *Trace) EventTime() time.Time

func (*Trace) EventType

func (t *Trace) EventType() EventType

func (*Trace) Span

func (t *Trace) Span(s *Span) *Span

Directories

Path Synopsis
Package api provides primitives to interact with the openapi HTTP API.
Package api provides primitives to interact with the openapi HTTP API.

Jump to

Keyboard shortcuts

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