|
| 1 | +# Starts an Open Telemetry span that abides by the semantic conventions for |
| 2 | +# Generative AI completions. |
| 3 | +# |
| 4 | +# See: https://opentelemetry.io/docs/specs/semconv/gen-ai/gen-ai-spans/#inference |
| 5 | +start_chat_span <- function( |
| 6 | + provider, |
| 7 | + tracer = default_tracer(), |
| 8 | + scope = parent.frame(), |
| 9 | + active = TRUE |
| 10 | +) { |
| 11 | + if (is.null(tracer) || !tracer$is_enabled()) { |
| 12 | + return(NULL) |
| 13 | + } |
| 14 | + # Ensure we set attributes relevant to sampling at span creation time. |
| 15 | + attributes <- list( |
| 16 | + "gen_ai.operation.name" = "chat", |
| 17 | + "gen_ai.system" = tolower(provider@name), |
| 18 | + "gen_ai.request.model" = provider@model |
| 19 | + ) |
| 20 | + if (active) { |
| 21 | + tracer$start_span( |
| 22 | + name = sprintf("chat %s", provider@model), |
| 23 | + options = list(kind = "CLIENT"), |
| 24 | + attributes = attributes, |
| 25 | + scope = scope |
| 26 | + ) |
| 27 | + } else { |
| 28 | + tracer$start_session( |
| 29 | + name = sprintf("chat %s", provider@model), |
| 30 | + options = list(kind = "CLIENT"), |
| 31 | + attributes = attributes, |
| 32 | + session_scope = scope |
| 33 | + ) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +record_chat_span_status <- function(span, result) { |
| 38 | + if (is.null(span) || !span$is_recording()) { |
| 39 | + return(invisible(span)) |
| 40 | + } |
| 41 | + if (!is.null(result$model)) { |
| 42 | + span$set_attribute("gen_ai.response.model", result$model) |
| 43 | + } |
| 44 | + if (!is.null(result$id)) { |
| 45 | + span$set_attribute("gen_ai.response.id", result$id) |
| 46 | + } |
| 47 | + if (!is.null(result$usage)) { |
| 48 | + span$set_attribute("gen_ai.usage.input_tokens", result$usage$prompt_tokens) |
| 49 | + span$set_attribute( |
| 50 | + "gen_ai.usage.output_tokens", |
| 51 | + result$usage$completion_tokens |
| 52 | + ) |
| 53 | + } |
| 54 | + # TODO: Consider setting gen_ai.response.finish_reasons. |
| 55 | + span$set_status("ok") |
| 56 | +} |
| 57 | + |
| 58 | +# Starts an Open Telemetry span that abides by the semantic conventions for |
| 59 | +# Generative AI tool calls. |
| 60 | +# |
| 61 | +# See: https://opentelemetry.io/docs/specs/semconv/gen-ai/gen-ai-spans/#execute-tool-span |
| 62 | +start_tool_span <- function( |
| 63 | + request, |
| 64 | + tracer = default_tracer(), |
| 65 | + scope = parent.frame(), |
| 66 | + active = TRUE |
| 67 | +) { |
| 68 | + if (is.null(tracer) || !tracer$is_enabled()) { |
| 69 | + return(NULL) |
| 70 | + } |
| 71 | + attributes <- compact(list( |
| 72 | + "gen_ai.operation.name" = "execute_tool", |
| 73 | + "gen_ai.tool.name" = request@tool@name, |
| 74 | + "gen_ai.tool.description" = request@tool@description, |
| 75 | + "gen_ai.tool.call.id" = request@id |
| 76 | + )) |
| 77 | + if (active) { |
| 78 | + tracer$start_span( |
| 79 | + name = sprintf("execute_tool %s", request@tool@name), |
| 80 | + options = list(kind = "INTERNAL"), |
| 81 | + attributes = attributes, |
| 82 | + scope = scope |
| 83 | + ) |
| 84 | + } else { |
| 85 | + tracer$start_session( |
| 86 | + name = sprintf("execute_tool %s", request@tool@name), |
| 87 | + options = list(kind = "INTERNAL"), |
| 88 | + attributes = attributes, |
| 89 | + session_scope = scope |
| 90 | + ) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +record_tool_error <- function(span, error) { |
| 95 | + if (is.null(span) || !span$is_recording()) { |
| 96 | + return() |
| 97 | + } |
| 98 | + span$record_exception(error) |
| 99 | + span$set_status("error") |
| 100 | + span$set_attribute("error.type", class(error)[1]) |
| 101 | +} |
| 102 | + |
| 103 | +# Starts an Open Telemetry span that abides by the semantic conventions for |
| 104 | +# Generative AI "agents". |
| 105 | +# |
| 106 | +# See: https://opentelemetry.io/docs/specs/semconv/gen-ai/gen-ai-spans/#inference |
| 107 | +start_agent_span <- function( |
| 108 | + provider, |
| 109 | + tracer = default_tracer(), |
| 110 | + scope = parent.frame(), |
| 111 | + active = TRUE |
| 112 | +) { |
| 113 | + if (is.null(tracer) || !tracer$is_enabled()) { |
| 114 | + return(NULL) |
| 115 | + } |
| 116 | + attributes <- list( |
| 117 | + "gen_ai.operation.name" = "chat", |
| 118 | + "gen_ai.system" = tolower(provider@name) |
| 119 | + ) |
| 120 | + if (active) { |
| 121 | + tracer$start_span( |
| 122 | + name = "invoke_agent", |
| 123 | + options = list(kind = "CLIENT"), |
| 124 | + attributes = attributes, |
| 125 | + scope = scope |
| 126 | + ) |
| 127 | + } else { |
| 128 | + tracer$start_session( |
| 129 | + name = "invoke_agent", |
| 130 | + options = list(kind = "CLIENT"), |
| 131 | + attributes = attributes, |
| 132 | + session_scope = scope |
| 133 | + ) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +default_tracer <- function() { |
| 138 | + if (!is_installed("otel")) { |
| 139 | + return(NULL) |
| 140 | + } |
| 141 | + otel::get_tracer("ellmer") |
| 142 | +} |
0 commit comments