Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker instructions #4797

Merged
merged 5 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@

### Step 1: Install OpenTelemetry Dependencies
Dependencies related to OpenTelemetry exporter and SDK have to be installed first.

Run the below commands after navigating to the application source folder:
```bash
dotnet add package OpenTelemetry
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol
dotnet add package OpenTelemetry.Extensions.Hosting
dotnet add package OpenTelemetry.Instrumentation.Runtime
dotnet add package OpenTelemetry.Instrumentation.AspNetCore
dotnet add package OpenTelemetry.AutoInstrumentation
```

 

### Step 2: Adding OpenTelemetry as a service and configuring exporter options

In your `Program.cs` file, add OpenTelemetry as a service. Here, we are configuring these variables:

`serviceName` - It is the name of your service.

`otlpOptions.Endpoint` - It is the endpoint for your OTel Collector agent.

 

Here’s a sample `Program.cs` file with the configured variables:

```bash
using System.Diagnostics;
using OpenTelemetry.Exporter;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

var builder = WebApplication.CreateBuilder(args);

// Configure OpenTelemetry with tracing and auto-start.
builder.Services.AddOpenTelemetry()
.ConfigureResource(resource =>
resource.AddService(serviceName: "{{MYAPP}}"))
.WithTracing(tracing => tracing
.AddAspNetCoreInstrumentation()
.AddOtlpExporter(otlpOptions =>
{
//sigNoz Cloud Endpoint
otlpOptions.Endpoint = new Uri("https://ingest.{{REGION}}.signoz.cloud:443");

otlpOptions.Protocol = OtlpExportProtocol.Grpc;

//SigNoz Cloud account Ingestion key
string headerKey = "signoz-access-token";
string headerValue = "{{SIGNOZ_INGESTION_KEY}}";

string formattedHeader = $"{headerKey}={headerValue}";
otlpOptions.Headers = formattedHeader;
}));

var app = builder.Build();

//The index route ("/") is set up to write out the OpenTelemetry trace information on the response:
app.MapGet("/", () => $"Hello World! OpenTelemetry Trace: {Activity.Current?.Id}");

app.Run();
```

 


The OpenTelemetry.Exporter.Options get or set the target to which the exporter is going to send traces. Here, we’re configuring it to send traces to the OTel Collector agent. The target must be a valid Uri with the scheme (http or https) and host and may contain a port and a path.

