Search in sources :

Example 1 with MetricBatchSender

use of com.newrelic.telemetry.metrics.MetricBatchSender in project beneficiary-fhir-data by CMSgov.

the class PipelineApplication method main.

/**
 * This method is the one that will get called when users launch the application from the command
 * line.
 *
 * @param args (should be empty, as this application accepts configuration via environment
 *     variables)
 * @throws Exception any unhandled checked {@link Exception}s that are encountered will cause the
 *     application to halt
 */
public static void main(String[] args) throws Exception {
    LOGGER.info("Application starting up!");
    configureUnexpectedExceptionHandlers();
    AppConfiguration appConfig = null;
    try {
        appConfig = AppConfiguration.readConfigFromEnvironmentVariables();
        LOGGER.info("Application configured: '{}'", appConfig);
    } catch (AppConfigurationException e) {
        System.err.println(e.getMessage());
        LOGGER.warn("Invalid app configuration.", e);
        System.exit(EXIT_CODE_BAD_CONFIG);
    }
    MetricRegistry appMetrics = new MetricRegistry();
    appMetrics.registerAll(new MemoryUsageGaugeSet());
    appMetrics.registerAll(new GarbageCollectorMetricSet());
    Slf4jReporter appMetricsReporter = Slf4jReporter.forRegistry(appMetrics).outputTo(LOGGER).build();
    MetricOptions metricOptions = appConfig.getMetricOptions();
    if (metricOptions.getNewRelicMetricKey().isPresent()) {
        SenderConfiguration configuration = SenderConfiguration.builder(metricOptions.getNewRelicMetricHost().orElse(null), metricOptions.getNewRelicMetricPath().orElse(null)).httpPoster(new OkHttpPoster()).apiKey(metricOptions.getNewRelicMetricKey().orElse(null)).build();
        MetricBatchSender metricBatchSender = MetricBatchSender.create(configuration);
        Attributes commonAttributes = new Attributes().put("host", metricOptions.getHostname().orElse("unknown")).put("appName", metricOptions.getNewRelicAppName().orElse(null));
        NewRelicReporter newRelicReporter = NewRelicReporter.build(appMetrics, metricBatchSender).commonAttributes(commonAttributes).build();
        newRelicReporter.start(metricOptions.getNewRelicMetricPeriod().orElse(15), TimeUnit.SECONDS);
    }
    appMetricsReporter.start(1, TimeUnit.HOURS);
    /*
     * Create the PipelineManager that will be responsible for running and managing the various
     * jobs.
     */
    PipelineJobRecordStore jobRecordStore = new PipelineJobRecordStore(appMetrics);
    PipelineManager pipelineManager = new PipelineManager(appMetrics, jobRecordStore);
    registerShutdownHook(appMetrics, pipelineManager);
    LOGGER.info("Job processing started.");
    // Create a pooled data source for use by the DatabaseSchemaUpdateJob.
    final HikariDataSource pooledDataSource = PipelineApplicationState.createPooledDataSource(appConfig.getDatabaseOptions(), appMetrics);
    /*
     * Register and wait for the database schema job to run, so that we don't have to worry about
     * declaring it as a dependency (since it is for pretty much everything right now).
     */
    pipelineManager.registerJob(new DatabaseSchemaUpdateJob(pooledDataSource));
    PipelineJobRecord<NullPipelineJobArguments> dbSchemaJobRecord = jobRecordStore.submitPendingJob(DatabaseSchemaUpdateJob.JOB_TYPE, null);
    try {
        jobRecordStore.waitForJobs(dbSchemaJobRecord);
    } catch (InterruptedException e) {
        pooledDataSource.close();
        throw new InterruptedException();
    }
    /*
     * Create and register the other jobs.
     */
    if (appConfig.getCcwRifLoadOptions().isPresent()) {
        // Create an application state that reuses the existing pooled data source with the ccw/rif
        // persistence unit.
        final PipelineApplicationState appState = new PipelineApplicationState(appMetrics, pooledDataSource, PipelineApplicationState.PERSISTENCE_UNIT_NAME, Clock.systemUTC());
        pipelineManager.registerJob(createCcwRifLoadJob(appConfig.getCcwRifLoadOptions().get(), appState));
        LOGGER.info("Registered CcwRifLoadJob.");
    } else {
        LOGGER.warn("CcwRifLoadJob is disabled in app configuration.");
    }
    if (appConfig.getRdaLoadOptions().isPresent()) {
        LOGGER.info("RDA API jobs are enabled in app configuration.");
        // Create an application state that reuses the existing pooled data source with the rda
        // persistence unit.
        final PipelineApplicationState rdaAppState = new PipelineApplicationState(appMetrics, pooledDataSource, PipelineApplicationState.RDA_PERSISTENCE_UNIT_NAME, Clock.systemUTC());
        final RdaLoadOptions rdaLoadOptions = appConfig.getRdaLoadOptions().get();
        final Optional<RdaServerJob> mockServerJob = rdaLoadOptions.createRdaServerJob();
        if (mockServerJob.isPresent()) {
            pipelineManager.registerJob(mockServerJob.get());
            LOGGER.warn("Registered RdaServerJob.");
        } else {
            LOGGER.info("Skipping RdaServerJob registration - not enabled in app configuration.");
        }
        pipelineManager.registerJob(rdaLoadOptions.createFissClaimsLoadJob(rdaAppState));
        LOGGER.info("Registered RdaFissClaimLoadJob.");
        pipelineManager.registerJob(rdaLoadOptions.createMcsClaimsLoadJob(rdaAppState));
        LOGGER.info("Registered RdaMcsClaimLoadJob.");
    } else {
        LOGGER.info("RDA API jobs are not enabled in app configuration.");
    }
/*
     * At this point, we're done here with the main thread. From now on, the PipelineManager's
     * executor service should be the only non-daemon thread running (and whatever it kicks off).
     * Once/if that thread stops, the application will run all registered shutdown hooks and Wait
     * for the PipelineManager to stop running jobs, and then check to see if we should exit
     * normally with 0 or abnormally with a non-0 because a job failed.
     */
}
Also used : RdaLoadOptions(gov.cms.bfd.pipeline.rda.grpc.RdaLoadOptions) PipelineJobRecordStore(gov.cms.bfd.pipeline.sharedutils.jobs.store.PipelineJobRecordStore) HikariDataSource(com.zaxxer.hikari.HikariDataSource) MetricRegistry(com.codahale.metrics.MetricRegistry) Attributes(com.newrelic.telemetry.Attributes) SenderConfiguration(com.newrelic.telemetry.SenderConfiguration) NullPipelineJobArguments(gov.cms.bfd.pipeline.sharedutils.NullPipelineJobArguments) MetricBatchSender(com.newrelic.telemetry.metrics.MetricBatchSender) PipelineApplicationState(gov.cms.bfd.pipeline.sharedutils.PipelineApplicationState) MemoryUsageGaugeSet(com.codahale.metrics.jvm.MemoryUsageGaugeSet) NewRelicReporter(com.codahale.metrics.newrelic.NewRelicReporter) RdaServerJob(gov.cms.bfd.pipeline.rda.grpc.RdaServerJob) Slf4jReporter(com.codahale.metrics.Slf4jReporter) OkHttpPoster(com.newrelic.telemetry.OkHttpPoster) DatabaseSchemaUpdateJob(gov.cms.bfd.pipeline.sharedutils.databaseschema.DatabaseSchemaUpdateJob) GarbageCollectorMetricSet(com.codahale.metrics.jvm.GarbageCollectorMetricSet)

