{"id":19312133,"url":"https://github.com/bigcommerce/bc-lightstep-ruby","last_synced_at":"2025-04-22T15:31:59.501Z","repository":{"id":41142877,"uuid":"132977831","full_name":"bigcommerce/bc-lightstep-ruby","owner":"bigcommerce","description":"Generic lightstep library for distributed tracing in ruby","archived":false,"fork":false,"pushed_at":"2024-04-11T16:41:43.000Z","size":205,"stargazers_count":2,"open_issues_count":1,"forks_count":3,"subscribers_count":37,"default_branch":"main","last_synced_at":"2024-10-18T00:18:20.806Z","etag":null,"topics":["lightstep","ruby","tracing"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bigcommerce.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2018-05-11T02:18:55.000Z","updated_at":"2023-02-05T17:19:16.000Z","dependencies_parsed_at":"2023-11-20T23:31:40.269Z","dependency_job_id":"eeeab82c-4c36-445c-85a6-469b6837dab0","html_url":"https://github.com/bigcommerce/bc-lightstep-ruby","commit_stats":{"total_commits":142,"total_committers":5,"mean_commits":28.4,"dds":"0.035211267605633756","last_synced_commit":"29b98ccd1a38f069088f6ebe22a506f97bcf2fbe"},"previous_names":[],"tags_count":45,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bigcommerce%2Fbc-lightstep-ruby","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bigcommerce%2Fbc-lightstep-ruby/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bigcommerce%2Fbc-lightstep-ruby/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bigcommerce%2Fbc-lightstep-ruby/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bigcommerce","download_url":"https://codeload.github.com/bigcommerce/bc-lightstep-ruby/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223900372,"owners_count":17222028,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["lightstep","ruby","tracing"],"created_at":"2024-11-10T00:33:01.517Z","updated_at":"2024-11-10T00:33:02.415Z","avatar_url":"https://github.com/bigcommerce.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# bc-lightstep-ruby - LightStep distributed tracing\n\n[![CircleCI](https://circleci.com/gh/bigcommerce/bc-lightstep-ruby/tree/main.svg?style=svg)](https://circleci.com/gh/bigcommerce/bc-lightstep-ruby/tree/main) [![Gem Version](https://badge.fury.io/rb/bc-lightstep-ruby.svg)](https://badge.fury.io/rb/bc-lightstep-ruby) [![Inline docs](http://inch-ci.org/github/bigcommerce/bc-lightstep-ruby.svg?branch=main)](http://inch-ci.org/github/bigcommerce/bc-lightstep-ruby) [![Maintainability](https://api.codeclimate.com/v1/badges/72191c29a56368431942/maintainability)](https://codeclimate.com/github/bigcommerce/bc-lightstep-ruby/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/72191c29a56368431942/test_coverage)](https://codeclimate.com/github/bigcommerce/bc-lightstep-ruby/test_coverage)\n\nAdds [LightStep](https://lightstep.com) tracing support for Ruby. This is an extension of the\n[LightStep ruby gem](https://github.com/lightstep/lightstep-tracer-ruby) and adds extra functionality and resiliency.\n\n## Installation\n\n```ruby\ngem 'bc-lightstep-ruby'\n```\n\nThen in an initializer or before use:\n\n```ruby\nrequire 'bigcommerce/lightstep'\nBigcommerce::Lightstep.configure do |c|\n  c.component_name = 'myapp'\n  c.access_token = 'abcdefg'\n  c.host = 'my.lightstep.service.io'\n  c.port = 8080\n  c.verbosity = 1\nend\n```\n\nThen in your script:\n\n```ruby\ntracer = Bigcommerce::Lightstep::Tracer.instance\ntracer.start_span('my-span', context: request.headers) do |span|\n  span.set_tag('my-tag', 'value')\n  # do thing to measure\nend\n```\n\n### Environment Config\n\nbc-lightstep-ruby can be automatically configured from these ENV vars, if you'd rather use that instead:\n\n| Name | Description |\n| ---- | ----------- |\n| LIGHTSTEP_ENABLED | Flag to determine whether to broadcast spans. Defaults to (1) enabled, 0 will disable.| 1 |\n| LIGHTSTEP_COMPONENT_NAME | The component name to use | '' |\n| LIGHTSTEP_ACCESS_TOKEN | The access token to use to connect to the collector. Optional. | '' |\n| LIGHTSTEP_HOST | Host of the collector. | `lightstep-collector.linkerd` |\n| LIGHTSTEP_PORT | Port of the collector. | `4140` |\n| LIGHTSTEP_HTTP1_ERROR_CODE | The HTTP error code to report in spans for internal errors | 500 |\n| LIGHTSTEP_HTTP1_ERROR_CODE_MINIMUM | The minimum HTTP error code value to be considered an error for span tag purposes. | 500 |\n| LIGHTSTEP_CONTROLLER_PREFIX | The prefix for Rails controllers to use | `controllers.` |\n| LIGHTSTEP_SSL_VERIFY_PEER | If using 443 as the port, toggle SSL verification. | true |\n| LIGHTSTEP_MAX_BUFFERED_SPANS | The maximum number of spans to buffer before dropping. | `1_000` |\n| LIGHTSTEP_MAX_LOG_RECORDS | Maximum number of log records on a span to accept. | `1_000` |\n| LIGHTSTEP_MAX_REPORTING_INTERVAL_SECONDS | The maximum number of seconds to wait before flushing the report to the collector. | 3.0 |\n| LIGHTSTEP_ACTIVE_RECORD_ENABLED | Whether or not to add ActiveRecord mysql spans. Only works with mysql2 gem. | 1 |\n| LIGHTSTEP_ACTIVE_RECORD_ALLOW_AS_ROOT_SPAN | Allow ActiveRecord mysql spans to be the root span? | 0 |\n| LIGHTSTEP_ACTIVE_RECORD_SPAN_PREFIX | What to prefix the ActiveRecord mysql span with | '' |\n| LIGHTSTEP_REDIS_ALLOW_AS_ROOT_SPAN | Allow redis to be the root span? | 0 |\n| LIGHTSTEP_REDIS_EXCLUDED_COMMANDS | Redis commands to exclude from spans. Comma-separated list. | ping |\n| LIGHTSTEP_VERBOSITY | The verbosity level of lightstep logs. | 1 |\n\nMost systems will only need to customize the component name.\n\n### Instrumenting Rails Controllers\n\nJust drop this include into ApplicationController:\n\n```ruby\ninclude Bigcommerce::Lightstep::RailsControllerInstrumentation\n```\n\n### Faraday Middleware\n\nTo use the supplied faraday middleware, simply:\n\n```ruby\nFaraday.new do |faraday|\n  faraday.use Bigcommerce::Lightstep::Middleware::Faraday, 'name-of-external-service'\nend\n```\n\nSpans will be built with the external service name. It's generally _not_ a good idea to use the Faraday adapter\nwith internal services that are also instrumented with LightStep - use the Faraday adapter on external services\nor systems outside of your instrumenting control.\n\n### Redis\n\nThis gem will automatically detect and instrument Redis calls when they are made using the `Redis::Client` class.\nIt will set as tags on the span the host, port, db instance, and the command (but no arguments).\n\nNote that this will not record redis timings if they are a root span. This is to prevent trace spamming. You can\nre-enable this by setting the `redis_allow_root_spans` configuration option to `true`.\n\nIt also excludes `ping` commands, and you can provide a custom list by setting the `redis_excluded_commands`\nconfiguration option to an array of commands to exclude.\n\n### ActiveRecord and MySQL\n\nThis gem will automatically instrument MySQL queries with spans when made with the `mysql2` gem and ActiveRecord.\nIt will set as tags on the span the host, database type, database name, and a sanitized version of the SQL query made.\nThe query will have no values - replaced with `?` - to ensure secure logging and no leaking of PII data.\n\nNote that this will not record mysql timings if they are a root span. This is to prevent trace spamming. You can\nconfigure this gem to allow it via ENV, but it is not recommended.\n\nBy default, it will also exclude `COMMIT`, `SCHEMA`, and `SHOW FULL FIELDS` queries.\n\n### Individual methods\n\nYou can easily instrument individual methods with the Traceable module and `trace` method:\n\n```ruby\nclass MyService\n  include ::Bigcommerce::Lightstep::Traceable\n\n  trace :call, 'operation.do-my-thing' do |span:, product:, options:|\n    span.set_tag('product_id', product.id)\n  end\n  # or, with no block:\n  trace :call, 'operation.do-my-thing'\n\n  def call(product:, options:)\n    # ...\n  end\nend\n```\n\n#### Tracing Positional Argument Methods\n\nFor positional argument methods, the behavior is a bit different. In your trace call, if tracing a method with\npositional arguments, you'll need to have the block arguments be positional as well:\n\n```ruby\nclass MyService\n  include ::Bigcommerce::Lightstep::Traceable\n\n  trace :positional, 'operation.do-my-thing' do |span, product, options|\n    span.set_tag('product_id', product.id)\n  end\n  def positional(product, options = {})\n    # ...\n  end\nend\n```\n\nNote that any default values in the argument will not carry over into the trace block. Secondly, with positional\nargument methods that have only a _single_ hash argument, since this library has no way to detect in that case if it\nis keyword-arguments or a single hash argument, the library will simply add the `span` to the hash itself, and you'll\nneed to adjust the trace block accordingly:\n\n```ruby\n\nclass MyService\n  include ::Bigcommerce::Lightstep::Traceable\n\n  trace :positional_single_hash_arg, 'operation.do-my-thing' do |my_hash|\n    my_hash[:span].set_tag('product_id', my_hash[:product_id])\n  end\n  def positional_single_hash_arg(my_hash)\n    # ...\n  end\nend\n```\n\nIt is recommended for this reason - and others - to never use single-hash positional arguments in Ruby.\n\n## RSpec\n\nThis library comes with a built-in matcher for testing span blocks. In your rspec config:\n\n```ruby\nrequire 'bigcommerce/lightstep/rspec'\n```\n\nThen, in a test:\n\n```ruby\nit 'should create a lightstep span' do\n  expect { my_code_here }.to create_a_lightstep_span(name: 'my-span-name', tags: { tag_one: 'value-here' })\nend\n```\n\n## Global Interceptors\n\nThis library has global interceptor support that will allow access to each span as it is built. This allows you to\ndynamically inject tags or alter spans as they are collected. You can configure interceptors via an initializer:\n\n```ruby\nBigcommerce::Lightstep.configure do |c|\n  c.interceptors.use(MyInterceptor, an_option: 123)\n  # or, alternatively:\n  c.interceptors.use(MyInterceptor.new(an_option: 123))\nend\n```\n\nIt's important to note that this is a CPU-intensive operation as interceptors will be run for every `start_span` tag,\nso don't build interceptors that require lots of processing power or that would impact latencies.\n\n### ENV Interceptor\n\nProvided out of the box is an interceptor to automatically inject ENV vars into span tags. You can configure like so:\n\n```ruby\nBigcommerce::Lightstep.configure do |c|\n  c.interceptors.use(::Bigcommerce::Lightstep::Interceptors::Env.new(\n    keys: {\n      version: 'VERSION'\n    },\n    presets: [:nomad, :hostname]\n  ))\nend\n```\n\nThe `keys` argument allows you to pass a `span tag =\u003e ENV key` mapping that will assign those ENV vars to spans. The\n`presets` argument comes with a bunch of preset mappings you can use rather than manually mapping them yourself.\n\nNote that this interceptor _must_ be instantiated in configuration, rather than passing the class and options,\nas it needs to pre-materialize the ENV values to reduce CPU usage.\n\n## License\n\nCopyright (c) 2018-present, BigCommerce Pty. Ltd. All rights reserved\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated\ndocumentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the\nrights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit\npersons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the\nSoftware.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE\nWARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\nCOPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR\nOTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbigcommerce%2Fbc-lightstep-ruby","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbigcommerce%2Fbc-lightstep-ruby","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbigcommerce%2Fbc-lightstep-ruby/lists"}