Add OpenTelemetry VCS attributes (#32904)

This commit is contained in:
Renaud Chaput 2024-11-18 09:41:09 +01:00 committed by GitHub
parent 1c6f53e70f
commit bcf5d30e01
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 167 additions and 152 deletions

View file

@ -92,6 +92,7 @@ jobs:
build-args: | build-args: |
MASTODON_VERSION_PRERELEASE=${{ inputs.version_prerelease }} MASTODON_VERSION_PRERELEASE=${{ inputs.version_prerelease }}
MASTODON_VERSION_METADATA=${{ inputs.version_metadata }} MASTODON_VERSION_METADATA=${{ inputs.version_metadata }}
SOURCE_COMMIT=${{ github.sha }}
platforms: ${{ inputs.platforms }} platforms: ${{ inputs.platforms }}
provenance: false provenance: false
builder: ${{ steps.buildx.outputs.name || steps.buildx-native.outputs.name }} builder: ${{ steps.buildx.outputs.name || steps.buildx-native.outputs.name }}

View file

@ -29,6 +29,8 @@ FROM docker.io/ruby:${RUBY_VERSION}-slim-${DEBIAN_VERSION} AS ruby
ARG MASTODON_VERSION_PRERELEASE="" ARG MASTODON_VERSION_PRERELEASE=""
# Append build metadata or fork information to version.rb [--build-arg MASTODON_VERSION_METADATA="pr-123456"] # Append build metadata or fork information to version.rb [--build-arg MASTODON_VERSION_METADATA="pr-123456"]
ARG MASTODON_VERSION_METADATA="" ARG MASTODON_VERSION_METADATA=""
# Will be available as Mastodon::Version.source_commit
ARG SOURCE_COMMIT=""
# Allow Ruby on Rails to serve static files # Allow Ruby on Rails to serve static files
# See: https://docs.joinmastodon.org/admin/config/#rails_serve_static_files # See: https://docs.joinmastodon.org/admin/config/#rails_serve_static_files
@ -45,30 +47,31 @@ ARG GID="991"
# Apply Mastodon build options based on options above # Apply Mastodon build options based on options above
ENV \ ENV \
# Apply Mastodon version information # Apply Mastodon version information
MASTODON_VERSION_PRERELEASE="${MASTODON_VERSION_PRERELEASE}" \ MASTODON_VERSION_PRERELEASE="${MASTODON_VERSION_PRERELEASE}" \
MASTODON_VERSION_METADATA="${MASTODON_VERSION_METADATA}" \ MASTODON_VERSION_METADATA="${MASTODON_VERSION_METADATA}" \
# Apply Mastodon static files and YJIT options SOURCE_COMMIT="${SOURCE_COMMIT}" \
# Apply Mastodon static files and YJIT options
RAILS_SERVE_STATIC_FILES=${RAILS_SERVE_STATIC_FILES} \ RAILS_SERVE_STATIC_FILES=${RAILS_SERVE_STATIC_FILES} \
RUBY_YJIT_ENABLE=${RUBY_YJIT_ENABLE} \ RUBY_YJIT_ENABLE=${RUBY_YJIT_ENABLE} \
# Apply timezone # Apply timezone
TZ=${TZ} TZ=${TZ}
ENV \ ENV \
# Configure the IP to bind Mastodon to when serving traffic # Configure the IP to bind Mastodon to when serving traffic
BIND="0.0.0.0" \ BIND="0.0.0.0" \
# Use production settings for Yarn, Node and related nodejs based tools # Use production settings for Yarn, Node and related nodejs based tools
NODE_ENV="production" \ NODE_ENV="production" \
# Use production settings for Ruby on Rails # Use production settings for Ruby on Rails
RAILS_ENV="production" \ RAILS_ENV="production" \
# Add Ruby and Mastodon installation to the PATH # Add Ruby and Mastodon installation to the PATH
DEBIAN_FRONTEND="noninteractive" \ DEBIAN_FRONTEND="noninteractive" \
PATH="${PATH}:/opt/ruby/bin:/opt/mastodon/bin" \ PATH="${PATH}:/opt/ruby/bin:/opt/mastodon/bin" \
# Optimize jemalloc 5.x performance # Optimize jemalloc 5.x performance
MALLOC_CONF="narenas:2,background_thread:true,thp:never,dirty_decay_ms:1000,muzzy_decay_ms:0" \ MALLOC_CONF="narenas:2,background_thread:true,thp:never,dirty_decay_ms:1000,muzzy_decay_ms:0" \
# Enable libvips, should not be changed # Enable libvips, should not be changed
MASTODON_USE_LIBVIPS=true \ MASTODON_USE_LIBVIPS=true \
# Sidekiq will touch tmp/sidekiq_process_has_started_and_will_begin_processing_jobs to indicate it is ready. This can be used for a readiness check in Kubernetes # Sidekiq will touch tmp/sidekiq_process_has_started_and_will_begin_processing_jobs to indicate it is ready. This can be used for a readiness check in Kubernetes
MASTODON_SIDEKIQ_READY_FILENAME=sidekiq_process_has_started_and_will_begin_processing_jobs MASTODON_SIDEKIQ_READY_FILENAME=sidekiq_process_has_started_and_will_begin_processing_jobs
# Set default shell used for running commands # Set default shell used for running commands
@ -79,14 +82,14 @@ ARG TARGETPLATFORM
RUN echo "Target platform is $TARGETPLATFORM" RUN echo "Target platform is $TARGETPLATFORM"
RUN \ RUN \
# Remove automatic apt cache Docker cleanup scripts # Remove automatic apt cache Docker cleanup scripts
rm -f /etc/apt/apt.conf.d/docker-clean; \ rm -f /etc/apt/apt.conf.d/docker-clean; \
# Sets timezone # Sets timezone
echo "${TZ}" > /etc/localtime; \ echo "${TZ}" > /etc/localtime; \
# Creates mastodon user/group and sets home directory # Creates mastodon user/group and sets home directory
groupadd -g "${GID}" mastodon; \ groupadd -g "${GID}" mastodon; \
useradd -l -u "${UID}" -g "${GID}" -m -d /opt/mastodon mastodon; \ useradd -l -u "${UID}" -g "${GID}" -m -d /opt/mastodon mastodon; \
# Creates /mastodon symlink to /opt/mastodon # Creates /mastodon symlink to /opt/mastodon
ln -s /opt/mastodon /mastodon; ln -s /opt/mastodon /mastodon;
# Set /opt/mastodon as working directory # Set /opt/mastodon as working directory
@ -94,13 +97,13 @@ WORKDIR /opt/mastodon
# hadolint ignore=DL3008,DL3005 # hadolint ignore=DL3008,DL3005
RUN \ RUN \
# Mount Apt cache and lib directories from Docker buildx caches # Mount Apt cache and lib directories from Docker buildx caches
--mount=type=cache,id=apt-cache-${TARGETPLATFORM},target=/var/cache/apt,sharing=locked \ --mount=type=cache,id=apt-cache-${TARGETPLATFORM},target=/var/cache/apt,sharing=locked \
--mount=type=cache,id=apt-lib-${TARGETPLATFORM},target=/var/lib/apt,sharing=locked \ --mount=type=cache,id=apt-lib-${TARGETPLATFORM},target=/var/lib/apt,sharing=locked \
# Apt update & upgrade to check for security updates to Debian image # Apt update & upgrade to check for security updates to Debian image
apt-get update; \ apt-get update; \
apt-get dist-upgrade -yq; \ apt-get dist-upgrade -yq; \
# Install jemalloc, curl and other necessary components # Install jemalloc, curl and other necessary components
apt-get install -y --no-install-recommends \ apt-get install -y --no-install-recommends \
curl \ curl \
file \ file \
@ -111,9 +114,9 @@ RUN \
tzdata \ tzdata \
wget \ wget \
; \ ; \
# Patch Ruby to use jemalloc # Patch Ruby to use jemalloc
patchelf --add-needed libjemalloc.so.2 /usr/local/bin/ruby; \ patchelf --add-needed libjemalloc.so.2 /usr/local/bin/ruby; \
# Discard patchelf after use # Discard patchelf after use
apt-get purge -y \ apt-get purge -y \
patchelf \ patchelf \
; ;
@ -132,10 +135,10 @@ ARG TARGETPLATFORM
# hadolint ignore=DL3008 # hadolint ignore=DL3008
RUN \ RUN \
# Mount Apt cache and lib directories from Docker buildx caches # Mount Apt cache and lib directories from Docker buildx caches
--mount=type=cache,id=apt-cache-${TARGETPLATFORM},target=/var/cache/apt,sharing=locked \ --mount=type=cache,id=apt-cache-${TARGETPLATFORM},target=/var/cache/apt,sharing=locked \
--mount=type=cache,id=apt-lib-${TARGETPLATFORM},target=/var/lib/apt,sharing=locked \ --mount=type=cache,id=apt-lib-${TARGETPLATFORM},target=/var/lib/apt,sharing=locked \
# Install build tools and bundler dependencies from APT # Install build tools and bundler dependencies from APT
apt-get install -y --no-install-recommends \ apt-get install -y --no-install-recommends \
autoconf \ autoconf \
automake \ automake \
@ -181,7 +184,7 @@ RUN \
; ;
RUN \ RUN \
# Configure Corepack # Configure Corepack
rm /usr/local/bin/yarn*; \ rm /usr/local/bin/yarn*; \
corepack enable; \ corepack enable; \
corepack prepare --activate; corepack prepare --activate;
@ -263,17 +266,17 @@ ARG TARGETPLATFORM
COPY Gemfile* /opt/mastodon/ COPY Gemfile* /opt/mastodon/
RUN \ RUN \
# Mount Ruby Gem caches # Mount Ruby Gem caches
--mount=type=cache,id=gem-cache-${TARGETPLATFORM},target=/usr/local/bundle/cache/,sharing=locked \ --mount=type=cache,id=gem-cache-${TARGETPLATFORM},target=/usr/local/bundle/cache/,sharing=locked \
# Configure bundle to prevent changes to Gemfile and Gemfile.lock # Configure bundle to prevent changes to Gemfile and Gemfile.lock
bundle config set --global frozen "true"; \ bundle config set --global frozen "true"; \
# Configure bundle to not cache downloaded Gems # Configure bundle to not cache downloaded Gems
bundle config set --global cache_all "false"; \ bundle config set --global cache_all "false"; \
# Configure bundle to only process production Gems # Configure bundle to only process production Gems
bundle config set --local without "development test"; \ bundle config set --local without "development test"; \
# Configure bundle to not warn about root user # Configure bundle to not warn about root user
bundle config set silence_root_warning "true"; \ bundle config set silence_root_warning "true"; \
# Download and install required Gems # Download and install required Gems
bundle install -j"$(nproc)"; bundle install -j"$(nproc)";
# Create temporary node specific build layer from build layer # Create temporary node specific build layer from build layer
@ -288,9 +291,9 @@ COPY .yarn /opt/mastodon/.yarn
# hadolint ignore=DL3008 # hadolint ignore=DL3008
RUN \ RUN \
--mount=type=cache,id=corepack-cache-${TARGETPLATFORM},target=/usr/local/share/.cache/corepack,sharing=locked \ --mount=type=cache,id=corepack-cache-${TARGETPLATFORM},target=/usr/local/share/.cache/corepack,sharing=locked \
--mount=type=cache,id=yarn-cache-${TARGETPLATFORM},target=/usr/local/share/.cache/yarn,sharing=locked \ --mount=type=cache,id=yarn-cache-${TARGETPLATFORM},target=/usr/local/share/.cache/yarn,sharing=locked \
# Install Node packages # Install Node packages
yarn workspaces focus --production @mastodon/mastodon; yarn workspaces focus --production @mastodon/mastodon;
# Create temporary assets build layer from build layer # Create temporary assets build layer from build layer
@ -311,10 +314,10 @@ ARG TARGETPLATFORM
RUN \ RUN \
ldconfig; \ ldconfig; \
# Use Ruby on Rails to create Mastodon assets # Use Ruby on Rails to create Mastodon assets
SECRET_KEY_BASE_DUMMY=1 \ SECRET_KEY_BASE_DUMMY=1 \
bundle exec rails assets:precompile; \ bundle exec rails assets:precompile; \
# Cleanup temporary files # Cleanup temporary files
rm -fr /opt/mastodon/tmp; rm -fr /opt/mastodon/tmp;
# Prep final Mastodon Ruby layer # Prep final Mastodon Ruby layer
@ -324,13 +327,13 @@ ARG TARGETPLATFORM
# hadolint ignore=DL3008 # hadolint ignore=DL3008
RUN \ RUN \
# Mount Apt cache and lib directories from Docker buildx caches # Mount Apt cache and lib directories from Docker buildx caches
--mount=type=cache,id=apt-cache-${TARGETPLATFORM},target=/var/cache/apt,sharing=locked \ --mount=type=cache,id=apt-cache-${TARGETPLATFORM},target=/var/cache/apt,sharing=locked \
--mount=type=cache,id=apt-lib-${TARGETPLATFORM},target=/var/lib/apt,sharing=locked \ --mount=type=cache,id=apt-lib-${TARGETPLATFORM},target=/var/lib/apt,sharing=locked \
# Mount Corepack and Yarn caches from Docker buildx caches # Mount Corepack and Yarn caches from Docker buildx caches
--mount=type=cache,id=corepack-cache-${TARGETPLATFORM},target=/usr/local/share/.cache/corepack,sharing=locked \ --mount=type=cache,id=corepack-cache-${TARGETPLATFORM},target=/usr/local/share/.cache/corepack,sharing=locked \
--mount=type=cache,id=yarn-cache-${TARGETPLATFORM},target=/usr/local/share/.cache/yarn,sharing=locked \ --mount=type=cache,id=yarn-cache-${TARGETPLATFORM},target=/usr/local/share/.cache/yarn,sharing=locked \
# Apt update install non-dev versions of necessary components # Apt update install non-dev versions of necessary components
apt-get install -y --no-install-recommends \ apt-get install -y --no-install-recommends \
libexpat1 \ libexpat1 \
libglib2.0-0 \ libglib2.0-0 \
@ -386,7 +389,7 @@ COPY --from=ffmpeg /usr/local/ffmpeg/lib /usr/local/lib
RUN \ RUN \
ldconfig; \ ldconfig; \
# Smoketest media processors # Smoketest media processors
vips -v; \ vips -v; \
ffmpeg -version; \ ffmpeg -version; \
ffprobe -version; ffprobe -version;
@ -396,10 +399,10 @@ RUN \
bundle exec bootsnap precompile --gemfile app/ lib/; bundle exec bootsnap precompile --gemfile app/ lib/;
RUN \ RUN \
# Pre-create and chown system volume to Mastodon user # Pre-create and chown system volume to Mastodon user
mkdir -p /opt/mastodon/public/system; \ mkdir -p /opt/mastodon/public/system; \
chown mastodon:mastodon /opt/mastodon/public/system; \ chown mastodon:mastodon /opt/mastodon/public/system; \
# Set Mastodon user as owner of tmp folder # Set Mastodon user as owner of tmp folder
chown -R mastodon:mastodon /opt/mastodon/tmp; chown -R mastodon:mastodon /opt/mastodon/tmp;
# Set the running user for resulting container # Set the running user for resulting container

View file

@ -65,6 +65,13 @@ if ENV.keys.any? { |name| name.match?(/OTEL_.*_ENDPOINT/) }
"#{prefix}#{separator}#{$PROGRAM_NAME.split('/').last}" "#{prefix}#{separator}#{$PROGRAM_NAME.split('/').last}"
end end
c.service_version = Mastodon::Version.to_s c.service_version = Mastodon::Version.to_s
if Mastodon::Version.source_commit.present?
c.resource = OpenTelemetry::SDK::Resources::Resource.create(
'vcs.repository.ref.revision' => Mastodon::Version.source_commit,
'vcs.repository.url.full' => Mastodon::Version.source_base_url
)
end
end end
end end

View file

@ -70,6 +70,10 @@ module Mastodon
end end
end end
def source_commit
ENV.fetch('SOURCE_COMMIT', nil)
end
def user_agent def user_agent
@user_agent ||= "Mastodon/#{Version} (#{HTTP::Request::USER_AGENT}; +http#{Rails.configuration.x.use_https ? 's' : ''}://#{Rails.configuration.x.web_domain}/)" @user_agent ||= "Mastodon/#{Version} (#{HTTP::Request::USER_AGENT}; +http#{Rails.configuration.x.use_https ? 's' : ''}://#{Rails.configuration.x.web_domain}/)"
end end