Skip to content

Commit

Permalink
feat(trait): Add telemetry trait
Browse files Browse the repository at this point in the history
* Define telemetry addon
* Jaeger discovery for OTLP API
* Basic configuration for tracing OTLP sampler
* update existing e2e using tracing to telemetry

Resolves: apache#3519
  • Loading branch information
gansheer committed Jan 16, 2023
1 parent efe78fe commit e86814e
Show file tree
Hide file tree
Showing 22 changed files with 579 additions and 30 deletions.
18 changes: 9 additions & 9 deletions addons/addons_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/apache/camel-k/addons/master"
"github.com/apache/camel-k/addons/tracing"
"github.com/apache/camel-k/addons/telemetry"
v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"github.com/apache/camel-k/pkg/trait"

Expand All @@ -44,7 +44,7 @@ func TestTraitConfiguration(t *testing.T) {
"labelKey": "test-label",
"labelValue": "test-value",
}),
"tracing": trait.ToAddonTrait(t, map[string]interface{}{
"telemetry": trait.ToAddonTrait(t, map[string]interface{}{
"enabled": true,
}),
},
Expand All @@ -63,10 +63,10 @@ func TestTraitConfiguration(t *testing.T) {
assert.Equal(t, "test-label", *master.LabelKey)
assert.Equal(t, "test-value", *master.LabelValue)

require.NotNil(t, c.GetTrait("tracing"))
tracing, ok := c.GetTrait("tracing").(*tracing.TestTracingTrait)
require.NotNil(t, c.GetTrait("telemetry"))
telemetry, ok := c.GetTrait("telemetry").(*telemetry.TestTelemetryTrait)
require.True(t, ok)
assert.True(t, *tracing.Enabled)
assert.True(t, *telemetry.Enabled)
}

func TestTraitConfigurationFromAnnotations(t *testing.T) {
Expand All @@ -78,7 +78,7 @@ func TestTraitConfigurationFromAnnotations(t *testing.T) {
"trait.camel.apache.org/master.resource-name": "test-lock",
"trait.camel.apache.org/master.label-key": "test-label",
"trait.camel.apache.org/master.label-value": "test-value",
"trait.camel.apache.org/tracing.enabled": "true",
"trait.camel.apache.org/telemetry.enabled": "true",
},
},
Spec: v1.IntegrationSpec{
Expand All @@ -97,8 +97,8 @@ func TestTraitConfigurationFromAnnotations(t *testing.T) {
assert.Equal(t, "test-label", *master.LabelKey)
assert.Equal(t, "test-value", *master.LabelValue)

require.NotNil(t, c.GetTrait("tracing"))
tracing, ok := c.GetTrait("tracing").(*tracing.TestTracingTrait)
require.NotNil(t, c.GetTrait("telemetry"))
telemetry, ok := c.GetTrait("telemetry").(*telemetry.TestTelemetryTrait)
require.True(t, ok)
assert.True(t, *tracing.Enabled)
assert.True(t, *telemetry.Enabled)
}
27 changes: 27 additions & 0 deletions addons/register_telemetry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package addons

import (
"github.com/apache/camel-k/addons/telemetry"
"github.com/apache/camel-k/pkg/trait"
)

func init() {
trait.AddToTraits(telemetry.NewTelemetryTrait)
}
73 changes: 73 additions & 0 deletions addons/telemetry/discovery/jaeger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package discovery

import (
"context"
"fmt"
"sort"
"strings"

"github.com/apache/camel-k/pkg/client"
"github.com/apache/camel-k/pkg/trait"
"github.com/apache/camel-k/pkg/util/log"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type JaegerTelemetryLocator struct {
allowHeadless bool
}

const (
jaegerPortName = "grpc-otlp"
)

func (loc *JaegerTelemetryLocator) FindEndpoint(ctx context.Context, c client.Client, l log.Logger, e *trait.Environment) (string, error) {
opts := metav1.ListOptions{
LabelSelector: "app.kubernetes.io/part-of=jaeger,app.kubernetes.io/component=service-collector",
}
lst, err := c.CoreV1().Services(e.Integration.Namespace).List(ctx, opts)
if err != nil {
return "", err
}
var candidates []string
for _, svc := range lst.Items {
if !loc.allowHeadless && strings.HasSuffix(svc.Name, "-headless") {
continue
}

for _, port := range svc.Spec.Ports {
if port.Name == jaegerPortName && port.Port > 0 {
candidates = append(candidates, fmt.Sprintf("http://%s.%s.svc.cluster.local:%d", svc.Name, svc.Namespace, port.Port))
}
}
}
sort.Strings(candidates)
if len(candidates) > 0 {
for _, endpoint := range candidates {
l.Infof("Detected Jaeger endpoint at: %s", endpoint)
}
return candidates[0], nil
}
return "", nil
}

// registering the locator.
func init() {
TelemetryLocators = append(TelemetryLocators, &JaegerTelemetryLocator{}, &JaegerTelemetryLocator{allowHeadless: true})
}
34 changes: 34 additions & 0 deletions addons/telemetry/discovery/locator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package discovery

import (
"context"

"github.com/apache/camel-k/pkg/client"
"github.com/apache/camel-k/pkg/trait"
"github.com/apache/camel-k/pkg/util/log"
)

// TelemetryLocators contains available telemetry OTLP locators.
var TelemetryLocators []TelemetryLocator

// TelemetryLocator is able to find the address of an available telemetry OTLP endpoint.
type TelemetryLocator interface {
FindEndpoint(context.Context, client.Client, log.Logger, *trait.Environment) (string, error)
}
158 changes: 158 additions & 0 deletions addons/telemetry/telemetry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package telemetry

import (
"k8s.io/utils/pointer"

"github.com/apache/camel-k/addons/telemetry/discovery"
v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
traitv1 "github.com/apache/camel-k/pkg/apis/camel/v1/trait"
"github.com/apache/camel-k/pkg/trait"
"github.com/apache/camel-k/pkg/util"
)

// The Telemetry trait can be used to automatically publish tracing information to an OTLP compatible collector.
//
// The trait is able to automatically discover the telemetry OTLP endpoint available in the namespace (supports **Jaerger** in version 1.35+).
//
// The Telemetry trait is disabled by default.
//
// WARNING: The Telemetry trait can't be enabled at the same time as the Tracing trait.
//
// +camel-k:trait=telemetry.
type Trait struct {
traitv1.Trait `property:",squash" json:",inline"`
// Enables automatic configuration of the trait, including automatic discovery of the telemetry endpoint.
Auto *bool `property:"auto" json:"auto,omitempty"`
// The name of the service that publishes telemetry data (defaults to the integration name)
ServiceName string `property:"service-name" json:"serviceName,omitempty"`
// The target endpoint of the Telemetry service (automatically discovered by default)
Endpoint string `property:"endpoint" json:"endpoint,omitempty"`
// The sampler of the telemetry used for tracing (default "on")
Sampler string `property:"sampler" json:"sampler,omitempty"`
// The sampler ratio of the telemetry used for tracing
SamplerRatio string `property:"sampler-ratio" json:"sampler-ratio,omitempty"`
// The sampler of the telemetry used for tracing is parent based (default "true")
SamplerParentBased *bool `property:"sampler-parent-based" json:"sampler-parent-based,omitempty"`
}

type telemetryTrait struct {
trait.BaseTrait
Trait `property:",squash"`
}

const (
propEnabled = "propEnabled"
propEndpoint = "propEndpoint"
propServiceName = "propServiceName"
propSampler = "propSampler"
propSamplerRatio = "propSamplerRatio"
propSamplerParentBased = "propSamplerParentBased"
)

var (
telemetryProperties = map[v1.RuntimeProvider]map[string]string{
v1.RuntimeProviderQuarkus: {
propEndpoint: "quarkus.opentelemetry.tracer.exporter.otlp.endpoint",
propServiceName: "quarkus.opentelemetry.tracer.resource-attributes",
propSampler: "quarkus.opentelemetry.tracer.sampler",
propSamplerRatio: "quarkus.opentelemetry.tracer.sampler.ratio",
propSamplerParentBased: "quarkus.opentelemetry.tracer.sampler.parent-based",
},
}
)

// NewTelemetryTrait instance the telemetry trait as a BaseTrait capable to inject quarkus properties.
func NewTelemetryTrait() trait.Trait {
return &telemetryTrait{
BaseTrait: trait.NewBaseTrait("telemetry", trait.TraitOrderBeforeControllerCreation),
}
}

func (t *telemetryTrait) Configure(e *trait.Environment) (bool, error) {
if e.Integration == nil || !pointer.BoolDeref(t.Enabled, false) {
return false, nil
}

if pointer.BoolDeref(t.Auto, true) {
if t.Endpoint == "" {
for _, locator := range discovery.TelemetryLocators {
endpoint, err := locator.FindEndpoint(e.Ctx, t.Client, t.L, e)
if err != nil {
return false, err
}
if endpoint != "" {
t.L.Infof("Using tracing endpoint: %s", endpoint)
t.Endpoint = endpoint
break
}
}
}

if t.ServiceName == "" {
t.ServiceName = e.Integration.Name
}

if t.Sampler == "" {
t.Sampler = "on"
}
}

return true, nil
}

func (t *telemetryTrait) Apply(e *trait.Environment) error {
util.StringSliceUniqueAdd(&e.Integration.Status.Capabilities, v1.CapabilityTelemetry)

if e.CamelCatalog != nil {
provider := e.CamelCatalog.CamelCatalogSpec.Runtime.Provider
properties := telemetryProperties[provider]

if appPropEnabled := properties[propEnabled]; appPropEnabled != "" {
e.ApplicationProperties[appPropEnabled] = "true"
}

if appPropEndpoint := properties[propEndpoint]; appPropEndpoint != "" && t.Endpoint != "" {
e.ApplicationProperties[appPropEndpoint] = t.Endpoint
}

if appPropServiceName := properties[propServiceName]; appPropServiceName != "" && t.ServiceName != "" {
e.ApplicationProperties[appPropServiceName] = "service.name=" + t.ServiceName
}

if appPropSampler := properties[propSampler]; appPropSampler != "" && t.Sampler != "" {
e.ApplicationProperties[appPropSampler] = t.Sampler
}

if appPropSamplerRatio := properties[propSamplerRatio]; appPropSamplerRatio != "" && t.SamplerRatio != "" {
e.ApplicationProperties[appPropSamplerRatio] = t.SamplerRatio
}

if appPropSamplerParentBased := properties[propSamplerParentBased]; appPropSamplerParentBased != "" {
if pointer.BoolDeref(t.SamplerParentBased, true) {
e.ApplicationProperties[appPropSamplerParentBased] = "true"
} else {
e.ApplicationProperties[appPropSamplerParentBased] = "false"
}
}

}

return nil
}
Loading

0 comments on commit e86814e

Please sign in to comment.