This is done by configuring an OpenTelemetry [TracerProvider](https:/open-telemetry/opentelemetry-dotnet/tree/main/docs/trace/customizing-the-sdk#readme) using extension methods and setting it to auto-start when the host is started.


### Step 3: Dockerize your application

Since the environment variables like SIGNOZ_INGESTION_KEY, Ingestion Endpoint and Service name are set in the `program.cs` file, you don't need to add any additional steps in your Dockerfile.

An **example** of a Dockerfile could look like this:

```bash

# Use the Microsoft official .NET SDK image to build the application
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build-env
WORKDIR /app

# Copy the CSPROJ file and restore any dependencies (via NUGET)
COPY *.csproj ./
RUN dotnet restore

# Copy the rest of the project files and build the application
COPY . ./
RUN dotnet publish -c Release -o out

# Generate the runtime image
FROM mcr.microsoft.com/dotnet/aspnet:8.0
WORKDIR /app
COPY --from=build-env /app/out .

# Expose port 5145 for the application
EXPOSE 5145

# Set the ASPNETCORE_URLS environment variable to listen on port 5145
ENV ASPNETCORE_URLS=http://+:5145

ENTRYPOINT ["dotnet", "YOUR-APPLICATION.dll"]
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Once you update your Dockerfile, you can build and run it using the commands below.

 

### Step 1: Build your dockerfile

Build your docker image

```bash
docker build -t <your-image-name> .
```

- `<your-image-name>` is the name of your Docker Image

&nbsp;

### Step 2: Run your docker image

```bash
docker run <your-image-name>
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## Setup OpenTelemetry Binary as an agent

&nbsp;

As a first step, you should install the OTel collector Binary according to the instructions provided on [this link](https://signoz.io/docs/tutorial/opentelemetry-binary-usage-in-virtual-machine/).

&nbsp;

Once you are done setting up the OTel collector binary, you can follow the next steps.

&nbsp;

Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
After setting up the Otel collector agent, follow the steps below to instrument your .NET Application

&nbsp;
&nbsp;

### Step 1: Install OpenTelemetry Dependencies
Install the following dependencies in your application.

```bash
dotnet add package OpenTelemetry
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol
dotnet add package OpenTelemetry.Extensions.Hosting
dotnet add package OpenTelemetry.Instrumentation.Runtime
dotnet add package OpenTelemetry.Instrumentation.AspNetCore
dotnet add package OpenTelemetry.AutoInstrumentation
```

&nbsp;

### Step 2: Adding OpenTelemetry as a service and configuring exporter options

In your `Program.cs` file, add OpenTelemetry as a service. Here, we are configuring these variables:

`serviceName` - It is the name of your service.

`otlpOptions.Endpoint` - It is the endpoint for your OTel Collector agent.

&nbsp;

Here’s a sample `Program.cs` file with the configured variables:

```bash
using System.Diagnostics;
using OpenTelemetry.Exporter;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

var builder = WebApplication.CreateBuilder(args);

// Configure OpenTelemetry with tracing and auto-start.
builder.Services.AddOpenTelemetry()
.ConfigureResource(resource =>
resource.AddService(serviceName: "{{MYAPP}}"))
.WithTracing(tracing => tracing
.AddAspNetCoreInstrumentation()
.AddOtlpExporter(otlpOptions =>
{
otlpOptions.Endpoint = new Uri("http://localhost:4317");

otlpOptions.Protocol = OtlpExportProtocol.Grpc;
}));

var app = builder.Build();

//The index route ("/") is set up to write out the OpenTelemetry trace information on the response:
app.MapGet("/", () => $"Hello World! OpenTelemetry Trace: {Activity.Current?.Id}");

app.Run();
```
&nbsp;

The OpenTelemetry.Exporter.Options get or set the target to which the exporter is going to send traces. Here, we’re configuring it to send traces to the OTel Collector agent. The target must be a valid Uri with the scheme (http or https) and host and may contain a port and a path.

This is done by configuring an OpenTelemetry [TracerProvider](https:/open-telemetry/opentelemetry-dotnet/tree/main/docs/trace/customizing-the-sdk#readme) using extension methods and setting it to auto-start when the host is started.


&nbsp;

### Step 3: Dockerize your application

Since the crucial environment variables like SIGNOZ_INGESTION_KEY, Ingestion Endpoint and Service name are set in the `program.cs` file, you don't need to add any additional steps in your Dockerfile.

An **example** of a Dockerfile could look like this:

```bash

# Use the Microsoft official .NET SDK image to build the application
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build-env
WORKDIR /app

# Copy the CSPROJ file and restore any dependencies (via NUGET)
COPY *.csproj ./
RUN dotnet restore

# Copy the rest of the project files and build the application
COPY . ./
RUN dotnet publish -c Release -o out

# Generate the runtime image
FROM mcr.microsoft.com/dotnet/aspnet:8.0
WORKDIR /app
COPY --from=build-env /app/out .

# Expose port 5145 for the application
EXPOSE 5145

# Set the ASPNETCORE_URLS environment variable to listen on port 5145
ENV ASPNETCORE_URLS=http://+:5145

ENTRYPOINT ["dotnet", "YOUR-APPLICATION.dll"]
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Once you update your Dockerfile, you can build and run it using the commands below.

&nbsp;

### Step 1: Build your dockerfile

Build your docker image

```bash
docker build -t <your-image-name> .
```

- `<your-image-name>` is the name of your Docker Image

&nbsp;

### Step 2: Run your docker image

```bash
docker run <your-image-name>
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
&nbsp;

Follow the steps below to instrument your Elixir (Phoenix + Ecto) Application

### Step 1: Add dependencies
Install dependencies related to OpenTelemetry by adding them to `mix.exs` file

```bash
{:opentelemetry_exporter, "~> 1.6"},
{:opentelemetry_api, "~> 1.2"},
{:opentelemetry, "~> 1.3"},
{:opentelemetry_semantic_conventions, "~> 0.2"},
{:opentelemetry_cowboy, "~> 0.2.1"},
{:opentelemetry_phoenix, "~> 1.1"},
{:opentelemetry_ecto, "~> 1.1"}
```
&nbsp;

In your application start, usually the `application.ex` file, setup the telemetry handlers

```bash
:opentelemetry_cowboy.setup()
OpentelemetryPhoenix.setup(adapter: :cowboy2)
OpentelemetryEcto.setup([:{{MYAPP}}, :repo])
```
&nbsp;

As an example, this is how you can setup the handlers in your application.ex file for an application called demo :

```bash
# application.ex
@impl true
def start(_type, _args) do
:opentelemetry_cowboy.setup()
OpentelemetryPhoenix.setup(adapter: :cowboy2)
OpentelemetryEcto.setup([:demo, :repo])

end
```

&nbsp;

### Step 2: Configure Application
You need to configure your application to send telemetry data by adding the following config to your `runtime.exs` file:

```bash
config :opentelemetry, :resource, service: %{name: "{{MYAPP}}"}

config :opentelemetry, :processors,
otel_batch_processor: %{
exporter: {
:opentelemetry_exporter,
%{
endpoints: ["https://ingest.{{REGION}}.signoz.cloud:443"],
headers: [
{"signoz-access-token", {{SIGNOZ_ACCESS_TOKEN}} }
]
}
}
}
```

&nbsp;

### Step 3: Dockerize your application

Since the environment variables like SIGNOZ_INGESTION_KEY, Ingestion Endpoint and Service name are set in the above steps, you don't need to add any additional steps in your Dockerfile.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Once you update your Dockerfile, you can build and run it using the commands below.

&nbsp;

### Step 1: Build your dockerfile

Build your docker image

```bash
docker build -t <your-image-name> .
```

- `<your-image-name>` is the name of your Docker Image

&nbsp;

### Step 2: Run your docker image

```bash
docker run <your-image-name>
```

&nbsp;

To see some examples for instrumented applications, you can checkout [this link](https://signoz.io/docs/instrumentation/elixir/#sample-examples)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## Setup OpenTelemetry Binary as an agent

&nbsp;

As a first step, you should install the OTel collector Binary according to the instructions provided on [this link](https://signoz.io/docs/tutorial/opentelemetry-binary-usage-in-virtual-machine/).

&nbsp;

Once you are done setting up the OTel collector binary, you can follow the next steps.

&nbsp;

Loading
Loading