Skip to content

[Bug]: Agent name shows "unknown" on grafana #72

Description

@chingjustwe

Description

When a user sends a message to an existing session (one that was created before the current plugin process started), the session.created event never fires for that session. The sessionTotals Map — which tracks per-session agent, token, and cost metadata — therefore has no entry for that session.

The chat.message hook receives the correct input.agent from the user's request, but only mutates an existing sessionTotals entry; it does not create one when absent. As a result, all subsequent event handlers fall through to "unknown".

This affects all OTel signals that carry agent attributes:

Prometheus metrics: opencode_token_usage_tokens_total, opencode_message_count_total, opencode_model_usage_total, opencode_cache_count_total, opencode_cost_usage_total, opencode_session_token_total, opencode_session_cost_total
Loki logs: agent, agent.name, agent.type fields
Tempo traces: agent.name, agent.type span attributes

Steps to reproduce

  1. Start opencode and send several messages to create a session
  2. Restart opencode (or the plugin process)
  3. Continue the existing session by sending a new message
  4. Export telemetry via the plugin to any OTLP backend (e.g., Grafana Alloy + Prometheus/Loki/Tempo)
  5. Observe that all agent-related labels/attributes for this session's metrics, logs, and traces show "unknown" instead of the correct agent name (e.g., "build")

Expected behavior

When a user message is sent to an existing session, the agent attribute in all telemetry signals should reflect the actual agent name from the request (e.g., "build", "plan", "explore").

Actual behavior

Agent attributes are reported as "unknown" for messages sent to sessions that existed before the current plugin process started.

Plugin version

v1.2.0 (latest)

Configuration

N/A

Relevant log output

Image

Checklist

  • I have searched existing issues to ensure this hasn't been reported
  • I have redacted any sensitive information (API keys, tokens, etc.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions