-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Packaging initial infrastructure (#133)
Not complete but enough to review as part 1 😄 * Introduces our own version of `OpenTelemetry.AutoInstrumentation` named `Elastic.OpenTelemetry.AutoInstrumentation`. Through a bit of msbuild magic we redistribute `instrument.{sh|cmd}` as `_instrument.{sh|cmd}` and introduce our own `instrument.{sh|cmd}` that sets the appropiate plugin environment variable before calling the opentelemetry instrument script. This allows the invocation to be exactly the same no matter if our distribution or the vanilla opentelemetry package is installed. A user can move from or away from Elastic without having to touch orchestration work. Simply installing the package is enough to move to or away from us. * This PR includes the start of a `./build.sh redistribute` build command that redistributes the opentelemetry autoinstrumentation zip files that include our AutoInstrumentation plugin. A follow up of this PR will focus on creating Elastic versions of the global auto instrumentation installation scripts and ensuring these get staged as part of the release.
- Loading branch information
Showing
13 changed files
with
256 additions
and
0 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
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,111 @@ | ||
// Licensed to Elasticsearch B.V under one or more agreements. | ||
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information | ||
|
||
|
||
module Packaging | ||
|
||
open System | ||
open System.IO | ||
open System.IO.Compression | ||
open System.Net.Http | ||
open Argu | ||
open BuildInformation | ||
open CommandLine | ||
open Octokit | ||
|
||
let private otelAutoVersion = BuildConfiguration.OpenTelemetryAutoInstrumentationVersion; | ||
|
||
let private downloadFolder = Path.Combine(".artifacts", "otel-distribution", otelAutoVersion.AsString) |> Directory.CreateDirectory | ||
let private distroFolder = Path.Combine(".artifacts", "elastic-distribution", otelAutoVersion.AsString) |> Directory.CreateDirectory | ||
|
||
let private fileInfo (directory: DirectoryInfo) file = Path.Combine(directory.FullName, file) |> FileInfo | ||
let private downloadFile (asset: ReleaseAsset) = fileInfo downloadFolder asset.Name | ||
let private stageFile (asset: ReleaseAsset) = fileInfo downloadFolder (asset.Name.Replace("opentelemetry", "stage")) | ||
let private distroFile (asset: ReleaseAsset) = fileInfo distroFolder (asset.Name.Replace("opentelemetry", "elastic")) | ||
|
||
let pluginFiles tfm = | ||
["dll"; "pdb"; "xml"] | ||
|> List.map(fun e -> $"Elastic.OpenTelemetry.%s{e}") | ||
|> List.map(fun f -> Path.Combine(".artifacts", "bin", "Elastic.OpenTelemetry", $"release_%s{tfm}", "", f)) | ||
|> List.map(fun f -> FileInfo(f)) | ||
|
||
|
||
/// downloads the artifacts if they don't already exist locally | ||
let downloadArtifacts (_:ParseResults<Build>) = | ||
let client = GitHubClient(ProductHeaderValue("Elastic.OpenTelemetry")) | ||
let token = Environment.GetEnvironmentVariable("GITHUB_TOKEN") | ||
if not(String.IsNullOrWhiteSpace(token)) then | ||
Console.WriteLine($"using GITHUB_TOKEN"); | ||
let tokenAuth = Credentials(token); | ||
client.Credentials <- tokenAuth | ||
|
||
let assets = | ||
async { | ||
let! release = client.Repository.Release.Get("open-telemetry", "opentelemetry-dotnet-instrumentation", $"v{otelAutoVersion.AsString}") |> Async.AwaitTask; | ||
Console.WriteLine($"Release %s{release.Name} has %i{release.Assets.Count} assets"); | ||
return release.Assets | ||
|> Seq.map (fun asset -> (asset, downloadFile asset)) | ||
|> Seq.toList | ||
} |> Async.RunSynchronously | ||
|
||
async { | ||
use httpClient = new HttpClient() | ||
assets | ||
|> Seq.filter (fun (_, f) -> not f.Exists) | ||
|> Seq.iter (fun (asset, f) -> | ||
async { | ||
Console.WriteLine($"Retrieving {asset.Name}"); | ||
let! fileData = httpClient.GetByteArrayAsync(asset.BrowserDownloadUrl) |> Async.AwaitTask | ||
Console.WriteLine($"Saveing %i{fileData.Length} bytes to {f.FullName}") | ||
File.WriteAllBytes(f.FullName, fileData) | ||
f.Refresh() | ||
} |> Async.RunSynchronously | ||
) | ||
} |> Async.RunSynchronously | ||
assets | ||
|
||
let injectPluginFiles (asset: ReleaseAsset) (stagedZip: FileInfo) tfm target = | ||
use zipArchive = ZipFile.Open(stagedZip.FullName, ZipArchiveMode.Update) | ||
pluginFiles tfm |> List.iter(fun f -> | ||
printfn $"Staging zip: %s{asset.Name}, Adding: %s{f.Name} (%s{tfm}_ to %s{target}" | ||
zipArchive.CreateEntryFromFile(f.FullName, Path.Combine(target, f.Name)) |> ignore | ||
) | ||
|
||
/// moves artifacts from open-distribution to elastic-distribution and renames them to `staged-dotnet-instrumentation*`. | ||
/// staged meaning we haven't injected our opentelemetry dll into the zip yet, | ||
let stageArtifacts (assets:List<ReleaseAsset * FileInfo>) = | ||
let stagedZips = | ||
assets | ||
|> List.filter(fun (a, _) -> a.Name.EndsWith(".zip")) | ||
|> List.map(fun (z, f) -> | ||
let stage = stageFile z | ||
z, f.CopyTo(stage.FullName, true) | ||
) | ||
|
||
stagedZips |> List.iter (fun (asset, path) -> | ||
|
||
injectPluginFiles asset path "netstandard2.1" "net" | ||
if asset.Name.EndsWith("-windows.zip") then | ||
injectPluginFiles asset path "net462" "netfx" | ||
|
||
let distro = distroFile asset | ||
path.MoveTo(distro.FullName, true) | ||
distro.Refresh() | ||
|
||
printfn $"Created: %s{distro.FullName}" | ||
) | ||
stagedZips | ||
|
||
|
||
|
||
let redistribute (arguments:ParseResults<Build>) = | ||
let assets = downloadArtifacts arguments | ||
let staged = stageArtifacts assets | ||
|
||
printfn "" | ||
assets |> List.iter (fun (asset, path) -> | ||
printfn "Asset: %s" asset.Name | ||
) | ||
|
||
|
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
52 changes: 52 additions & 0 deletions
52
...lastic.OpenTelemetry.AutoInstrumentation/Elastic.OpenTelemetry.AutoInstrumentation.csproj
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,52 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net6.0;net8.0;net462</TargetFrameworks> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<IsPackable>True</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="OpenTelemetry.AutoInstrumentation" Version="1.7.0" GeneratePathProperty="true" PrivateAssets="contentfiles" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Content Remove="README.md" /> | ||
|
||
<!-- ensure we remove the linked instrument.cmd from base OpenTelemetry.AutoInstrumentation | ||
and link it as _instrument.cmd since we manually copy it over in the prebuild event --> | ||
<Content Update="instrument.cmd" CopyToPublishDirectory="Never" CopyToOutputDirectory="Never" /> | ||
<Content Remove="instrument.cmd" /> | ||
<Content Include="_instrument.cmd" CopyToOutputDirectory="Always" CopyToPublishDirectory="Always" | ||
Pack="True" PackagePath="contentFiles/any/any/_instrument.cmd"/> | ||
<Content Include="instrument.cmd" CopyToOutputDirectory="Always" CopyToPublishDirectory="Always" | ||
Pack="True" PackagePath="contentFiles/any/any/instrument.cmd"/> | ||
|
||
<!-- ensure we remove the linked instrument.sh from base OpenTelemetry.AutoInstrumentation | ||
and link it as _instrument.sh since we manually copy it over in the prebuild event --> | ||
|
||
<Content Update="instrument.sh" CopyToPublishDirectory="Never" CopyToOutputDirectory="Never" /> | ||
<Content Remove="instrument.sh" /> | ||
<Content Include="_instrument.sh" CopyToOutputDirectory="Always" CopyToPublishDirectory="Always" | ||
Pack="True" PackagePath="contentFiles/any/any/_instrument.sh"/> | ||
<Content Include="instrument.sh" CopyToOutputDirectory="Always" CopyToPublishDirectory="Always" | ||
Pack="True" PackagePath="contentFiles/any/any/instrument.sh"/> | ||
|
||
<Content Include="instrument.props" CopyToOutputDirectory="Always" CopyToPublishDirectory="Always" | ||
Pack="True" PackagePath="build/elastic.opentelemetry.autoinstrumentation.props"/> | ||
|
||
</ItemGroup> | ||
|
||
<Target Name="PreBuild" BeforeTargets="PreBuildEvent"> | ||
<!-- Copies the content files manually as physical files in the source repository --> | ||
<!-- we manually repackage these as contentfiles (albeit renamed) --> | ||
<Copy SourceFiles="$(PkgOpenTelemetry_AutoInstrumentation)/contentFiles/any/any/instrument.cmd" DestinationFiles="$(MSBuildThisFileDirectory)/_instrument.cmd"/> | ||
<Copy SourceFiles="$(PkgOpenTelemetry_AutoInstrumentation)/contentFiles/any/any/instrument.sh" DestinationFiles="$(MSBuildThisFileDirectory)/_instrument.sh"/> | ||
</Target> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Elastic.OpenTelemetry\Elastic.OpenTelemetry.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
31 changes: 31 additions & 0 deletions
31
src/Elastic.OpenTelemetry.AutoInstrumentation/_instrument.cmd
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,31 @@ | ||
@echo off | ||
setlocal | ||
|
||
:: This script is expected to be used in a build that specified a RuntimeIdentifier (RID) | ||
set BASE_PATH=%~dp0 | ||
|
||
:: Settings for .NET Framework | ||
set COR_ENABLE_PROFILING=1 | ||
set COR_PROFILER={918728DD-259F-4A6A-AC2B-B85E1B658318} | ||
set COR_PROFILER_PATH=%BASE_PATH%OpenTelemetry.AutoInstrumentation.Native.dll | ||
|
||
:: On .NET Framework automatic assembly redirection MUST be disabled. This setting | ||
:: is ignored on .NET. This is necessary because the NuGet package doesn't bring | ||
:: the pre-defined versions of the transitive dependencies used in the automatic | ||
:: redirection. Instead the transitive dependencies versions are determined by | ||
:: the NuGet version resolution algorithm when building the application. | ||
set OTEL_DOTNET_AUTO_NETFX_REDIRECT_ENABLED=false | ||
|
||
:: Settings for .NET | ||
set ASPNETCORE_HOSTINGSTARTUPASSEMBLIES=OpenTelemetry.AutoInstrumentation.AspNetCoreBootstrapper | ||
set CORECLR_ENABLE_PROFILING=1 | ||
set CORECLR_PROFILER={918728DD-259F-4A6A-AC2B-B85E1B658318} | ||
set CORECLR_PROFILER_PATH=%BASE_PATH%OpenTelemetry.AutoInstrumentation.Native.dll | ||
set DOTNET_STARTUP_HOOKS=%BASE_PATH%OpenTelemetry.AutoInstrumentation.StartupHook.dll | ||
|
||
:: Settings for OpenTelemetry | ||
set OTEL_DOTNET_AUTO_HOME=%BASE_PATH% | ||
set OTEL_DOTNET_AUTO_RULE_ENGINE_ENABLED=false | ||
|
||
@echo on | ||
%* |
17 changes: 17 additions & 0 deletions
17
src/Elastic.OpenTelemetry.AutoInstrumentation/_instrument.sh
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,17 @@ | ||
#!/bin/sh | ||
|
||
BASE_PATH="$(cd "$(dirname "$0")" && pwd)" | ||
|
||
# Settings for .NET | ||
export ASPNETCORE_HOSTINGSTARTUPASSEMBLIES=OpenTelemetry.AutoInstrumentation.AspNetCoreBootstrapper | ||
export CORECLR_ENABLE_PROFILING=1 | ||
export CORECLR_PROFILER="{918728DD-259F-4A6A-AC2B-B85E1B658318}" | ||
CORECLR_PROFILER_PATH="$(ls ${BASE_PATH}/OpenTelemetry.AutoInstrumentation.Native.*)" | ||
export CORECLR_PROFILER_PATH | ||
export DOTNET_STARTUP_HOOKS=${BASE_PATH}/OpenTelemetry.AutoInstrumentation.StartupHook.dll | ||
|
||
# Settings for OpenTelemetry | ||
export OTEL_DOTNET_AUTO_HOME=${BASE_PATH} | ||
export OTEL_DOTNET_AUTO_RULE_ENGINE_ENABLED=false | ||
|
||
exec "$@" |
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,9 @@ | ||
@echo off | ||
setlocal | ||
|
||
:: This script is expected to be used in a build that specified a RuntimeIdentifier (RID) | ||
set BASE_PATH=%~dp0 | ||
|
||
set OTEL_DOTNET_AUTO_PLUGINS=Elastic.OpenTelemetry.AutoInstrumentationPlugin, Elastic.OpenTelemetry | ||
|
||
call %BASE_PATH%_instrument.cmd %* |
12 changes: 12 additions & 0 deletions
12
src/Elastic.OpenTelemetry.AutoInstrumentation/instrument.props
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,12 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<!-- Hide the shipped content files VS project tree. | ||
Can be removed once https://github.com/NuGet/Home/issues/4856 is resolved --> | ||
<ItemGroup> | ||
<Content Update="@(Content)"> | ||
<Visible Condition="'%(NuGetItemType)' == 'Content' and '%(NuGetPackageId)' == 'elastic.opentelemetry.autoinstrumentation'">False</Visible> | ||
<CopyToOutputDirectory Condition="'%(NuGetItemType)' == 'Content' and '%(NuGetPackageId)' == 'elastic.opentelemetry.autoinstrumentation'">Always</CopyToOutputDirectory> | ||
<CopyToPublishDirectory Condition="'%(NuGetItemType)' == 'Content' and '%(NuGetPackageId)' == 'elastic.opentelemetry.autoinstrumentation'">Always</CopyToPublishDirectory> | ||
</Content> | ||
</ItemGroup> | ||
</Project> |
10 changes: 10 additions & 0 deletions
10
src/Elastic.OpenTelemetry.AutoInstrumentation/instrument.sh
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,10 @@ | ||
#!/bin/sh | ||
|
||
# This script is expected to be used in a build that specified a RuntimeIdentifier (RID) | ||
BASE_PATH="$(cd "$(dirname "$0")" && pwd)" | ||
|
||
export OTEL_DOTNET_AUTO_PLUGINS="Elastic.OpenTelemetry.AutoInstrumentationPlugin, Elastic.OpenTelemetry" | ||
|
||
. $BASE_PATH/_instrument.sh | ||
|
||
exec "$@" |