Hosted
.NET
Erlang/Elixir
Golang
Java
JavaScript
PHP
Python
Ruby
Rust
Resource Detectors in OpenTelemetry Go
Resource detectors are used to automatically detect and collect information about the environment in which your application is running. This information is then attached to the telemetry data (traces, metrics, and logs) to provide additional context.
OpenTelemetry Go provides several built-in resource detectors, and you can also create custom detectors to gather specific information relevant to your application.
By default, uptrace-go uses host and environment resource detectors, but you can configure it to use additional detectors, for example:
go
import (
"github.com/uptrace/uptrace-go/uptrace"
"go.opentelemetry.io/contrib/detectors/aws/ec2"
)
uptrace.ConfigureOpentelemetry(
// copy your project DSN here or use UPTRACE_DSN env var
//uptrace.WithDSN("<FIXME>"),
uptrace.WithServiceName("myservice"),
uptrace.WithServiceVersion("1.0.0"),
uptrace.WithResourceDetectors(ec2.NewResourceDetector()),
)
AWS
See AWS detectors.
EC2
go
import "go.opentelemetry.io/contrib/detectors/aws/ec2"
ec2ResourceDetector := ec2.NewResourceDetector()
ECS
go
import "go.opentelemetry.io/contrib/detectors/aws/ecs"
ecsResourceDetector := ecs.NewResourceDetector()
EKS
go
import "go.opentelemetry.io/contrib/detectors/aws/eks"
eksResourceDetector := eks.NewResourceDetector()
Google Cloud
See GCP detectors.
Cloud Run
go
import "go.opentelemetry.io/contrib/detectors/gcp"
cloudRunResourceDetector := gcp.NewCloudRun()
GCE
go
import "go.opentelemetry.io/contrib/detectors/gcp"
gceResourceDetector := gcp.GCE{}
GKE
go
import "go.opentelemetry.io/contrib/detectors/gcp"
gkeResourceDetector := gcp.GKE{}
Custom resource detector
If you need to detect custom information, you can create your own resource detector by implementing the Detector
interface.
go
package main
import (
"context"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
"log"
)
type CustomDetector struct{}
func (d *CustomDetector) Detect(ctx context.Context) (*resource.Resource, error) {
return resource.New(ctx,
resource.WithAttributes(
attribute.String("custom.key", "custom-value"),
),
)
}
func main() {
ctx := context.Background()
// Create a resource with a custom detector
res, err := resource.New(ctx,
resource.WithDetectors(&CustomDetector{}),
resource.WithHost(),
resource.WithProcess(),
)
if err != nil {
log.Fatalf("Failed to create resource: %v", err)
}
// Log the detected resource attributes
log.Printf("Detected resource: %v", res)
}