Example 2 with MetricBatchSender

use of com.newrelic.telemetry.metrics.MetricBatchSender in project dropwizard-metrics-newrelic by newrelic.

the class NewRelicReporterBuilder method build.

public NewRelicReporter build() {
    long rateFactor = rateUnit.toSeconds(1);
    double durationFactor = durationUnit.toNanos(1);
    Predicate<MetricAttribute> metricAttributePredicate = attr -> !disabledMetricAttributes.contains(attr);
    TimeTracker timeTracker = new TimeTracker(Clock.defaultClock());
    MeterTransformer meterTransformer = MeterTransformer.build(timeTracker, rateFactor, metricAttributePredicate, nameCustomizer, attributeCustomizer);
    TimerTransformer timerTransformer = TimerTransformer.build(timeTracker, rateFactor, durationFactor, metricAttributePredicate, nameCustomizer, attributeCustomizer);
    GaugeTransformer gaugeTransformer = new GaugeTransformer(nameCustomizer, attributeCustomizer);
    CounterTransformer counterTransformer = new CounterTransformer(nameCustomizer, attributeCustomizer);
    HistogramTransformer histogramTransformer = HistogramTransformer.build(timeTracker, nameCustomizer, attributeCustomizer);
    return new NewRelicReporter(timeTracker, registry, name, filter, rateUnit, durationUnit, new TelemetryClient(metricBatchSender, null, null, null), commonAttributes, histogramTransformer, gaugeTransformer, counterTransformer, meterTransformer, timerTransformer, disabledMetricAttributes);
}
Also used : Clock(com.codahale.metrics.Clock) TimeTracker(com.codahale.metrics.newrelic.util.TimeTracker) MetricRegistry(com.codahale.metrics.MetricRegistry) MetricAttributesCustomizer(com.codahale.metrics.newrelic.transformer.customizer.MetricAttributesCustomizer) TelemetryClient(com.newrelic.telemetry.TelemetryClient) TimerTransformer(com.codahale.metrics.newrelic.transformer.TimerTransformer) Predicate(java.util.function.Predicate) Attributes(com.newrelic.telemetry.Attributes) Set(java.util.Set) HistogramTransformer(com.codahale.metrics.newrelic.transformer.HistogramTransformer) TimeUnit(java.util.concurrent.TimeUnit) MetricAttribute(com.codahale.metrics.MetricAttribute) CounterTransformer(com.codahale.metrics.newrelic.transformer.CounterTransformer) MeterTransformer(com.codahale.metrics.newrelic.transformer.MeterTransformer) MetricNameCustomizer(com.codahale.metrics.newrelic.transformer.customizer.MetricNameCustomizer) MetricBatchSender(com.newrelic.telemetry.metrics.MetricBatchSender) MetricFilter(com.codahale.metrics.MetricFilter) GaugeTransformer(com.codahale.metrics.newrelic.transformer.GaugeTransformer) Collections(java.util.Collections) HistogramTransformer(com.codahale.metrics.newrelic.transformer.HistogramTransformer) TimerTransformer(com.codahale.metrics.newrelic.transformer.TimerTransformer) MeterTransformer(com.codahale.metrics.newrelic.transformer.MeterTransformer) MetricAttribute(com.codahale.metrics.MetricAttribute) TelemetryClient(com.newrelic.telemetry.TelemetryClient) CounterTransformer(com.codahale.metrics.newrelic.transformer.CounterTransformer) TimeTracker(com.codahale.metrics.newrelic.util.TimeTracker) GaugeTransformer(com.codahale.metrics.newrelic.transformer.GaugeTransformer)

