Skip to content
This repository has been archived by the owner on Jun 17, 2024. It is now read-only.

feat:docker instructions #1616

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion docs/instrumentation/dotnet.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ For .NET application deployed on Kubernetes, you need to install OTel Collector

Once you have set up OTel Collector agent, you can proceed with OpenTelemetry .NET instrumentation by following the below steps:

**Step 2: Installing the OpenTelemetry dependency packages:**
**Step 1: Installing the OpenTelemetry dependency packages:**

Install the following dependencies in your application.

Expand Down
315 changes: 315 additions & 0 deletions docs/instrumentation/golang.md
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,321 @@ Once you have set up OTel Collector agent, you can proceed with OpenTelemetry Go
If you want to update your `service_name`, you can modify the `SERVICE_NAME` variable.<br></br>
`SERVICE_NAME`: goGinApp (you can name it whatever you want)

7. **Dockerize your application (Optional)**<br></br>
To dockerize your application, you don't need to do **step 6** mentioned above, instead you just set the Environment variables before your run command in Dockerfile as:

```bash
...
# Set environment variables
ENV SERVICE_NAME=<service-name> \
INSECURE_MODE=true \
OTEL_EXPORTER_OTLP_ENDPOINT=localhost:4317 \
...
```

Now build and run your docker application.

