Can we covergroup inside a monitor

In reply to Vickyvinayk:

I’m using a coverage component in each agent, because I want to measure what I was stimulating with the sequencer/driver. It looks like this:

//-----------------------------------------------------------------------------
class apb_coverage extends uvm_subscriber #(apb_seq_item);
//-----------------------------------------------------------------------------

`uvm_component_utils(apb_coverage)
  apb_seq_item item;

  // Insert your covergroups here

  //------------------------------------------
  // Cover Group(s)
  //------------------------------------------
  covergroup apb_cov;
  // insert your coverpoints and bins here
    cp_0: coverpoint item.data;
    //   add bins here
    cp_1: coverpoint item.addr;
    //   add bins here
    cp_2: coverpoint item.we;
    //   add bins here
    cp_3: coverpoint item.delay;
    //   add bins here
    cp_4: coverpoint item.error;
    //   add bins here
  endgroup

  extern function new(string name = "apb_coverage", uvm_component parent);
  extern function void build_phase(uvm_phase phase);
  extern function void write(input apb_seq_item t);
endclass : apb_coverage 

//-----------------------------------------------------------------------------
// apb_coverage implementation
//-----------------------------------------------------------------------------
function apb_coverage::new(string name = "apb_coverage", uvm_component parent);
   super.new(name,parent);
   apb_is_covered = 0;
   apb_cov = new();
endfunction : new

function void apb_coverage::build_phase(uvm_phase phase);
  super.build_phase(phase);
endfunction : build_phase

function void apb_coverage::write(apb_seq_item t);
  item = t;
  apb_cov.sample();
endfunction : write