code: func (s *State) checkSettings(c context.Context) (*tsmon.State, *tsmonSettings) {
  	state := tsmon.GetState(c)
  
  	var settings tsmonSettings
  	if s.testingSettings != nil {
  		settings = *s.testingSettings
  	} else {
  		settings = fetchCachedSettings(c)
  	}
  
  	// Read the values used when handling previous request. In most cases they
  	// are identical to current ones and we can skip grabbing a heavier write
  	// lock.
  	s.lock.RLock()
  	if s.state == state && s.lastSettings == settings {
  		s.lock.RUnlock()
  		return state, &settings
  	}
  	s.lock.RUnlock()
  
  	// 'settings' or 'state' has changed. Reinitialize tsmon as needed under
  	// the write lock.
  	s.lock.Lock()
  	defer s.lock.Unlock()
  
  	// First call to 'checkSettings' ever?
  	if s.state == nil {
  		s.state = state
  		s.state.SetMonitor(monitor.NewNilMonitor()) // doFlush uses its own monitor
  		s.state.InhibitGlobalCallbacksOnFlush()
  	} else if state != s.state {
  		panic("tsmon state in the context was unexpectedly changed between requests")
  	}
  
  	switch {
  	case !bool(s.lastSettings.Enabled) && bool(settings.Enabled):
  		s.enableTsMon(c)
  	case bool(s.lastSettings.Enabled) && !bool(settings.Enabled):
  		s.disableTsMon(c)
  	}
  	s.lastSettings = settings
  
  	return state, &settings
  }
docs: // checkSettings fetches tsmon settings and initializes, reinitializes or
  // deinitializes tsmon, as needed.
  //
  // Returns current tsmon state and settings. Panics if the context is using
  // unexpected tsmon state.