Skip to content

Commit

Permalink
Update metric example to use resource detectors (#724)
Browse files Browse the repository at this point in the history
* update metric example to use resource detectors

Co-authored-by: Aaron Abbott <aaronabbott@google.com>
  • Loading branch information
dashpole and aabmass authored Sep 21, 2023
1 parent fe9ad49 commit 412fafa
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
23 changes: 22 additions & 1 deletion example/metric/sdk/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ import (

mexporter "github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric"

"go.opentelemetry.io/contrib/detectors/gcp"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.18.0"
)

type observedFloat struct {
Expand All @@ -53,6 +56,7 @@ func newObservedFloat(v float64) *observedFloat {
}

func main() {
ctx := context.Background()
// Initialization. In order to pass the credentials to the exporter,
// prepare credential file following the instruction described in this doc.
// https://pkg.go.dev/golang.org/x/oauth2/google?tab=doc#FindDefaultCredentials
Expand All @@ -61,11 +65,28 @@ func main() {
log.Fatalf("Failed to create exporter: %v", err)
}

res, err := resource.New(
ctx,
// Use the GCP resource detector to detect information about the GCP platform
resource.WithDetectors(gcp.NewDetector()),
// Keep the default detectors
resource.WithTelemetrySDK(),
// Add attributes from environment variables
resource.WithFromEnv(),
// Add your own custom attributes to identify your application
resource.WithAttributes(
semconv.ServiceNameKey.String("example-application"),
),
)
if err != nil {
log.Fatalf("resource.New: %v", err)
}

// initialize a MeterProvider with that periodically exports to the GCP exporter.
provider := sdkmetric.NewMeterProvider(
sdkmetric.WithReader(sdkmetric.NewPeriodicReader(exporter)),
sdkmetric.WithResource(res),
)
ctx := context.Background()
defer func() {
if err = provider.Shutdown(ctx); err != nil {
log.Fatalf("failed to shutdown meter provider: %v", err)
Expand Down
6 changes: 5 additions & 1 deletion example/metric/sdk/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ go 1.20

require (
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.43.1
go.opentelemetry.io/contrib/detectors/gcp v1.19.0
go.opentelemetry.io/otel v1.18.0
go.opentelemetry.io/otel/metric v1.18.0
go.opentelemetry.io/otel/sdk v1.18.0
go.opentelemetry.io/otel/sdk/metric v0.41.0
)

require (
cloud.google.com/go/compute v1.23.0 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/monitoring v1.15.1 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.19.1 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.43.1 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
Expand All @@ -23,7 +26,6 @@ require (
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
github.com/googleapis/gax-go/v2 v2.11.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/otel/sdk v1.18.0 // indirect
go.opentelemetry.io/otel/trace v1.18.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/net v0.10.0 // indirect
Expand All @@ -45,3 +47,5 @@ replace github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metr
replace github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/cloudmock => ../../../internal/cloudmock

replace github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping => ../../../internal/resourcemapping

replace github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp => ../../../detectors/gcp
2 changes: 2 additions & 0 deletions example/metric/sdk/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=
go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo=
go.opentelemetry.io/contrib/detectors/gcp v1.19.0 h1:jQuVHQiHTnJa8JmZLe8ivKHZUh6x7pqC0DSGDG376II=
go.opentelemetry.io/contrib/detectors/gcp v1.19.0/go.mod h1:0OKp3ML259QXdgA40Ij04gxOHosnWLHAIh/zAjG5mu4=
go.opentelemetry.io/otel v1.18.0 h1:TgVozPGZ01nHyDZxK5WGPFB9QexeTMXEH7+tIClWfzs=
go.opentelemetry.io/otel v1.18.0/go.mod h1:9lWqYO0Db579XzVuCKFNPDl4s73Voa+zEck3wHaAYQI=
go.opentelemetry.io/otel/metric v1.18.0 h1:JwVzw94UYmbx3ej++CwLUQZxEODDj/pOuTCvzhtRrSQ=
Expand Down

0 comments on commit 412fafa

Please sign in to comment.