Skip to content

Commit

Permalink
Merge pull request #5249 from sul-dlss/index_tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
mjgiarlo authored Jan 15, 2025
2 parents 3e0f218 + 9c7c870 commit fda1062
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 29 deletions.
9 changes: 5 additions & 4 deletions app/jobs/reindex_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ class ReindexJob < ApplicationJob
# @param [Hash] model the cocina object attributes (without metadata)
# @param [DateTime] created the time the object was created
# @param [DateTime] modified the time the object was last modified
def perform(model:, created:, modified:)
# @param [String] trace_id the trace id
def perform(model:, created:, modified:, trace_id:)
cocina_object = Cocina::Models.build(model)
cocina_object_with_metadata = Cocina::Models.with_metadata(cocina_object, 'void', created:, modified:)
Indexer.reindex(cocina_object: cocina_object_with_metadata)
Indexer.reindex(cocina_object: cocina_object_with_metadata, trace_id: trace_id)
rescue CocinaObjectStore::CocinaObjectStoreError => e
Rails.logger.error("Error reindexing #{model[:externalIdentifier]}: #{e.message}")
Honeybadger.notify(e, context: { druid: model[:externalIdentifier] })
Rails.logger.error("Error reindexing #{model[:externalIdentifier]} - trace_id #{trace_id}: #{e.message}")
Honeybadger.notify(e, context: { druid: model[:externalIdentifier], trace_id: })
end
end
17 changes: 13 additions & 4 deletions app/services/indexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
# Indexes a Cocina object.
class Indexer
# @param [Cocina::Models::DROWithMetadata|CollectionWithMetadata|AdminPolicyWithMetadata]
def self.reindex(cocina_object:)
def self.reindex(cocina_object:, trace_id: nil)
return unless Settings.solr.enabled

solr_doc = Indexing::Builders::DocumentBuilder.for(
model: cocina_object
model: cocina_object,
trace_id: trace_id || trace_id_for(druid: cocina_object.externalIdentifier)
).to_solr
solr.add(solr_doc)
solr.commit
Expand All @@ -21,16 +22,24 @@ def self.delete(druid:)
end

# @param [Cocina::Models::DROWithMetadata|CollectionWithMetadata|AdminPolicyWithMetadata]
def self.reindex_later(cocina_object:)
def self.reindex_later(cocina_object:, trace_id: nil)
ReindexJob.perform_later(
model: cocina_object.to_h,
created: cocina_object.created,
modified: cocina_object.modified
modified: cocina_object.modified,
trace_id: trace_id || trace_id_for(druid: cocina_object.externalIdentifier)
)
end

def self.solr
RSolr.connect(timeout: 120, open_timeout: 120, url: Settings.solr.url)
end
private_class_method :solr

def self.trace_id_for(druid:)
source = Kernel.caller_locations(2, 1).first.to_s.delete_prefix("#{Rails.root}/") # rubocop:disable Rails/FilePath
SecureRandom.uuid.tap do |trace_id|
Rails.logger.info("Reindexing #{druid} from #{source} with trace_id=#{trace_id}")
end
end
end
8 changes: 5 additions & 3 deletions app/services/indexing/builders/document_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,18 @@ def self.reset_parent_collections
@@parent_collections = {} # rubocop:disable Style/ClassVars
end

def initialize(model:)
def initialize(model:, trace_id: SecureRandom.uuid)
@model = model
@trace_id = trace_id
end

