Skip to content

feat: deployment.environment.name instead of deprecated deployment.environment #554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 8, 2025
Merged
7 changes: 6 additions & 1 deletion extension/apmconfigextension/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ the
[service.name](https://www.elastic.co/guide/en/ecs/1.12/ecs-service.html#field-service-name)
and
[service.environment](https://www.elastic.co/guide/en/ecs/1.12/ecs-service.html#field-service-environment)
(optional) attributes. These attributes **must** be set on the
(optional) attributes. The equivalent OpenTelemetry Semantic Conventions
attributes are:
- [service.name](https://github.com/open-telemetry/semantic-conventions/blob/v1.32.0/docs/attributes-registry/service.md)
- [deployment.environment.name](https://github.com/open-telemetry/semantic-conventions/blob/v1.32.0/docs/attributes-registry/deployment.md)

These attributes (`service.name` and optionally `deployment.environment.name`) **must** be set on the
[AgentDescription.identifying_attributes](https://github.com/open-telemetry/opamp-spec/blob/main/specification.md#agentdescriptionidentifying_attributes)
field during the first send
[AgentToServer](https://github.com/open-telemetry/opamp-spec/blob/main/specification.md#agenttoserver-message)
Expand Down
8 changes: 7 additions & 1 deletion extension/apmconfigextension/apmconfig/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@ import (

var UnidentifiedAgent = errors.New("agent could not be identified")

// protobufs.AgentToServer.InstanceUid
type (
InstanceUid []byte
IdentifyingAttributes []*protobufs.KeyValue
)

// RemoteConfigClient is an adapter interface that can be used between different
// remote configuration providers.
type RemoteConfigClient interface {
// RemoteConfig returns the upstream remote configuration that needs to be applied. Empty RemoteConfig Attrs if no remote configuration is available for the specified service.
RemoteConfig(context.Context, *protobufs.AgentToServer) (*protobufs.AgentRemoteConfig, error)
RemoteConfig(context.Context, InstanceUid, IdentifyingAttributes) (*protobufs.AgentRemoteConfig, error)
}
41 changes: 14 additions & 27 deletions extension/apmconfigextension/elastic/centralconfig/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ import (
"context"
"encoding/json"
"fmt"
"time"

"github.com/elastic/opentelemetry-collector-components/extension/apmconfigextension/apmconfig"
"github.com/elastic/opentelemetry-lib/agentcfg"
"github.com/open-telemetry/opamp-go/protobufs"
semconv "go.opentelemetry.io/collector/semconv/v1.26.0"
semconv "go.opentelemetry.io/otel/semconv/v1.28.0"
"go.uber.org/zap"
)

Expand All @@ -36,49 +35,37 @@ var _ apmconfig.RemoteConfigClient = (*fetcherAPMWatcher)(nil)

type fetcherAPMWatcher struct {
configFetcher agentcfg.Fetcher
cacheDuration time.Duration

// OpAMP instanceID to service mapping
uidToService map[string]agentcfg.Service

logger *zap.Logger
logger *zap.Logger
}

func NewFetcherAPMWatcher(fetcher agentcfg.Fetcher, cacheDuration time.Duration, logger *zap.Logger) *fetcherAPMWatcher {
func NewFetcherAPMWatcher(fetcher agentcfg.Fetcher, logger *zap.Logger) *fetcherAPMWatcher {
return &fetcherAPMWatcher{
configFetcher: fetcher,
cacheDuration: cacheDuration,
uidToService: make(map[string]agentcfg.Service),
logger: logger,
}
}

func (fw *fetcherAPMWatcher) RemoteConfig(ctx context.Context, agentMsg *protobufs.AgentToServer) (*protobufs.AgentRemoteConfig, error) {
if agentDescription := agentMsg.GetAgentDescription(); agentDescription != nil {
var serviceParams agentcfg.Service
for _, attr := range agentDescription.GetIdentifyingAttributes() {
switch attr.GetKey() {
case semconv.AttributeServiceName:
serviceParams.Name = attr.GetValue().GetStringValue()
case semconv.AttributeDeploymentEnvironment:
serviceParams.Environment = attr.GetValue().GetStringValue()
}
}
// only update the internal cache if service name is set
if serviceParams.Name != "" {
fw.uidToService[string(agentMsg.GetInstanceUid())] = serviceParams
func (fw *fetcherAPMWatcher) RemoteConfig(ctx context.Context, agentUid apmconfig.InstanceUid, agentAttrs apmconfig.IdentifyingAttributes) (*protobufs.AgentRemoteConfig, error) {
var serviceParams agentcfg.Service
for _, attr := range agentAttrs {
switch attr.GetKey() {
case string(semconv.ServiceNameKey):
serviceParams.Name = attr.GetValue().GetStringValue()
case string(semconv.DeploymentEnvironmentNameKey):
serviceParams.Environment = attr.GetValue().GetStringValue()
}
}

serviceParams, ok := fw.uidToService[string(agentMsg.GetInstanceUid())]
if !ok || serviceParams.Name == "" {
if serviceParams.Name == "" {
return nil, fmt.Errorf("%w: service.name attribute must be provided", apmconfig.UnidentifiedAgent)
}
result, err := fw.configFetcher.Fetch(ctx, agentcfg.Query{
Service: serviceParams,
})
if err != nil {
return nil, err
} else if len(result.Source.Settings) == 0 {
return nil, nil
}

marshallConfig, err := json.Marshal(result.Source.Settings)
Expand Down
131 changes: 131 additions & 0 deletions extension/apmconfigextension/elastic/centralconfig/fetcher_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. 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 centralconfig // import "github.com/elastic/opentelemetry-collector-components/extension/apmconfigextension/elastic/centralconfig"

import (
"context"
"errors"
"testing"

"github.com/elastic/opentelemetry-collector-components/extension/apmconfigextension/apmconfig"
"github.com/elastic/opentelemetry-lib/agentcfg"
"github.com/open-telemetry/opamp-go/protobufs"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
)

type agentFetcherMock struct {
fetchFn func(context.Context, agentcfg.Query) (agentcfg.Result, error)
}

func (f *agentFetcherMock) Fetch(ctx context.Context, query agentcfg.Query) (agentcfg.Result, error) {
return f.fetchFn(ctx, query)
}

func TestRemoteConfig(t *testing.T) {
testcases := map[string]struct {
agentUid apmconfig.InstanceUid
agentAttrs apmconfig.IdentifyingAttributes
mockedFetchFn func(context.Context, agentcfg.Query) (agentcfg.Result, error)

expectedRemoteConfig *protobufs.AgentRemoteConfig
expectedError error
}{
"no identifying attributes": {
agentUid: apmconfig.InstanceUid("test-agent"),
mockedFetchFn: func(context.Context, agentcfg.Query) (agentcfg.Result, error) {
return agentcfg.Result{}, nil
},
expectedRemoteConfig: nil,
expectedError: apmconfig.UnidentifiedAgent,
},
"no service.name identifying attribute": {
agentUid: apmconfig.InstanceUid("test-agent"),
agentAttrs: apmconfig.IdentifyingAttributes{
&protobufs.KeyValue{
Key: "deployment.environment",
Value: &protobufs.AnyValue{Value: &protobufs.AnyValue_StringValue{StringValue: "dev"}},
},
},
mockedFetchFn: func(context.Context, agentcfg.Query) (agentcfg.Result, error) {
return agentcfg.Result{}, nil
},
expectedRemoteConfig: nil,
expectedError: apmconfig.UnidentifiedAgent,
},
"valid service.name": {
agentUid: apmconfig.InstanceUid("test-agent"),
agentAttrs: apmconfig.IdentifyingAttributes{
&protobufs.KeyValue{
Key: "service.name",
Value: &protobufs.AnyValue{Value: &protobufs.AnyValue_StringValue{StringValue: "dev"}},
},
},
mockedFetchFn: func(context.Context, agentcfg.Query) (agentcfg.Result, error) {
return agentcfg.Result{
Source: agentcfg.Source{
Etag: "abcd",
Settings: agentcfg.Settings{
"test": "aaa",
},
},
}, nil
},
expectedRemoteConfig: &protobufs.AgentRemoteConfig{
ConfigHash: []byte("abcd"),
Config: &protobufs.AgentConfigMap{
ConfigMap: map[string]*protobufs.AgentConfigFile{
"": {
Body: []byte(`{"test":"aaa"}`),
ContentType: "text/json",
},
},
},
},
expectedError: nil,
},
"fetcher error": {
agentUid: apmconfig.InstanceUid("test-agent"),
agentAttrs: apmconfig.IdentifyingAttributes{
&protobufs.KeyValue{
Key: "service.name",
Value: &protobufs.AnyValue{Value: &protobufs.AnyValue_StringValue{StringValue: "dev"}},
},
},
mockedFetchFn: func(context.Context, agentcfg.Query) (agentcfg.Result, error) {
return agentcfg.Result{}, errors.New("mocked fetch error")
},
expectedRemoteConfig: nil,
expectedError: errors.New("mocked fetch error"),
},
}

for name, tt := range testcases {
t.Run(name, func(t *testing.T) {
fetcher := NewFetcherAPMWatcher(&agentFetcherMock{
fetchFn: tt.mockedFetchFn,
}, zap.NewNop())

actualRemoteConfig, actualError := fetcher.RemoteConfig(context.Background(), tt.agentUid, tt.agentAttrs)
if tt.expectedError != nil {
assert.ErrorContains(t, actualError, tt.expectedError.Error())
}
assert.Equal(t, tt.expectedRemoteConfig, actualRemoteConfig)
})
}
}
Binary file modified extension/apmconfigextension/extension-workflow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion extension/apmconfigextension/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (op *apmConfigExtension) Start(ctx context.Context, host component.Host) er
return err
}

return op.opampServer.Start(server.StartSettings{ListenEndpoint: op.extensionConfig.OpAMP.Server.Endpoint, Settings: server.Settings{Callbacks: *newRemoteConfigCallbacks(remoteConfigClient, op.telemetrySettings.Logger)}})
return op.opampServer.Start(server.StartSettings{ListenEndpoint: op.extensionConfig.OpAMP.Server.Endpoint, Settings: server.Settings{Callbacks: *newRemoteConfigCallbacks(remoteConfigClient, op.telemetrySettings.Logger).Callbacks}})
}

func (op *apmConfigExtension) Shutdown(ctx context.Context) error {
Expand Down
2 changes: 1 addition & 1 deletion extension/apmconfigextension/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func createExtension(_ context.Context, set extension.Settings, cfg component.Co
}()

wg.Wait()
return centralconfig.NewFetcherAPMWatcher(fetcher, extCfg.AgentConfig.CacheDuration, telemetry.Logger), nil
return centralconfig.NewFetcherAPMWatcher(fetcher, telemetry.Logger), nil
}
return newApmConfigExtension(cfg.(*Config), set, elasticsearchRemoteConfig), nil
}
3 changes: 1 addition & 2 deletions extension/apmconfigextension/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ require (
github.com/open-telemetry/opamp-go v0.19.0
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
go.opentelemetry.io/collector/pdata v1.31.0 // indirect
go.opentelemetry.io/collector/semconv v0.125.0
go.opentelemetry.io/otel v1.35.0 // indirect
go.opentelemetry.io/otel v1.35.0
go.opentelemetry.io/otel/metric v1.35.0 // indirect
go.opentelemetry.io/otel/trace v1.35.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions extension/apmconfigextension/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,6 @@ go.opentelemetry.io/collector/pdata v1.31.0 h1:P5WuLr1l2JcIvr6Dw2hl01ltp2ZafPnC4
go.opentelemetry.io/collector/pdata v1.31.0/go.mod h1:m41io9nWpy7aCm/uD1L9QcKiZwOP0ldj83JEA34dmlk=
go.opentelemetry.io/collector/pipeline v0.125.0 h1:oitBgcAFqntDB4ihQJUHJSQ8IHqKFpPkaTVbTYdIUzM=
go.opentelemetry.io/collector/pipeline v0.125.0/go.mod h1:TO02zju/K6E+oFIOdi372Wk0MXd+Szy72zcTsFQwXl4=
go.opentelemetry.io/collector/semconv v0.125.0 h1:SyRP617YGvNSWRSKMy7Lbk9RaJSR+qFAAfyxJOeZe4s=
go.opentelemetry.io/collector/semconv v0.125.0/go.mod h1:te6VQ4zZJO5Lp8dM2XIhDxDiL45mwX0YAQQWRQ0Qr9U=
go.opentelemetry.io/contrib/bridges/otelzap v0.10.0 h1:ojdSRDvjrnm30beHOmwsSvLpoRF40MlwNCA+Oo93kXU=
go.opentelemetry.io/contrib/bridges/otelzap v0.10.0/go.mod h1:oTTm4g7NEtHSV2i/0FeVdPaPgUIZPfQkFbq0vbzqnv0=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0 h1:sbiXRNDSWJOTobXh5HyQKjq6wUC5tNybqjIqDpAY4CU=
Expand Down
Loading