Example 3 with MetricBatchSender

use of com.newrelic.telemetry.metrics.MetricBatchSender in project beneficiary-fhir-data by CMSgov.

the class SpringConfiguration method metricRegistry.

/**
 * @return the {@link MetricRegistry} for the application, which can be used to collect statistics
 *     on the application's performance
 */
@Bean
public MetricRegistry metricRegistry() {
    MetricRegistry metricRegistry = new MetricRegistry();
    metricRegistry.registerAll(new MemoryUsageGaugeSet());
    metricRegistry.registerAll(new GarbageCollectorMetricSet());
    String newRelicMetricKey = System.getenv("NEW_RELIC_METRIC_KEY");
    if (newRelicMetricKey != null) {
        String newRelicAppName = System.getenv("NEW_RELIC_APP_NAME");
        String newRelicMetricHost = System.getenv("NEW_RELIC_METRIC_HOST");
        String newRelicMetricPath = System.getenv("NEW_RELIC_METRIC_PATH");
        String rawNewRelicPeriod = System.getenv("NEW_RELIC_METRIC_PERIOD");
        int newRelicPeriod;
        try {
            newRelicPeriod = Integer.parseInt(rawNewRelicPeriod);
        } catch (NumberFormatException ex) {
            newRelicPeriod = 15;
        }
        String hostname;
        try {
            hostname = InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
            hostname = "unknown";
        }
        SenderConfiguration configuration = SenderConfiguration.builder(newRelicMetricHost, newRelicMetricPath).httpPoster(new OkHttpPoster()).apiKey(newRelicMetricKey).build();
        MetricBatchSender metricBatchSender = MetricBatchSender.create(configuration);
        Attributes commonAttributes = new Attributes().put("host", hostname).put("appName", newRelicAppName);
        NewRelicReporter newRelicReporter = NewRelicReporter.build(metricRegistry, metricBatchSender).commonAttributes(commonAttributes).build();
        newRelicReporter.start(newRelicPeriod, TimeUnit.SECONDS);
    }
    return metricRegistry;
}
Also used : MemoryUsageGaugeSet(com.codahale.metrics.jvm.MemoryUsageGaugeSet) UnknownHostException(java.net.UnknownHostException) NewRelicReporter(com.codahale.metrics.newrelic.NewRelicReporter) MetricRegistry(com.codahale.metrics.MetricRegistry) Attributes(com.newrelic.telemetry.Attributes) SenderConfiguration(com.newrelic.telemetry.SenderConfiguration) OkHttpPoster(com.newrelic.telemetry.OkHttpPoster) MetricBatchSender(com.newrelic.telemetry.metrics.MetricBatchSender) GarbageCollectorMetricSet(com.codahale.metrics.jvm.GarbageCollectorMetricSet) Bean(org.springframework.context.annotation.Bean) LocalContainerEntityManagerFactoryBean(org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean)

