Skip to content

Commit

Permalink
chore: add factory test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
sdewitt-newrelic committed Apr 30, 2024
1 parent 8f2a5e2 commit 2b3af04
Show file tree
Hide file tree
Showing 6 changed files with 1,146 additions and 82 deletions.
23 changes: 0 additions & 23 deletions src/newrelic_logging/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,26 +287,3 @@ def make_auth_from_env(config: Config) -> dict:
})

raise Exception(f'Wrong or missing grant_type')


def new_authenticator(
instance_config: Config,
data_cache: DataCache,
) -> Authenticator:
token_url = instance_config.get('token_url', env_var_name=SF_TOKEN_URL)

if not token_url:
raise ConfigException('token_url', 'missing token URL')

if 'auth' in instance_config:
return Authenticator(
token_url,
make_auth_from_config(instance_config.sub('auth')),
data_cache,
)

return Authenticator(
token_url,
make_auth_from_env(instance_config),
data_cache
)
28 changes: 19 additions & 9 deletions src/newrelic_logging/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
from .instance import Instance
from .integration import Integration
from .pipeline import Pipeline
from .query.receiver import QueryReceiver
from .limits.receiver import LimitsReceiver
from .telemetry import print_info, Telemetry


Expand Down Expand Up @@ -142,7 +140,7 @@ def new_integration(
numeric_fields_list: set = set(),
):
if not 'instances' in config or len(config['instances']) == 0:
raise Exception('no instances found to run')
raise ConfigException('no instances found to run')