# @param [Cocina::Models::DROWithMetadata,Cocina::Models::CollectionWithMetadata,Cocina::Model::AdminPolicyWithMetadata] model
def for
indexer_for_type(model.type).new(id:,
cocina: model,
parent_collections:,
administrative_tags:)
administrative_tags:,
trace_id:)
rescue StandardError => e
Honeybadger.notify('[DATA ERROR] Unexpected indexing exception',
tags: 'data_error',
Expand All @@ -77,7 +79,7 @@ def for

private

attr_reader :model, :workflow_client
attr_reader :model, :workflow_client, :trace_id

def id
model.externalIdentifier
Expand Down
6 changes: 4 additions & 2 deletions app/services/indexing/indexers/basic_indexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ module Indexing
module Indexers
# Basic indexing for any object
class BasicIndexer
attr_reader :cocina, :workflow_client
attr_reader :cocina, :workflow_client, :trace_id

def initialize(cocina:, **)
def initialize(cocina:, trace_id:, **)
@cocina = cocina
@trace_id = trace_id
@workflow_client = workflow_client
end

# @return [Hash] the partial solr document for basic data
def to_solr
{}.tap do |solr_doc|
solr_doc[:id] = cocina.externalIdentifier
solr_doc['trace_id_ss'] = trace_id
solr_doc['current_version_isi'] = cocina.version # Argo Facet field "Version"
solr_doc['obj_label_tesim'] = cocina.label

Expand Down
3 changes: 2 additions & 1 deletion bin/rolling_index
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ Daemons.run_proc(
cocina_object = CocinaObjectStore.find(identifier)
# This returns a Solr doc hash
Indexing::Builders::DocumentBuilder.for(
model: cocina_object
model: cocina_object,
trace_id: Indexer.trace_id_for(druid: identifier)
).to_solr
rescue CocinaObjectStore::CocinaObjectNotFoundError
Honeybadger.notify('Rolling indexer cannot reindex since not found.', { druid: identifier })
Expand Down
7 changes: 4 additions & 3 deletions spec/jobs/reindex_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

RSpec.describe ReindexJob do
subject(:perform) do
described_class.perform_now(model: dro.to_h, created: Time.zone.now, modified: Time.zone.now)
described_class.perform_now(model: dro.to_h, created: Time.zone.now, modified: Time.zone.now, trace_id:)
end

let(:dro) { build(:dro) }
let(:trace_id) { 'abc123' }

context 'when no errors' do
before do
Expand All @@ -16,7 +17,7 @@

it 'invokes the Indexer' do
perform
expect(Indexer).to have_received(:reindex).with(cocina_object: an_instance_of(Cocina::Models::DROWithMetadata))
expect(Indexer).to have_received(:reindex).with(cocina_object: an_instance_of(Cocina::Models::DROWithMetadata), trace_id:)
end
end

Expand All @@ -28,7 +29,7 @@

it 'Honeybadger alerts' do
perform
expect(Indexer).to have_received(:reindex).with(cocina_object: an_instance_of(Cocina::Models::DROWithMetadata))
expect(Indexer).to have_received(:reindex).with(cocina_object: an_instance_of(Cocina::Models::DROWithMetadata), trace_id:)
expect(Honeybadger).to have_received(:notify)
end
end
Expand Down
37 changes: 32 additions & 5 deletions spec/services/indexer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
let(:indexer) { double(to_solr: solr_doc) } # rubocop:disable RSpec/VerifiedDoubles
let(:solr_doc) { instance_double(Hash) }
let(:solr) { instance_double(RSolr::Client, add: nil, commit: nil, delete_by_id: nil) }
let(:trace_id) { 'abc123' }
let(:generated_trace_id) { 'def456' }

before do
allow(RSolr).to receive(:connect).and_return(solr)
allow(SecureRandom).to receive(:uuid).and_return(generated_trace_id)
end

describe '#reindex' do
Expand All @@ -20,9 +23,10 @@
end

it 'reindexes the object' do
described_class.reindex(cocina_object:)
described_class.reindex(cocina_object:, trace_id:)
expect(Indexing::Builders::DocumentBuilder).to have_received(:for).with(
model: cocina_object
model: cocina_object,
trace_id:
)
expect(solr).to have_received(:add).with(solr_doc)
expect(solr).to have_received(:commit)
Expand All @@ -34,12 +38,22 @@
end

it 'does not reindex the object' do
described_class.reindex(cocina_object:)
described_class.reindex(cocina_object:, trace_id:)
expect(Indexing::Builders::DocumentBuilder).not_to have_received(:for)
expect(solr).not_to have_received(:add)
expect(solr).not_to have_received(:commit)
end
end

context 'when trace_id is not provided' do
it 'generates a trace_id' do
described_class.reindex(cocina_object:)
expect(Indexing::Builders::DocumentBuilder).to have_received(:for).with(
model: cocina_object,
trace_id: generated_trace_id
)
end
end
end

describe '#delete' do
Expand Down Expand Up @@ -68,12 +82,25 @@
end

it 'reindexes the object later' do
described_class.reindex_later(cocina_object:)
described_class.reindex_later(cocina_object:, trace_id:)
expect(ReindexJob).to have_received(:perform_later).with(
model: cocina_object.to_h,
created: cocina_object.created,
modified: cocina_object.modified
modified: cocina_object.modified,
trace_id:
)
end

context 'when trace_id is not provided' do
it 'generates a trace_id' do
described_class.reindex_later(cocina_object:)
expect(ReindexJob).to have_received(:perform_later).with(
model: cocina_object.to_h,
created: cocina_object.created,
modified: cocina_object.modified,
trace_id: generated_trace_id
)
end
end
end
end
10 changes: 7 additions & 3 deletions spec/services/indexing/builders/document_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

RSpec.describe Indexing::Builders::DocumentBuilder do
subject(:indexer) do
described_class.for(model: cocina_with_metadata)
described_class.for(model: cocina_with_metadata, trace_id:)
end

let(:cocina_with_metadata) do
Cocina::Models.with_metadata(cocina, 'unknown_lock', created: DateTime.parse('Wed, 01 Jan 2020 12:00:01 GMT'),
modified: DateTime.parse('Thu, 04 Mar 2021 23:05:34 GMT'))
end
let(:druid) { 'druid:xx999xx9999' }
let(:trace_id) { 'abc123' }

let(:releasable) do
instance_double(Indexing::Indexers::ReleasableIndexer, to_solr: { 'released_to_ssim' => %w[searchworks earthworks] })
end
Expand Down Expand Up @@ -68,7 +70,8 @@
before do
allow(CocinaObjectStore).to receive(:find).and_return(related)
described_class.for(
model: cocina_with_metadata
model: cocina_with_metadata,
trace_id:
)
end

Expand All @@ -94,7 +97,8 @@
.with(cocina: Cocina::Models::DROWithMetadata,
id: String,
administrative_tags: [],
parent_collections: [])
parent_collections: [],
trace_id:)
expect(Honeybadger).to have_received(:notify).with('Bad association found on druid:xx999xx9999. druid:bc999df2323 could not be found')
end
end
Expand Down
12 changes: 8 additions & 4 deletions spec/services/indexing/indexers/basic_indexer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
let(:indexer) do
Indexing::Indexers::CompositeIndexer.new(
described_class
).new(id: 'druid:ab123cd4567', cocina:, workflow_client: instance_double(Dor::Workflow::Client))
).new(id: 'druid:ab123cd4567', cocina:, workflow_client: instance_double(Dor::Workflow::Client), trace_id:)
end
let(:doc) { indexer.to_solr }
let(:trace_id) { 'abc123' }

