mirror of https://github.com/knative/pkg.git
[observability/resource] fix service name calculation (#3204)
* fix service name calculation * drop comment * fix linter * simplify comment
This commit is contained in:
parent
68cdb02d48
commit
9435aa66de
|
|
@ -17,6 +17,8 @@ limitations under the License.
|
|||
package resource
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"knative.dev/pkg/changeset"
|
||||
"knative.dev/pkg/system"
|
||||
|
||||
|
|
@ -25,6 +27,8 @@ import (
|
|||
semconv "go.opentelemetry.io/otel/semconv/v1.34.0"
|
||||
)
|
||||
|
||||
const otelServiceNameKey = "OTEL_SERVICE_NAME"
|
||||
|
||||
// Default returns a default OpenTelemetry Resource enriched
|
||||
// with common Knative/Kubernetes attributes.
|
||||
//
|
||||
|
|
@ -33,10 +37,16 @@ import (
|
|||
// - PodName using system.PodName
|
||||
// - ServiceVersion with changeset.Get
|
||||
func Default(serviceName string) *resource.Resource {
|
||||
// If the OTEL_SERVICE_NAME is set then let this override
|
||||
// our own serviceName
|
||||
if name := os.Getenv(otelServiceNameKey); name != "" {
|
||||
serviceName = name
|
||||
}
|
||||
|
||||
attrs := []attribute.KeyValue{
|
||||
semconv.K8SNamespaceName(system.Namespace()),
|
||||
semconv.ServiceName(serviceName),
|
||||
semconv.ServiceVersion(changeset.Get()),
|
||||
semconv.ServiceName(serviceName),
|
||||
}
|
||||
|
||||
if pn := system.PodName(); pn != "" {
|
||||
|
|
@ -46,13 +56,11 @@ func Default(serviceName string) *resource.Resource {
|
|||
// Ignore the error because it complains about semconv
|
||||
// schema version differences
|
||||
resource, err := resource.Merge(
|
||||
resource.Default(),
|
||||
resource.NewWithAttributes(
|
||||
semconv.SchemaURL,
|
||||
attrs...,
|
||||
),
|
||||
// We merge 'Default' last since this allows overriding
|
||||
// the service name etc. using env variables
|
||||
resource.Default(),
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
|
|
|||
|
|
@ -19,6 +19,9 @@ package resource
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
semconv "go.opentelemetry.io/otel/semconv/v1.34.0"
|
||||
|
||||
_ "knative.dev/pkg/system/testing"
|
||||
)
|
||||
|
||||
|
|
@ -28,4 +31,35 @@ func TestDefault(t *testing.T) {
|
|||
if r == nil {
|
||||
t.Fatalf("expected resource to not be nil")
|
||||
}
|
||||
|
||||
for _, v := range r.Attributes() {
|
||||
if v.Key == semconv.ServiceNameKey {
|
||||
got := v.Value.AsString()
|
||||
want := "myservice"
|
||||
|
||||
if diff := cmp.Diff(want, got); diff != "" {
|
||||
t.Error("unexpected service name diff (-want +got):", diff)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultWithEnvOverride(t *testing.T) {
|
||||
t.Setenv(otelServiceNameKey, "another")
|
||||
r := Default("myservice")
|
||||
|
||||
if r == nil {
|
||||
t.Fatalf("expected resource to not be nil")
|
||||
}
|
||||
|
||||
for _, v := range r.Attributes() {
|
||||
if v.Key == semconv.ServiceNameKey {
|
||||
got := v.Value.AsString()
|
||||
want := "another"
|
||||
|
||||
if diff := cmp.Diff(want, got); diff != "" {
|
||||
t.Error("unexpected service name diff (-want +got):", diff)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue