Repository URL to install this package:
|
Version:
0.7.0 ▾
|
// (c) Copyright 2023 Supertenant Ltd. - all rights reserved.
// See LICENSE file in project root for license terms.
"use strict";const{secrets,tracing,util:{ensureNestedObjectExists}}=require("@supertenant/core"),tracingConstants=tracing["constants"];let logger;logger=require("../logger").getLogger("announceCycle/unannounced",newLogger=>{logger=newLogger});const agentConnection=require("../agentConnection"),agentOpts=require("../agent/opts"),pidStore=require("../pidStore"),initialRetryDelay=1e4,backoffFactor=1.5,maxRetryDelay=6e4;function tryToAnnounce(ctx,retryDelay=initialRetryDelay){let nextRetryDelay;nextRetryDelay=retryDelay*backoffFactor>=maxRetryDelay?maxRetryDelay:retryDelay*backoffFactor,agentConnection.announceNodeCollector((err,rawResponse)=>{if(err)logger.info("Announce attempt failed: %s. Will retry in %s ms",err.message,retryDelay),setTimeout(tryToAnnounce,retryDelay,ctx,nextRetryDelay).unref();else{let agentResponse;try{agentResponse=JSON.parse(rawResponse)}catch(e){return logger.warn("Failed to JSON.parse agent response. Response was %s. Will retry in %s ms",rawResponse,retryDelay,e),void setTimeout(tryToAnnounce,retryDelay,ctx,nextRetryDelay).unref()}err=agentResponse.pid;logger.info("Overwriting pid for reporting purposes to: %s",err),pidStore.pid=err,agentOpts.agentUuid=agentResponse.agentUuid,applyAgentConfiguration(agentResponse),ctx.transitionTo("announced")}})}function applyAgentConfiguration(agentResponse){applySecretsConfiguration(agentResponse),applyExtraHttpHeaderConfiguration(agentResponse),applyKafkaTracingConfiguration(agentResponse),applySpanBatchingConfiguration(agentResponse)}function applySecretsConfiguration(agentResponse){agentResponse.secrets&&("string"!=typeof agentResponse.secrets.matcher?logger.warn("Received invalid secrets configuration from agent, attribute matcher is not a string: $s",agentResponse.secrets.matcher):Object.keys(secrets.matchers).indexOf(agentResponse.secrets.matcher)<0?logger.warn("Received invalid secrets configuration from agent, matcher is not supported: $s",agentResponse.secrets.matcher):Array.isArray(agentResponse.secrets.list)?secrets.setMatcher(agentResponse.secrets.matcher,agentResponse.secrets.list):logger.warn("Received invalid secrets configuration from agent, attribute list is not an array: $s",agentResponse.secrets.list))}function applyExtraHttpHeaderConfiguration(agentResponse){agentResponse.tracing?actuallyApplyExtraHttpHeaderConfiguration(agentResponse.tracing["extra-http-headers"]):actuallyApplyExtraHttpHeaderConfiguration(agentResponse.extraHeaders)}function actuallyApplyExtraHttpHeaderConfiguration(extraHeaders){Array.isArray(extraHeaders)&&(ensureNestedObjectExists(agentOpts.config,["tracing","http"]),agentOpts.config.tracing.http.extraHttpHeadersToCapture=extraHeaders.map(s=>s.toLowerCase()))}function applyKafkaTracingConfiguration(agentResponse){agentResponse.tracing&&agentResponse.tracing.kafka&&(agentResponse={traceCorrelation:null!=(agentResponse=agentResponse.tracing.kafka)["trace-correlation"]?agentResponse["trace-correlation"]:tracingConstants.kafkaTraceCorrelationDefault,headerFormat:null!=agentResponse["header-format"]?agentResponse["header-format"]:tracingConstants.kafkaHeaderFormatDefault},ensureNestedObjectExists(agentOpts.config,["tracing","kafka"]),agentOpts.config.tracing.kafka=agentResponse)}function applySpanBatchingConfiguration(agentResponse){agentResponse.tracing?!0===agentResponse.tracing["span-batching-enabled"]&&(ensureNestedObjectExists(agentOpts.config,["tracing"]),agentOpts.config.tracing.spanBatchingEnabled=!0):!0!==agentResponse.spanBatchingEnabled&&"true"!==agentResponse.spanBatchingEnabled||(logger.info("Enabling span batching via agent configuration."),ensureNestedObjectExists(agentOpts.config,["tracing"]),agentOpts.config.tracing.spanBatchingEnabled=!0)}module.exports={enter:function(ctx){tryToAnnounce(ctx)},leave:function(){}};