data_format = config.get('newrelic.data_format', 'logs').lower()
if data_format == 'logs':
Expand All @@ -157,13 +155,16 @@ def new_integration(
instances = []

for index, i in enumerate(config['instances']):
if not 'name' in i:
raise ConfigException(f'missing instance name for instance {i}')

instance_config = config.sub(f'instances.{index}.arguments')
instance_config.set_prefix(
instance_config['auth_env_prefix'] \
if 'auth_env_prefix' in instance_config else ''
)

labels = i['labels']
labels = i['labels'] if 'labels' in i else {}
labels['nr-labs'] = 'data'

instances.append(factory.new_instance(
Expand All @@ -185,17 +186,26 @@ def new_new_relic(self, config: Config):
env_var_name=newrelic.NR_LICENSE_KEY,
)

region = config.get('newrelic.api_endpoint')
account_id = config.get('newrelic.account_id', env_var_name=newrelic.NR_ACCOUNT_ID)
account_id = config.get(
'newrelic.account_id',
env_var_name=newrelic.NR_ACCOUNT_ID,
)
if not account_id:
raise NewRelicApiException(f'missing New Relic account ID')

region = config.get('newrelic.api_endpoint')
if region == "US":
logs_api_endpoint = newrelic.US_LOGGING_ENDPOINT
events_api_endpoint = newrelic.US_EVENTS_ENDPOINT.format(account_id=account_id)
events_api_endpoint = newrelic.US_EVENTS_ENDPOINT.format(
account_id=account_id,
)
elif region == "EU":
logs_api_endpoint = newrelic.EU_LOGGING_ENDPOINT
events_api_endpoint = newrelic.EU_EVENTS_ENDPOINT.format(account_id=account_id)
events_api_endpoint = newrelic.EU_EVENTS_ENDPOINT.format(
account_id=account_id,
)
else:
raise NewRelicApiException(f'Invalid region {region}')
raise NewRelicApiException(f'invalid New Relic API region {region}')

return newrelic.NewRelic(
logs_api_endpoint,
Expand Down
12 changes: 0 additions & 12 deletions src/newrelic_logging/telemetry.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


import json
import time
from requests import Session
Expand All @@ -9,16 +7,6 @@
from .newrelic import NewRelic


def singleton(class_):
instances = {}
def getinstance(*args, **kwargs):
if class_ not in instances:
instances[class_] = class_(*args, **kwargs)
return instances[class_]
return getinstance


@singleton
class Telemetry:
logs = []
integration_name = None
Expand Down
182 changes: 169 additions & 13 deletions src/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def list_limits(self, session: Session, api_ver: str = None) -> dict:
class AuthenticatorStub:
def __init__(
self,
config: Config = None,
instance_config: Config = None,
data_cache: DataCache = None,
token_url: str = '',
access_token: str = '',
Expand All @@ -108,7 +108,7 @@ def __init__(
reauthenticate_called: bool = False,
raise_login_error = False,
):
self.config = config
self.instance_config = instance_config
self.data_cache = data_cache
self.token_url = token_url
self.access_token = access_token
Expand Down Expand Up @@ -168,12 +168,14 @@ def reauthenticate(
class DataCacheStub:
def __init__(
self,
config: Config = None,
instance_config: Config = None,
backend_factory: BackendFactory = None,
cached_logs = {},
cached_records = [],
skip_record_ids = [],
):
self.config = config
self.instance_config = instance_config
self.backend_factory = backend_factory
self.cached_logs = cached_logs
self.cached_records = cached_records
self.skip_record_ids = skip_record_ids
Expand Down Expand Up @@ -298,7 +300,7 @@ def new(
class PipelineStub:
def __init__(
self,
config: Config = Config({}),
instance_config: Config = Config({}),
data_cache: DataCache = None,
new_relic: NewRelic = None,
data_format: DataFormat = DataFormat.LOGS,
Expand All @@ -308,7 +310,7 @@ def __init__(
raise_login_error: bool = False,
raise_newrelic_error: bool = False,
):
self.config = config
self.instance_config = instance_config
self.data_cache = data_cache
self.new_relic = new_relic
self.data_format = data_format
Expand All @@ -319,6 +321,10 @@ def __init__(
self.raise_error = raise_error
self.raise_login_error = raise_login_error
self.raise_newrelic_error = raise_newrelic_error
self.receivers = []

def add_receiver(self, receiver) -> None:
self.receivers.append(receiver)

def execute(
self,
Expand Down Expand Up @@ -470,7 +476,12 @@ class InstanceStub:
def __init__(
self,
instance_name: str = '',
config: Config = Config({}),
instance_config: Config = Config({}),
data_format: DataFormat = DataFormat.LOGS,
new_relic: NewRelic = None,
receivers: list[callable] = [],
labels: dict = {},
numeric_fields_list: set = set(),
api: Api = None,
pipeline: Pipeline = None,
queries: list[dict] = None,
Expand All @@ -481,7 +492,12 @@ def __init__(
raise_unexpected_error: bool = False,
):
self.name = instance_name
self.config = config
self.instance_config = instance_config
self.data_format = data_format
self.new_relic = new_relic
self.receivers = receivers
self.labels = labels
self.numeric_fields_list = numeric_fields_list
self.api = api
self.pipeline = pipeline
self.queries = queries
Expand Down Expand Up @@ -515,19 +531,26 @@ class IntegrationStub:
def __init__(
self,
config: Config = Config({}),
event_type_fields_mapping: dict = {},
receivers: list[callable] = [],
numeric_fields_list: set = set(),
initial_delay: int = 0,
):
pass
self.config = config
self.receivers = receivers
self.numeric_fields_list = numeric_fields_list

class ReceiverStub:
def __init__(
self,
instance_config: Config = Config({}),
data_cache: DataCache = None,
api: Api = None,
raise_login_error: bool = False,
raise_error: bool = False,
logs: list[dict] = [],
):
self.instance_config = instance_config
self.data_cache = data_cache
self.api = api
self.executed = False
self.raise_login_error = raise_login_error
self.raise_error = raise_error
Expand Down Expand Up @@ -575,11 +598,11 @@ def get(self, *args, **kwargs):
class TelemetryStub:
def __init__(
self,
integration_name: str = 'test_instance',
config: Config = Config({}),
new_relic: NewRelic = None,
empty: bool = True
):
self.integration_name = integration_name
self.config = config
self.new_relic = new_relic
self.empty = empty
self.flush_called = False
Expand Down Expand Up @@ -607,6 +630,139 @@ def flush(self, session: Session):


class FactoryStub:
def __init__(
self,
backend_factory: BackendFactoryStub = None,
data_cache: DataCacheStub = None,
authenticator: AuthenticatorStub = None,
api: ApiStub = None,
pipeline: PipelineStub = None,
instance: InstanceStub = None,
integration: IntegrationStub = None,
new_relic: NewRelicStub = None,
telemetry: TelemetryStub = None
):
self.backend_factory = backend_factory
self.data_cache = data_cache
self.authenticator = authenticator
self.api = api
self.pipeline = pipeline
self.instance = instance
self.integration = integration
self.new_relic = new_relic
self.telemetry = telemetry

def new_backend_factory(self):
if self.backend_factory:
return self.backend_factory

return BackendFactoryStub()

def new_data_cache(
self,
instance_config: Config,
backend_factory: BackendFactory,
) -> DataCache:
if self.data_cache:
return self.data_cache

return DataCacheStub(instance_config, backend_factory)

def new_authenticator(
self,
instance_config: Config,
data_cache: DataCache,
) -> Authenticator:
if self.authenticator:
return self.authenticator

return AuthenticatorStub(instance_config, data_cache)

def new_api(self, authenticator: Authenticator, api_ver: str):
if self.api:
return self.api

return ApiStub(authenticator, api_ver)

def new_pipeline(
self,
instance_config: Config,
data_cache: DataCache,
new_relic: NewRelic,
data_format: DataFormat,
labels: dict,
numeric_fields_list: set,
) -> Pipeline:
if self.pipeline:
return self.pipeline

return PipelineStub(
instance_config,
data_cache,
new_relic,
data_format,
labels,
numeric_fields_list,
)

def new_instance(
self,
factory,
instance_name: str,
instance_config: Config,
data_format: DataFormat,
new_relic: NewRelic,
receivers: list[callable],
labels: dict,
numeric_fields_list: set = set(),
) -> Instance:
if self.instance:
return self.instance

return InstanceStub(
instance_name,
instance_config,
data_format,
new_relic,
receivers,
labels,
numeric_fields_list,
)

def new_integration(
self,
factory,
config: Config,
receivers: list[callable],
numeric_fields_list: set = set(),
) -> Integration:
if self.integration:
return self.integration

return IntegrationStub(
config,
receivers,
numeric_fields_list,
)

def new_new_relic(self, config: Config):
if self.new_relic:
return self.new_relic

return NewRelicStub(config)

def new_telemetry(
self,
config: Config,
new_relic: NewRelic,
):
if self.telemetry:
return self.telemetry

return TelemetryStub(config, new_relic)


class DelegatingFactoryStub:
def __init__(
self,
f: Factory,
Expand Down
Loading

0 comments on commit 2b3af04

Please sign in to comment.