Skip to content

Fix/telemetry #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ puts result.text # Generated text response
The gem supports OpenTelemetry integration for monitoring and observability. You can configure telemetry settings to control what data is recorded and add metadata for better tracing:

**Telemetry Options:**
- `is_enabled`: Enable/disable telemetry (default: false)
- `record_inputs`: Record input messages (default: true, disable for sensitive data)
- `record_outputs`: Record output responses (default: true, disable for sensitive data)
- `enabled`: Enable/disable telemetry (default: true)
- `record_inputs`: Record input messages (default: false, disable for sensitive data)
- `record_outputs`: Record output responses (default: false, disable for sensitive data)
- `function_id`: Identifier for grouping telemetry data by function
- `metadata`: Additional metadata for OpenTelemetry traces (agent identification, service info, etc.)

Expand All @@ -157,7 +157,8 @@ telemetry_settings = Ai::TelemetrySettings.new(
'service.namespace' => 'customer-service',
'cx.application.name' => 'ai-tracing',
'cx.subsystem.name' => 'mastra-agents'
}
},
tracer:nil
)

# Use with text generation
Expand Down
15 changes: 14 additions & 1 deletion lib/ai/clients/mastra.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ module Ai
module Clients
class Mastra < Ai::Client
extend T::Sig
RUBY_TO_API_KEY_MAPPING = T.let({
'enabled' => 'is_enabled'
}.freeze,
T::Hash[String, String])
Comment on lines +11 to +14
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in order to be more idiomatic for ruby, while using the required Mastra API prop. I explored putting it directly in the camelized options with an if clause, but if in the future we adapt other API methods that are not idiomatic for Ruby, this way we just add it to the hash.

I'm changing this because before we had the is_enabled, which was the Mastra prop name, but you told me to use enabled to be more idiomatic ( which I think is a good tip tbh ).

Any other approach you think would be better?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sorry. What I mean is that this feels very complex just for one particular case. When you pass the telemetry settings to the options, couldn't the settings itself know how to serialize themselves (so returning a hash with is_enabled) instead of having the client know about this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes hella more sense, yup, ty


sig { params(endpoint: String).void }
def initialize(endpoint)
Expand Down Expand Up @@ -45,6 +49,11 @@ def generate(agent_name, messages:, options: {})

private

sig { params(key: String).returns(String) }
def map_ruby_key_to_api(key)
RUBY_TO_API_KEY_MAPPING.fetch(key, key)
end

sig do
params(
url: URI::Generic,
Expand All @@ -61,7 +70,11 @@ def response(url:, messages:, options:)
request['Origin'] = Ai.config.origin

# convert to camelCase and unpacking for API compatibility
camelized_options = options.deep_transform_keys { |key| key.to_s.camelize(:lower).to_sym }
camelized_options = options.deep_transform_keys do |key|
mapped_key = map_ruby_key_to_api(key.to_s)
mapped_key.camelize(:lower).to_sym
end

request.body = { messages: messages, **camelized_options }.to_json

response = http.request(request)
Expand Down
2 changes: 1 addition & 1 deletion lib/ai/types/telemetry_settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Ai
class TelemetrySettings < T::Struct
# Enable or disable telemetry. Enabled by default.
const :is_enabled, T::Boolean, default: true
const :enabled, T::Boolean, default: true

# Enable or disable input recording. You might want to disable this to avoid
# recording sensitive information, reduce data transfers, or increase performance.
Expand Down
22 changes: 11 additions & 11 deletions spec/fixtures/vcr_cassettes/mastra_generate_agent_object.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading