Skip to content

Commit

Permalink
Merge pull request #739 from logzio/go-docs-dec31
Browse files Browse the repository at this point in the history
nested metrics
  • Loading branch information
Simplychee authored Dec 31, 2024
2 parents 4ffdb5d + 95f71e8 commit a7b51b9
Showing 1 changed file with 112 additions and 113 deletions.
225 changes: 112 additions & 113 deletions docs/shipping/Code/go.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,118 @@ if err != nil {
l.Stop() // Drains the log buffer
```

</TabItem>
<TabItem value="OpenTelemetry" label="OpenTelemetry">

This integration uses the OpenTelemetry logging exporter to send logs to Logz.io via the OpenTelemetry Protocol (OTLP) listener.

### Prerequisites

- Go 1.21 or newer

:::note
If you need an example aplication to test this integration, please refer to our [Go OpenTelemetry repository](https://github.com/logzio/opentelemetry-examples/tree/main/go/logs).
:::

### Configure the instrumentation


1. Install OpenTelemetry dependencies:

```bash
go get go.opentelemetry.io/otel
go get go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp
go get go.opentelemetry.io/otel/exporters/stdout/stdoutlog
```

2. Create a new file named `otel.go` and add the following code to set up OpenTelemetry logging:


```go
package main

import (
"context"
"fmt"
"log"

"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp"
"go.opentelemetry.io/otel/exporters/stdout/stdoutlog"
"go.opentelemetry.io/otel/log/global"
sdklog "go.opentelemetry.io/otel/sdk/log"
)

func newLoggerProvider() (*sdklog.LoggerProvider, error) {
// Create stdout log exporter
stdoutExporter, err := stdoutlog.New(stdoutlog.WithPrettyPrint())
if err != nil {
return nil, fmt.Errorf("failed to create stdout exporter: %w", err)
}

// Create OTLP HTTP log exporter for Logz.io
httpExporter, err := otlploghttp.New(context.Background(),
otlploghttp.WithEndpoint("otlp-listener.logz.io"),
otlploghttp.WithHeaders(map[string]string{
"Authorization": "Bearer <LOG-SHIPPING-TOKEN>",
"user-agent": "logzio-go-logs-otlp",
}),
otlploghttp.WithURLPath("/v1/logs"),
)
if err != nil {
return nil, fmt.Errorf("failed to create OTLP HTTP exporter: %w", err)
}

// Create a logger provider with both exporters
loggerProvider := sdklog.NewLoggerProvider(
sdklog.WithProcessor(sdklog.NewBatchProcessor(stdoutExporter)), // For stdout
sdklog.WithProcessor(sdklog.NewBatchProcessor(httpExporter)), // For HTTP export
)

return loggerProvider, nil
}

// setupOTelSDK bootstraps the OpenTelemetry logging pipeline.
func setupOTelSDK(ctx context.Context) (shutdown func(context.Context) error, err error) {
// Set up logger provider.
loggerProvider, err := newLoggerProvider()
if err != nil {
return nil, err
}

// Set the global logger provider
global.SetLoggerProvider(loggerProvider)

// Return a shutdown function
shutdown = func(ctx context.Context) error {
err := loggerProvider.Shutdown(ctx)
if err != nil {
log.Printf("Error during logger provider shutdown: %v", err)
}
return err
}

return shutdown, nil
}

```


{@include: ../../_include/log-shipping/log-shipping-token.md}
Update the `listener.logz.io` part in `https://otlp-listener.logz.io/v1/logs` with the URL for [your hosting region](https://docs.logz.io/docs/user-guide/admin/hosting-regions/account-region).


3. Run your application.

### Check Logz.io for your logs


Allow some time for data ingestion, then open [Open Search Dashboards](https://app.logz.io/#/dashboard/osd).

Encounter an issue? See our [log shipping troubleshooting](https://docs.logz.io/docs/user-guide/log-management/troubleshooting/log-shipping-troubleshooting/) guide.


</TabItem>
</Tabs>

## Metrics

Expand Down Expand Up @@ -379,119 +491,6 @@ Install the pre-built dashboard to enhance the observability of your metrics.

{@include: ../../_include/metric-shipping/generic-dashboard.html}

</TabItem>
<TabItem value="OpenTelemetry" label="OpenTelemetry">

This integration uses the OpenTelemetry logging exporter to send logs to Logz.io via the OpenTelemetry Protocol (OTLP) listener.

### Prerequisites

- Go 1.21 or newer

:::note
If you need an example aplication to test this integration, please refer to our [Go OpenTelemetry repository](https://github.com/logzio/opentelemetry-examples/tree/main/go/logs).
:::

### Configure the instrumentation


1. Install OpenTelemetry dependencies:

```bash
go get go.opentelemetry.io/otel
go get go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp
go get go.opentelemetry.io/otel/exporters/stdout/stdoutlog
```

2. Create a new file named `otel.go` and add the following code to set up OpenTelemetry logging:


```go
package main

import (
"context"
"fmt"
"log"

"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp"
"go.opentelemetry.io/otel/exporters/stdout/stdoutlog"
"go.opentelemetry.io/otel/log/global"
sdklog "go.opentelemetry.io/otel/sdk/log"
)

func newLoggerProvider() (*sdklog.LoggerProvider, error) {
// Create stdout log exporter
stdoutExporter, err := stdoutlog.New(stdoutlog.WithPrettyPrint())
if err != nil {
return nil, fmt.Errorf("failed to create stdout exporter: %w", err)
}

// Create OTLP HTTP log exporter for Logz.io
httpExporter, err := otlploghttp.New(context.Background(),
otlploghttp.WithEndpoint("otlp-listener.logz.io"),
otlploghttp.WithHeaders(map[string]string{
"Authorization": "Bearer <LOG-SHIPPING-TOKEN>",
"user-agent": "logzio-go-logs-otlp",
}),
otlploghttp.WithURLPath("/v1/logs"),
)
if err != nil {
return nil, fmt.Errorf("failed to create OTLP HTTP exporter: %w", err)
}

// Create a logger provider with both exporters
loggerProvider := sdklog.NewLoggerProvider(
sdklog.WithProcessor(sdklog.NewBatchProcessor(stdoutExporter)), // For stdout
sdklog.WithProcessor(sdklog.NewBatchProcessor(httpExporter)), // For HTTP export
)

return loggerProvider, nil
}

// setupOTelSDK bootstraps the OpenTelemetry logging pipeline.
func setupOTelSDK(ctx context.Context) (shutdown func(context.Context) error, err error) {
// Set up logger provider.
loggerProvider, err := newLoggerProvider()
if err != nil {
return nil, err
}

// Set the global logger provider
global.SetLoggerProvider(loggerProvider)

// Return a shutdown function
shutdown = func(ctx context.Context) error {
err := loggerProvider.Shutdown(ctx)
if err != nil {
log.Printf("Error during logger provider shutdown: %v", err)
}
return err
}

return shutdown, nil
}

```


{@include: ../../_include/log-shipping/log-shipping-token.md}
Update the `listener.logz.io` part in `https://otlp-listener.logz.io/v1/logs` with the URL for [your hosting region](https://docs.logz.io/docs/user-guide/admin/hosting-regions/account-region).


3. Run your application.

### Check Logz.io for your logs


Allow some time for data ingestion, then open [Open Search Dashboards](https://app.logz.io/#/dashboard/osd).

Encounter an issue? See our [log shipping troubleshooting](https://docs.logz.io/docs/user-guide/log-management/troubleshooting/log-shipping-troubleshooting/) guide.


</TabItem>
</Tabs>


## Traces

Expand Down

0 comments on commit a7b51b9

Please sign in to comment.