-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Download, configure and launch collector (#4)
- Loading branch information
Showing
11 changed files
with
274 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
module ScoutApm | ||
module Logging | ||
module Collector | ||
# Creates the configuration to be used when launching the collector. | ||
class Configuration | ||
attr_reader :context | ||
|
||
def initialize(context) | ||
@context = context | ||
end | ||
|
||
def setup! | ||
create_config_file | ||
end | ||
|
||
def create_config_file | ||
File.write(config_file, config_contents) | ||
end | ||
|
||
private | ||
|
||
def config_file | ||
context.config.value('collector_config_file') | ||
end | ||
|
||
def config_contents | ||
<<~CONFIG | ||
receivers: | ||
filelog: | ||
include: [#{context.config.value('monitored_logs').join(',')}] | ||
processors: | ||
batch: | ||
exporters: | ||
otlp: | ||
endpoint: #{context.config.value('logs_reporting_endpoint')} | ||
headers: | ||
x-telemetryhub-key: #{context.config.value('logging_ingest_key')} | ||
service: | ||
pipelines: | ||
logs: | ||
receivers: | ||
- filelog | ||
processors: | ||
- batch | ||
exporters: | ||
- otlp | ||
CONFIG | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# frozen_string_literal: true | ||
|
||
module ScoutApm | ||
module Logging | ||
module Collector | ||
# Downloads the collector-contrib binary from the OpenTelemetry project. | ||
class Downloader | ||
attr_reader :context | ||
|
||
def initialize(context) | ||
@context = context | ||
end | ||
|
||
def run! | ||
download_collector | ||
extract_collector | ||
end | ||
|
||
def download_collector(url = nil) # rubocop:disable Metrics/AbcSize | ||
# TODO: Check if we have already downloaded the collector. | ||
url_to_download = url || collector_url | ||
uri = URI(url_to_download) | ||
|
||
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| | ||
request = Net::HTTP::Get.new(uri) | ||
http.request(request) do |response| | ||
return download_collector(response['location']) if response.code == '302' | ||
|
||
File.open(destination, 'wb') do |file| | ||
response.read_body do |chunk| | ||
file.write(chunk) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
def extract_collector | ||
Utils.ensure_directory_exists(destination) | ||
`tar -xzf #{destination} -C #{context.config.value('collector_download_dir')}` | ||
end | ||
|
||
private | ||
|
||
def collector_url | ||
collector_version = context.config.value('collector_version') | ||
|
||
# https://opentelemetry.io/docs/collector/installation/#manual-linux-installation | ||
"https://github.com/open-telemetry/opentelemetry-collector-releases/releases/download/v#{collector_version}/otelcol-contrib_#{collector_version}_#{host_os}_#{architecture}.tar.gz" | ||
end | ||
|
||
# TODO: Add support for other platforms | ||
def architecture | ||
if /arm/ =~ RbConfig::CONFIG['arch'] | ||
'arm64' | ||
else | ||
'amd64' | ||
end | ||
end | ||
|
||
def host_os | ||
if /darwin|mac os/ =~ RbConfig::CONFIG['host_os'] | ||
'darwin' | ||
else | ||
'linux' | ||
end | ||
end | ||
|
||
def destination | ||
"#{context.config.value('collector_download_dir')}/otelcol.tar.gz" | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative './configuration' | ||
require_relative './downloader' | ||
|
||
module ScoutApm | ||
module Logging | ||
module Collector | ||
# Manager class for the downloading, configuring, and starting of the collector. | ||
class Manager | ||
attr_reader :context | ||
|
||
def initialize(context) | ||
@context = context | ||
end | ||
|
||
def setup! | ||
Configuration.new(context).setup! | ||
Downloader.new(context).run! | ||
|
||
start_collector | ||
end | ||
|
||
def start_collector | ||
Process.spawn("#{extracted_collector_path}/otelcol-contrib --config #{config_file}") | ||
end | ||
|
||
private | ||
|
||
def extracted_collector_path | ||
context.config.value('collector_download_dir') | ||
end | ||
|
||
def config_file | ||
context.config.value('collector_config_file') | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'fileutils' | ||
|
||
module ScoutApm | ||
module Logging | ||
# Miscellaneous utilities for the logging module. | ||
module Utils | ||
def self.ensure_directory_exists(file_path) | ||
directory = File.dirname(file_path) | ||
FileUtils.mkdir_p(directory) unless File.directory?(directory) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.