Documentation
¶
Index ¶
- Constants
- Variables
- func DetachContext(ctx context.Context) context.Context
- func LLMRoundFromContext(ctx context.Context) (int, bool)
- func NewLogSpanProcessor(log LogService) sdktrace.SpanProcessor
- func NewTurnIDGenerator() sdktrace.IDGenerator
- func ParseHeaders(raw string) (map[string]string, error)
- func SpanContextForTurn(turnID string) trace.SpanContext
- func SpanFromContext(ctx context.Context) trace.Span
- func Tracer() trace.Tracer
- func WithLLMAttributes(provider, model, operation string, streaming bool) trace.SpanStartOption
- func WithLLMRound(ctx context.Context, round int) context.Context
- func WithTurnID(ctx context.Context, turnID string) context.Context
- type LogService
- type OTLPOptions
- type Options
- type OutputMode
- type ShutdownFunc
Constants ¶
const ( ToolExecutionAuto = "auto" ToolExecutionApproved = "approved" )
const ( StreamOutcomeCompleted = "completed" StreamOutcomeCancelled = "canceled" StreamOutcomeErrored = "errored" StreamOutcomeEmpty = "empty" )
StreamOutcome values.
const TracerName = "github.com/mattermost/mattermost-plugin-ai"
Variables ¶
var ( LLMProvider = attribute.Key("agents.llm.provider") LLMModel = attribute.Key("agents.llm.model") LLMOperation = attribute.Key("agents.llm.operation") LLMStreaming = attribute.Key("agents.llm.streaming") LLMInputTokens = attribute.Key("agents.llm.input_tokens") LLMOutputTokens = attribute.Key("agents.llm.output_tokens") LLMCachedReadTokens = attribute.Key("agents.llm.cached_read_tokens") LLMCachedWriteTokens = attribute.Key("agents.llm.cached_write_tokens") LLMReasoningTokens = attribute.Key("agents.llm.reasoning_tokens") LLMCost = attribute.Key("agents.llm.cost") // Routing — which bifrost code path the request took. LLMPath = attribute.Key("agents.llm.path") // "chat" | "responses" LLMUseResponsesAPI = attribute.Key("agents.llm.use_responses_api") // bot/service-level toggle // Reasoning — what (if anything) was attached to the outbound request. LLMReasoningEffort = attribute.Key("agents.llm.reasoning.effort") LLMReasoningMaxTokens = attribute.Key("agents.llm.reasoning.max_tokens") LLMReasoningSent = attribute.Key("agents.llm.reasoning.sent") // true when the request includes a reasoning block // Bifrost error surface — populated when a request fails so opaque // "bifrost error: …" log lines can be correlated with status/type/code. LLMBifrostStatusCode = attribute.Key("agents.llm.bifrost.status_code") LLMBifrostErrorType = attribute.Key("agents.llm.bifrost.error_type") LLMBifrostErrorCode = attribute.Key("agents.llm.bifrost.error_code") LLMBifrostErrorProvider = attribute.Key("agents.llm.bifrost.error_provider") LLMBifrostIsBifrostErr = attribute.Key("agents.llm.bifrost.is_bifrost_error") // Per-source breakdown of agents.llm.input_tokens, derived from the // request and emitted on the LLM-call span. One attribute per source. LLMTokensSystem = attribute.Key("agents.llm.tokens.system") LLMTokensHistory = attribute.Key("agents.llm.tokens.history") LLMTokensToolDefs = attribute.Key("agents.llm.tokens.tool_defs") LLMTokensToolResults = attribute.Key("agents.llm.tokens.tool_results") LLMTokensImages = attribute.Key("agents.llm.tokens.images") // Per-round debugging signal. LLMFinishReason = attribute.Key("agents.llm.finish_reason") LLMTimeToFirstTokenMs = attribute.Key("agents.llm.time_to_first_token_ms") LLMRound = attribute.Key("agents.llm.round") // 0-based index of the LLM call within one tool loop )
Attribute keys for LLM operations
var ( AgentName = attribute.Key("agents.agent.name") AgentID = attribute.Key("agents.agent.id") ServiceID = attribute.Key("agents.service.id") // BridgePluginID is the plugin calling the LLM bridge API. BridgePluginID = attribute.Key("agents.bridge.plugin_id") AgentProgressPhase = attribute.Key("agents.agent.progress.phase") AgentProgressSequence = attribute.Key("agents.agent.progress.sequence") )
Attribute keys for agent context
var ( ToolName = attribute.Key("agents.tool.name") ToolID = attribute.Key("agents.tool.id") ToolStatus = attribute.Key("agents.tool.status") // ToolResultBytes and ToolResultTokens size the result the model will // see; the result itself is never attached. ToolResultBytes = attribute.Key("agents.tool.result.bytes") ToolResultTokens = attribute.Key("agents.tool.result.tokens_estimate") // ToolExecution is ToolExecutionAuto or ToolExecutionApproved. ToolExecution = attribute.Key("agents.tool.execution") // ToolReturnedNames lists the tool names search_tools returned; // ToolLoadedName is the tool load_tool loaded. ToolReturnedNames = attribute.Key("agents.tool.returned_names") ToolLoadedName = attribute.Key("agents.tool.loaded_name") // SkillID is the skill load_skill loaded. SkillID = attribute.Key("agents.skill.id") )
Attribute keys for tool operations
var ( MCPServer = attribute.Key("agents.mcp.server") MCPTool = attribute.Key("agents.mcp.tool") // MCPErrorClass and MCPErrorCode classify a failed tool call. The class is // from a fixed vocabulary and the code is an HTTP status or JSON-RPC code; // neither carries error text, arguments, or result content. MCPErrorClass = attribute.Key("agents.mcp.error_class") MCPErrorCode = attribute.Key("agents.mcp.error_code") )
Attribute keys for MCP operations
var ( WebSearchProvider = attribute.Key("agents.websearch.provider") WebSearchResultLimit = attribute.Key("agents.websearch.result_limit") // Failure surface: which early return a search bailed out on, plus the // upstream HTTP status when there was one. The query is never attached. WebSearchFailStage = attribute.Key("agents.websearch.fail_stage") WebSearchStatusCode = attribute.Key("agents.websearch.status_code") )
Attribute keys for web search operations
var ( UserID = attribute.Key("agents.user.id") ChannelID = attribute.Key("agents.channel.id") PostID = attribute.Key("agents.post.id") ResponsePostID = attribute.Key("agents.response_post.id") ThreadRootPostID = attribute.Key("agents.thread.root_post.id") ConversationID = attribute.Key("agents.conversation.id") UserTurnID = attribute.Key("agents.conversation.user_turn.id") // RunTraceID is set on spans outside an agent run's trace (the message // hook) to name the trace the run was recorded under. RunTraceID = attribute.Key("agents.agent.run.trace_id") )
Attribute keys for Mattermost entities
var ( StreamOutcome = attribute.Key("agents.stream.outcome") StreamMessageLength = attribute.Key("agents.stream.message_length") // bytes StreamContinuation = attribute.Key("agents.stream.continuation") )
Attribute keys for post streaming
var ( ABACResourceType = attribute.Key("agents.abac.resource_type") ABACResourceID = attribute.Key("agents.abac.resource_id") ABACResourceName = attribute.Key("agents.abac.resource_name") ABACOutcome = attribute.Key("agents.abac.outcome") // ABACDenied is true when the check denied access. A denial is an // expected decision, so it does not set the span status to Error. ABACDenied = attribute.Key("agents.abac.denied") // ABACDecisionSource is set when something other than an access policy // made the decision ("legacy": the agent's allow/block lists). ABACDecisionSource = attribute.Key("agents.abac.decision_source") // ABACAvailable is the result of the ABAC availability probe; on failure // ABACProbeStatusCode and ABACProbeErrorID say why. ABACAvailable = attribute.Key("agents.abac.available") ABACProbeStatusCode = attribute.Key("agents.abac.probe_status_code") ABACProbeErrorID = attribute.Key("agents.abac.probe_error_id") )
Attribute keys for attribute-based access control (ABAC) decisions
var ( // AmbientShouldReply is the ambient classifier's decision; it is unset // when classification failed. AmbientShouldReply = attribute.Key("agents.ambient.should_reply") )
Attribute keys for ambient auto-reply
Functions ¶
func DetachContext ¶
DetachContext returns a context whose lifetime is independent of ctx (it derives from the plugin root context, canceled on deactivation) but preserves the active OpenTelemetry span. Use this when handing a request context to background work (post streaming, async processing) that must continue after the originating HTTP handler returns. Without it, the request context's cancellation propagates into the background goroutine and truncates the work.
func LLMRoundFromContext ¶ added in v2.10.0
LLMRoundFromContext returns the round set by WithLLMRound.
func NewLogSpanProcessor ¶
func NewLogSpanProcessor(log LogService) sdktrace.SpanProcessor
NewLogSpanProcessor returns a SpanProcessor that emits one log entry per finished span. Spans whose status code is Error are logged at Error level; everything else at Info.
func NewTurnIDGenerator ¶
func NewTurnIDGenerator() sdktrace.IDGenerator
NewTurnIDGenerator returns an IDGenerator suitable for sdktrace.WithIDGenerator that produces deterministic IDs derived from the turn ID stashed in ctx via WithTurnID, and falls back to the SDK's default random generator otherwise.
func ParseHeaders ¶ added in v2.10.0
ParseHeaders parses OTLP headers in the OTEL_EXPORTER_OTLP_HEADERS format: comma-separated key=value pairs with percent-encoded values. The error never echoes a value, since values are credentials.
func SpanContextForTurn ¶
func SpanContextForTurn(turnID string) trace.SpanContext
SpanContextForTurn returns a valid SpanContext anchored to the trace associated with turnID, suitable for use as a trace.Link target. The SpanID matches what the run's root span would have, so a link points to a real, locatable trace in Tempo even though the link's exact span ID won't necessarily resolve.
func SpanFromContext ¶
SpanFromContext extracts the current span from context.
func WithLLMAttributes ¶
func WithLLMAttributes(provider, model, operation string, streaming bool) trace.SpanStartOption
WithLLMAttributes returns a SpanStartOption with standard LLM attributes.
func WithLLMRound ¶ added in v2.10.0
WithLLMRound records on ctx the 0-based index of the next LLM call within a tool loop, so the LLM span can carry agents.llm.round.
Types ¶
type LogService ¶
type LogService interface {
Info(message string, keyValuePairs ...any)
Error(message string, keyValuePairs ...any)
}
LogService is the subset of pluginapi.LogService that the log span processor needs. Defined locally so tests can supply a fake without pulling in the plugin API.
type OTLPOptions ¶ added in v2.10.0
type OTLPOptions struct {
// Endpoint is the collector's host:port.
Endpoint string
// TLS dials the endpoint over TLS (system root CAs) instead of plaintext.
TLS bool
// Headers are sent as gRPC metadata on every export, typically an
// Authorization header for a hosted collector. They are credentials:
// never log them.
Headers map[string]string
}
OTLPOptions configures the OTLP gRPC exporter.
type Options ¶ added in v2.10.0
type Options struct {
ServiceName string
ServiceVersion string
Mode OutputMode
// OTLP is required for OutputModeOTLP.
OTLP OTLPOptions
// Log is required for OutputModeLogs.
Log LogService
}
Options selects and configures the trace output.
type OutputMode ¶
type OutputMode string
OutputMode selects where finished spans are sent.
const ( // OutputModeOff disables tracing entirely (no-op TracerProvider). OutputModeOff OutputMode = "off" // OutputModeLogs writes spans to the Mattermost server log via // pluginapi.LogService. No collector required. OutputModeLogs OutputMode = "logs" // OutputModeOTLP exports spans to an OTLP gRPC endpoint such as // Grafana Tempo or Jaeger. OutputModeOTLP OutputMode = "otlp" )
type ShutdownFunc ¶
ShutdownFunc is returned by Init and must be called to flush pending spans.
func Init ¶
func Init(ctx context.Context, opts Options) (ShutdownFunc, error)
Init sets up the global TracerProvider for the selected output mode.
- OutputModeOff registers a no-op provider.
- OutputModeLogs writes finished spans through opts.Log.
- OutputModeOTLP exports spans to the OTLP gRPC endpoint.