def harvester(cls): pid = os.getpid() harvester = cls._harvesters.get(pid, None) if harvester: return harvester with cls._lock: harvester = cls._harvesters.get(pid, None) if harvester: return harvester insert_key = os.environ["NEW_RELIC_INSERT_KEY"] host = os.environ.get("NEW_RELIC_HOST", None) client = MetricClient(insert_key, host=host) service_name = os.environ.get("NEW_RELIC_SERVICE_NAME", "Airflow") batch = MetricBatch({"service.name": service_name}) _logger.info("PID: %d -- Using New Relic Stats Recorder", pid) harvester = cls._harvesters[pid] = Harvester(client, batch) harvester.start() atexit.register(harvester.stop) return harvester
def recorder(cls): pid = os.getpid() recorder = cls._recorders.get(pid, None) if recorder: return recorder service_name = os.environ.get("NEW_RELIC_SERVICE_NAME", "Airflow") insert_key = os.environ["NEW_RELIC_INSERT_KEY"] client = MetricClient(insert_key) batch = MetricBatch({"service.name": service_name}) use_harvester = cls.use_harvester() _logger.info( "PID: %d -- Using New Relic Stats Recorder -- use_harvester: %r", os.getpid(), use_harvester, ) if use_harvester: recorder = Harvester(client, batch) recorder.start() atexit.register(join_harvester, recorder) else: recorder = batch atexit.register(send_batch, client, batch) cls._recorders[pid] = recorder return recorder
def __init__(self, insert_key, service_name, host=None, interval=5): client = self.client = MetricClient(insert_key=insert_key, host=host) client.add_version_info("NewRelic-OpenCensus-Exporter", __version__) self.views = {} self.count_values = {} # Register an exporter thread for this exporter thread = self._thread = transport.get_exporter_thread( [stats.stats], self, interval=interval) self.interval = thread.interval self._common = { "interval.ms": self.interval * 1000, "attributes": { "service.name": service_name }, }
def make_new_relic_service(options, spectator_helper=None): spectator_helper = spectator_helper or spectator_client.SpectatorClientHelper( options) if 'newrelic' in options and 'insert_key' in options['newrelic']: insert_key = options['newrelic']['insert_key'] elif 'NEWRELIC_INSERT_KEY' in os.environ: insert_key = os.environ['NEWRELIC_INSERT_KEY'] else: raise Exception( "New Relic is enabled but the config file has no New Relic Insights Insert Key option \n" "See https://docs.newrelic.com/docs/insights/insights-data-sources/custom-data/send-custom-events-event-api for details on insert keys" ) if 'NEWRELIC_HOST' in os.environ: host = os.environ['NEWRELIC_HOST'] elif 'newrelic' in options and 'host' in options['newrelic']: host = options['newrelic']['host'] else: host = 'metric-api.newrelic.com' tags = extract_tags(options) metric_client = MetricClient(insert_key, host=host) return NewRelicMetricsService(spectator_helper, metric_client, tags, options)
#!/usr/bin/env python import os from newrelic_telemetry_sdk import GaugeMetric, MetricClient metric_client = MetricClient(os.environ['NEW_RELIC_INSERT_KEY']) temperature = GaugeMetric("temperature", 78.6, {"units": "Farenheit"}) response = metric_client.send(temperature)