Example 4 with MetricBatchSender

use of com.newrelic.telemetry.metrics.MetricBatchSender in project dropwizard-metrics-newrelic by newrelic.

the class NewRelicReporterFactory method build.

@Override
@NotNull
public ScheduledReporter build(final MetricRegistry registry) {
    MetricBatchSenderFactory factory = MetricBatchSenderFactory.fromHttpImplementation(OkHttpPoster::new);
    SenderConfiguration.SenderConfigurationBuilder config = factory.configureWith(apiKey);
    config = configureEndpoint(config);
    final Attributes attributes = new Attributes();
    if (commonAttributes != null) {
        for (final Map.Entry<String, Object> entry : commonAttributes.entrySet()) {
            if (entry.getValue() instanceof String)
                attributes.put(entry.getKey(), (String) entry.getValue());
            else if (entry.getValue() instanceof Number)
                attributes.put(entry.getKey(), (Number) entry.getValue());
            else if (entry.getValue() instanceof Boolean)
                attributes.put(entry.getKey(), (Boolean) entry.getValue());
        }
    }
    MetricBatchSender metricBatchSender = MetricBatchSender.create(config.build());
    return NewRelicReporterBuilder.forRegistry(registry, metricBatchSender).durationUnit(getDurationUnit()).rateUnit(getRateUnit()).filter(getFilter()).commonAttributes(attributes).disabledMetricAttributes(disabledMetricAttributes).build();
}
Also used : MetricBatchSenderFactory(com.newrelic.telemetry.MetricBatchSenderFactory) Attributes(com.newrelic.telemetry.Attributes) OkHttpPoster(com.newrelic.telemetry.OkHttpPoster) SenderConfiguration(com.newrelic.telemetry.SenderConfiguration) Map(java.util.Map) MetricBatchSender(com.newrelic.telemetry.metrics.MetricBatchSender) NotNull(javax.validation.constraints.NotNull)

Aggregations

Attributes (com.newrelic.telemetry.Attributes)4 MetricBatchSender (com.newrelic.telemetry.metrics.MetricBatchSender)4 MetricRegistry (com.codahale.metrics.MetricRegistry)3 OkHttpPoster (com.newrelic.telemetry.OkHttpPoster)3 SenderConfiguration (com.newrelic.telemetry.SenderConfiguration)3 GarbageCollectorMetricSet (com.codahale.metrics.jvm.GarbageCollectorMetricSet)2 MemoryUsageGaugeSet (com.codahale.metrics.jvm.MemoryUsageGaugeSet)2 NewRelicReporter (com.codahale.metrics.newrelic.NewRelicReporter)2 Clock (com.codahale.metrics.Clock)1 MetricAttribute (com.codahale.metrics.MetricAttribute)1 MetricFilter (com.codahale.metrics.MetricFilter)1 Slf4jReporter (com.codahale.metrics.Slf4jReporter)1 CounterTransformer (com.codahale.metrics.newrelic.transformer.CounterTransformer)1 GaugeTransformer (com.codahale.metrics.newrelic.transformer.GaugeTransformer)1 HistogramTransformer (com.codahale.metrics.newrelic.transformer.HistogramTransformer)1 MeterTransformer (com.codahale.metrics.newrelic.transformer.MeterTransformer)1 TimerTransformer (com.codahale.metrics.newrelic.transformer.TimerTransformer)1 MetricAttributesCustomizer (com.codahale.metrics.newrelic.transformer.customizer.MetricAttributesCustomizer)1 MetricNameCustomizer (com.codahale.metrics.newrelic.transformer.customizer.MetricNameCustomizer)1 TimeTracker (com.codahale.metrics.newrelic.util.TimeTracker)1