-
Notifications
You must be signed in to change notification settings - Fork 489
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2224 from jsternberg/otelsdk-package
otel: include service instance id attribute to resource and move to metricutil package
- Loading branch information
Showing
7 changed files
with
149 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package confutil | ||
|
||
import ( | ||
"os" | ||
"strconv" | ||
) | ||
|
||
// IsExperimental checks if the experimental flag has been configured. | ||
func IsExperimental() bool { | ||
if v, ok := os.LookupEnv("BUILDX_EXPERIMENTAL"); ok { | ||
vv, _ := strconv.ParseBool(v) | ||
return vv | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package metricutil | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path/filepath" | ||
"sync" | ||
|
||
"github.com/google/uuid" | ||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/sdk/resource" | ||
semconv "go.opentelemetry.io/otel/semconv/v1.21.0" | ||
) | ||
|
||
var ( | ||
res *resource.Resource | ||
resOnce sync.Once | ||
) | ||
|
||
// Resource retrieves the OTEL resource for the buildx CLI. | ||
func Resource() *resource.Resource { | ||
resOnce.Do(func() { | ||
var err error | ||
res, err = resource.New(context.Background(), | ||
resource.WithDetectors(serviceNameDetector{}), | ||
resource.WithAttributes( | ||
// Use a unique instance id so OTEL knows that each invocation | ||
// of the CLI is its own instance. Without this, downstream | ||
// OTEL processors may think the same process is restarting | ||
// continuously and reset the metric counters. | ||
semconv.ServiceInstanceID(uuid.New().String()), | ||
), | ||
resource.WithFromEnv(), | ||
resource.WithTelemetrySDK(), | ||
) | ||
if err != nil { | ||
otel.Handle(err) | ||
} | ||
}) | ||
return res | ||
} | ||
|
||
type serviceNameDetector struct{} | ||
|
||
func (serviceNameDetector) Detect(ctx context.Context) (*resource.Resource, error) { | ||
return resource.StringDetector( | ||
semconv.SchemaURL, | ||
semconv.ServiceNameKey, | ||
func() (string, error) { | ||
return filepath.Base(os.Args[0]), nil | ||
}, | ||
).Detect(ctx) | ||
} |