context 'with collections' do
let(:structural) do
Expand All @@ -42,7 +43,8 @@
'is_member_of_collection_ssim' => ['info:fedora/druid:bb777bb7777', 'info:fedora/druid:dd666dd6666'],
'modified_latest_dttsi' => '2021-03-04T23:05:34Z',
'created_at_dttsi' => '2020-01-01T12:00:01Z',
'id' => 'druid:xx999xx9999'
'id' => 'druid:xx999xx9999',
'trace_id_ss' => 'abc123'
)
end
end
Expand All @@ -62,7 +64,8 @@
'has_constituents_ssim' => nil,
'modified_latest_dttsi' => '2021-03-04T23:05:34Z',
'created_at_dttsi' => '2020-01-01T12:00:01Z',
'id' => 'druid:xx999xx9999'
'id' => 'druid:xx999xx9999',
'trace_id_ss' => 'abc123'
)
end
end
Expand All @@ -82,7 +85,8 @@
'is_member_of_collection_ssim' => [],
'modified_latest_dttsi' => '2021-03-04T23:05:34Z',
'created_at_dttsi' => '2020-01-01T12:00:01Z',
'id' => 'druid:xx999xx9999'
'id' => 'druid:xx999xx9999',
'trace_id_ss' => 'abc123'
)
end
end
Expand Down

0 comments on commit fda1062

Please sign in to comment.