github.com/grafana/pyroscope@v1.18.0/docs/sources/configure-client/language-sdks/ruby.md (about)

     1  ---
     2  title: "Ruby"
     3  menuTitle: "Ruby"
     4  description: "Instrumenting Ruby applications for continuous profiling."
     5  weight: 50
     6  aliases:
     7    - /docs/phlare/latest/configure-client/language-sdks/ruby
     8  ---
     9  
    10  # Ruby
    11  
    12  The Ruby Profiler revolutionizes performance tuning in Ruby applications.
    13  Integrated with Pyroscope, it offers real-time performance data, allowing developers to delve deep into their Ruby codebase.
    14  This tool is essential for identifying performance issues, optimizing code efficiency, and enhancing the overall speed and reliability of Ruby applications.
    15  
    16  {{< admonition type="note" >}}
    17  Refer to [Available profiling types](https://grafana.com/docs/pyroscope/<PYROSCOPE_VERSION>/configure-client/profile-types/) for a list of profile types supported by Ruby.
    18  {{< /admonition >}}
    19  
    20  ## Before you begin
    21  
    22  To capture and analyze profiling data, you need either a hosted Pyroscope OSS server or a hosted [Pyroscope instance with Grafana Cloud Profiles](/products/cloud/profiles-for-continuous-profiling/) (requires a free Grafana Cloud account).
    23  
    24  The Pyroscope server can be a local server for development or a remote server for production use.
    25  
    26  ## Add Ruby profiling to your application
    27  
    28  Add the `pyroscope` gem to your Gemfile:
    29  
    30  ```bash
    31  bundle add pyroscope
    32  ```
    33  
    34  ## Configure the Ruby client
    35  
    36  Add the following code to your application. If you're using Rails, put this into `config/initializers` directory. This code will initialize the Pyroscope profiler and start profiling:
    37  
    38  ```ruby
    39  require 'pyroscope'
    40  
    41  Pyroscope.configure do |config|
    42    config.application_name = "my.ruby.app" # replace this with some name for your application
    43    config.server_address   = "http://my-pyroscope-server:4040" # replace this with the address of your Pyroscope server
    44  end
    45  ```
    46  
    47  ## How to add profiling labels to Ruby applications
    48  
    49  The Pyroscope Ruby integration provides a number of ways to tag profiling data. For example, you can provide tags when you're initializing the profiler:
    50  
    51  ```ruby
    52  require 'pyroscope'
    53  
    54  Pyroscope.configure do |config|
    55    config.application_name = "my.ruby.app"
    56    config.server_address   = "http://my-pyroscope-server:4040"
    57  
    58    config.tags = {
    59      "hostname" => ENV["HOSTNAME"],
    60    }
    61  end
    62  ```
    63  
    64  Or you can dynamically tag certain parts of your code:
    65  
    66  ```ruby
    67  Pyroscope.tag_wrapper({ "controller": "slow_controller_i_want_to_profile" }) do
    68    slow_code
    69  end
    70  ```
    71  
    72  ## Rails profiling auto-instrumentation
    73  
    74  By default, if you add Pyroscope to a Rails application it will automatically tag your actions with a `action="<controller_name>/<action_name>"` tag.
    75  
    76  To disable Rails auto-instrumentation, set `autoinstrument_rails` to `false`:
    77  ```ruby
    78  Pyroscope.configure do |config|
    79    config.autoinstrument_rails = false
    80    # more configuration
    81  end
    82  ```
    83  
    84  ## Sending data to Pyroscope OSS or Grafana Cloud Profiles with Ruby SDK
    85  
    86  ```ruby
    87  require "pyroscope"
    88  
    89  Pyroscope.configure do |config|
    90    config.application_name = "example.ruby.app"
    91    config.server_address = "<URL>"
    92    config.basic_auth_username='<User>'
    93    config.basic_auth_password='<Password>'
    94    # Optional Pyroscope tenant ID (only needed if using multi-tenancy). Not needed for Grafana Cloud.
    95    # config.tenant_id='<TenantID>'
    96  end
    97  ```
    98  
    99  To configure the Ruby SDK to send data to Pyroscope, replace the `<URL>` placeholder with the appropriate server URL. This could be the Grafana Cloud URL or your own custom Pyroscope server URL.
   100  
   101  If you need to send data to Grafana Cloud, you'll have to configure HTTP Basic authentication. Replace `<User>` with your Grafana Cloud stack user and `<Password>` with your Grafana Cloud API key.
   102  
   103  If your Pyroscope server has multi-tenancy enabled, you'll need to configure a tenant ID. Replace `<TenantID>` with your Pyroscope tenant ID.
   104  
   105  ### Locate the URL, user, and password in Grafana Cloud Profiles
   106  
   107  [//]: # 'Shared content for URl location in Grafana Cloud Profiles'
   108  [//]: # 'This content is located in /pyroscope/docs/sources/shared/locate-url-pw-user-cloud-profiles.md'
   109  
   110  {{< docs/shared source="pyroscope" lookup="locate-url-pw-user-cloud-profiles.md" version="latest" >}}
   111  
   112  ## Ruby profiling examples
   113  
   114  Check out the following resources to learn more about Ruby profiling:
   115  - [Ruby examples](https://github.com/pyroscope-io/pyroscope/tree/main/examples/language-sdk-instrumentation/ruby) demonstrating how Ruby applications, including Rails, can be profiled with Pyroscope.
   116  - A [Ruby demo](https://play.grafana.org/a/grafana-pyroscope-app/profiles-explorer?searchText=&panelType=time-series&layout=grid&hideNoData=off&explorationType=flame-graph&var-serviceName=pyroscope-rideshare-ruby&var-profileMetricId=process_cpu:cpu:nanoseconds:cpu:nanoseconds&var-dataSource=grafanacloud-profiles) on play.grafana.org.