8. You can validate if your application is sending traces to SigNoz cloud by following the instructions [here](#validating-instrumentation-by-checking-for-traces).

</TabItem>

<TabItem value="docker" label="Docker" default>

There are two ways to send data to SigNoz Cloud:

- Send traces directly to SigNoz Cloud
- Send traces via OTel Collector binary **(recommended)**

#### **Send traces directly to SigNoz Cloud**

1. **Install Dependencies**<br></br>
Dependencies related to OpenTelemetry exporter and SDK have to be installed first. Note that we are assuming you are using `gin` request router. If you are using other request routers, check out the [corresponding package](#request-routers).

Run the below commands after navigating to the application source folder:

```bash
go get go.opentelemetry.io/otel \
go.opentelemetry.io/otel/trace \
go.opentelemetry.io/otel/sdk \
go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin \
go.opentelemetry.io/otel/exporters/otlp/otlptrace \
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc
```

2. **Declare environment variables for configuring OpenTelemetry**<br></br>
Declare the following global variables in `main.go` which we will use to configure OpenTelemetry:

```bash
var (
serviceName = os.Getenv("SERVICE_NAME")
collectorURL = os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT")
insecure = os.Getenv("INSECURE_MODE")
)
```

3. **Instrument your Go application with OpenTelemetry**<br></br>
To configure your application to send data we will need a function to initialize OpenTelemetry. Add the following snippet of code in your `main.go` file.

```go

import (
.....

"google.golang.org/grpc/credentials"
"github.com/gin-gonic/gin"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"

"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)

func initTracer() func(context.Context) error {

var secureOption otlptracegrpc.Option

if strings.ToLower(insecure) == "false" || insecure == "0" || strings.ToLower(insecure) == "f" {
secureOption = otlptracegrpc.WithTLSCredentials(credentials.NewClientTLSFromCert(nil, ""))
} else {
secureOption = otlptracegrpc.WithInsecure()
}

exporter, err := otlptrace.New(
context.Background(),
otlptracegrpc.NewClient(
secureOption,
otlptracegrpc.WithEndpoint(collectorURL),
),
)

if err != nil {
log.Fatalf("Failed to create exporter: %v", err)
}
resources, err := resource.New(
context.Background(),
resource.WithAttributes(
attribute.String("service.name", serviceName),
attribute.String("library.language", "go"),
),
)
if err != nil {
log.Fatalf("Could not set resources: %v", err)
}

otel.SetTracerProvider(
sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithBatcher(exporter),
sdktrace.WithResource(resources),
),
)
return exporter.Shutdown
}
```

4. **Initialize the tracer in main.go**<br></br>
Modify the main function to initialise the tracer in `main.go`. Initiate the tracer at the very beginning of our main function.

```go
func main() {
cleanup := initTracer()
defer cleanup(context.Background())

......
}
```

5. **Add the OpenTelemetry Gin middleware**<br></br>
Configure Gin to use the middleware by adding the following lines in `main.go`.

```go
import (
....
"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
)

func main() {
......
r := gin.Default()
r.Use(otelgin.Middleware(serviceName))
......
}
```

6. **Dockerize your application**<br></br>
Update the dockerfile to set the environment variable as shown below:

```bash
...
# Set environment variables
ENV SERVICE_NAME=<service-name> \
INSECURE_MODE=false \
OTEL_EXPORTER_OTLP_HEADERS="signoz-access-token=<SIGNOZ-INGESTION-KEY>" \
OTEL_EXPORTER_OTLP_ENDPOINT=ingest.in.signoz.cloud:443
...
```

Now build and run your docker application.

We can replace the placeholders based on our environment.

`<service-name>`: name of your service or application

`<SIGNOZ-INGESTION-TOKEN>` is the Ingestion Key provided by SigNoz. You can find it in the email sent by SigNoz with your cloud account details.

Depending on the choice of your region for SigNoz cloud, the ingest endpoint will vary according to this table.

| Region | Endpoint |
| --- | --- |
| US | ingest.us.signoz.cloud:443 |
| IN | ingest.in.signoz.cloud:443 |
| EU | ingest.eu.signoz.cloud:443 |


---


#### **Send traces via OTel Collector binary**

OTel Collector binary helps to collect logs, hostmetrics, resource and infra attributes. It is recommended to install Otel Collector binary to collect and send traces to SigNoz cloud. You can correlate signals and have rich contextual data through this way.

You can find instructions to install OTel Collector binary [here](https://signoz.io/docs/tutorial/opentelemetry-binary-usage-in-virtual-machine/) in your VM. Once you are done setting up your OTel Collector binary, you can follow the below steps for instrumenting your Golang application.

1. **Install Dependencies**<br></br>
Dependencies related to OpenTelemetry exporter and SDK have to be installed first. Note that we are assuming you are using `gin` request router. If you are using other request routers, check out the [corresponding package](#request-routers).

Run the below commands after navigating to the application source folder:

```bash
go get go.opentelemetry.io/otel \
go.opentelemetry.io/otel/trace \
go.opentelemetry.io/otel/sdk \
go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin \
go.opentelemetry.io/otel/exporters/otlp/otlptrace \
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc
```

2. **Declare environment variables for configuring OpenTelemetry**<br></br>
Declare the following global variables in `main.go` which we will use to configure OpenTelemetry:

```go
var (
serviceName = os.Getenv("SERVICE_NAME")
collectorURL = os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT")
insecure = os.Getenv("INSECURE_MODE")
)
```

3. **Instrument your Go application with OpenTelemetry**<br></br>
To configure your application to send data we will need a function to initialize OpenTelemetry. Add the following snippet of code in your `main.go` file.

```go

import (
.....

"google.golang.org/grpc/credentials"
"github.com/gin-gonic/gin"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"

"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)

func initTracer() func(context.Context) error {

var secureOption otlptracegrpc.Option

if strings.ToLower(insecure) == "false" || insecure == "0" || strings.ToLower(insecure) == "f" {
secureOption = otlptracegrpc.WithTLSCredentials(credentials.NewClientTLSFromCert(nil, ""))
} else {
secureOption = otlptracegrpc.WithInsecure()
}

exporter, err := otlptrace.New(
context.Background(),
otlptracegrpc.NewClient(
secureOption,
otlptracegrpc.WithEndpoint(collectorURL),
),
)

if err != nil {
log.Fatalf("Failed to create exporter: %v", err)
}
resources, err := resource.New(
context.Background(),
resource.WithAttributes(
attribute.String("service.name", serviceName),
attribute.String("library.language", "go"),
),
)
if err != nil {
log.Fatalf("Could not set resources: %v", err)
}

otel.SetTracerProvider(
sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithBatcher(exporter),
sdktrace.WithResource(resources),
),
)
return exporter.Shutdown
}

4. **Initialize the tracer in main.go**<br></br>
Modify the main function to initialise the tracer in `main.go`. Initiate the tracer at the very beginning of our main function.

```go
func main() {
cleanup := initTracer()
defer cleanup(context.Background())

......
}
```

5. **Add the OpenTelemetry Gin middleware**<br></br>
Configure Gin to use the middleware by adding the following lines in `main.go`.

```go
import (
....
"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
)

func main() {
......
r := gin.Default()
r.Use(otelgin.Middleware(serviceName))
......
}
```

6. **Dockerize your application**<br></br>
Update the dockerfile to set the environment variable as shown below:

```bash
...
# Set environment variables
ENV SERVICE_NAME=<service-name> \
INSECURE_MODE=true \
OTEL_EXPORTER_OTLP_ENDPOINT=localhost:4317 \
...
```

Now build and run your docker application.

We can replace the placeholders based on our environment.

`<service-name>`: name of your service or application

7. You can validate if your application is sending traces to SigNoz cloud by following the instructions [here](#validating-instrumentation-by-checking-for-traces).

</TabItem>
Expand Down
Loading
Loading