Files
tasks/common/rpc/test/http_test.go
Stephan BehnkeandGitHub 44529ec9cb OTEL workflow span attributes (#6699)
## What changed?
<!-- Describe what has changed in this PR -->

**(1)** Adds the following OpenTelemetry (OTEL) span attributes:
- `temporalWorkflowID`
- `temporalRunID`

These are the same OTEL attribute keys the SDKs emit. It makes sense to
me to make them the same for consistency.

**(2)** Replaces the deprecated approach of OTEL interceptors with
`stats.Handler`s

**(3)** If there are no exporters, disables the `stats.Handlers`
completely. Right now we run through the OTEL processing even if it's
disabled.

## Why?
<!-- Tell your future self why have you made these changes -->

Right now there is no way to query all spans for a workflow together.

The newly added span attribute allows to correlate spans that belong to
the same workflow execution.

## How did you test it?
<!-- How have you verified this change? Tested locally? Added a unit
test? Checked in staging env? -->

(1) Added unit tests

(2) Manually

Using 
```
docker run -p 127.0.0.1:4317:4317 -p 127.0.0.1:55679:55679 otel/opentelemetry-collector:0.106.1 2>&1 | tee collector-output.txt
```
it outputs

<img width="591" alt="image"
src="https://github.com/user-attachments/assets/0a1dfcc3-b3ae-49b7-bd0b-9667e8ef3945">

## Potential risks
<!-- Assuming the worst case, what can be broken when deploying this
change to production? -->

It appears to be **no** breaking change. (ie same OTEL attributes as
before)

<img width="2011" alt="Screenshot 2024-10-26 at 10 00 41 AM"
src="https://github.com/user-attachments/assets/c871eeac-ed7b-428e-9c0a-425c796d8761">

## Documentation
<!-- Have you made sure this change doesn't falsify anything currently
stated in `docs/`? If significant
new behavior is added, have you described that in `docs/`? -->

## Is hotfix candidate?
<!-- Is this PR a hotfix candidate or does it require a notification to
be sent to the broader community? (Yes/No) -->
2024-11-04 16:45:07 +00:00

138 lines
4.4 KiB
Go

// The MIT License
//
// Copyright (c) 2024 Temporal Technologies Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package rpc
import (
"net"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"github.com/stretchr/testify/require"
"go.temporal.io/server/common/membership"
"go.temporal.io/server/common/primitives"
"go.temporal.io/server/common/rpc"
"go.uber.org/mock/gomock"
)
func TestCreateLocalFrontendHTTPClient_UsingMembership(t *testing.T) {
ctrl := gomock.NewController(t)
monitor := membership.NewMockMonitor(ctrl)
resolver := membership.NewMockServiceResolver(ctrl)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
}))
defer srv.Close()
addr := srv.Listener.Addr()
_, portStr, err := net.SplitHostPort(addr.String())
require.NoError(t, err)
port, err := strconv.ParseInt(portStr, 10, 64)
require.NoError(t, err)
monitor.EXPECT().GetResolver(primitives.FrontendService).Return(resolver, nil)
resolver.EXPECT().AvailableMembers().Return([]membership.HostInfo{membership.NewHostInfoFromAddress(addr.String())})
fact := rpc.NewFactory(
nil,
primitives.HistoryService,
nil, // No logger
nil,
membership.GRPCResolverURLForTesting(monitor, primitives.FrontendService),
membership.GRPCResolverURLForTesting(monitor, primitives.FrontendService),
int(port),
nil, // No TLS
nil,
monitor,
)
client, err := fact.CreateLocalFrontendHTTPClient()
require.NoError(t, err)
require.Equal(t, "internal", client.Address)
require.Equal(t, "http", client.Scheme)
res, err := client.Get(srv.URL)
require.NoError(t, err)
defer res.Body.Close()
require.Equal(t, http.StatusOK, res.StatusCode)
}
func TestCreateLocalFrontendHTTPClient_UsingFixedHostPort(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
}))
defer srv.Close()
addr := srv.Listener.Addr()
fact := rpc.NewFactory(
nil, // unused
primitives.HistoryService,
nil, // No logger
nil,
membership.GRPCResolverURLForTesting(nil, primitives.FrontendService),
addr.String(),
0, // Port is unused
nil, // No TLS
nil,
nil, // monitor should not be used
)
client, err := fact.CreateLocalFrontendHTTPClient()
require.NoError(t, err)
require.Equal(t, addr.String(), client.Address)
require.Equal(t, "http", client.Scheme)
res, err := client.Get(srv.URL)
require.NoError(t, err)
defer res.Body.Close()
require.Equal(t, http.StatusOK, res.StatusCode)
}
func TestCreateLocalFrontendHTTPClient_UsingFixedHostPort_AndTLS(t *testing.T) {
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
}))
defer srv.Close()
addr := srv.Listener.Addr()
tlsConfig := srv.Client().Transport.(*http.Transport).TLSClientConfig
fact := rpc.NewFactory(
nil, // unused
primitives.HistoryService,
nil, // No logger
nil,
membership.GRPCResolverURLForTesting(nil, primitives.FrontendService),
addr.String(),
0, // Port is unused
tlsConfig,
nil,
nil, // monitor should not be used
)
client, err := fact.CreateLocalFrontendHTTPClient()
require.NoError(t, err)
require.Equal(t, addr.String(), client.Address)
require.Equal(t, "https", client.Scheme)
res, err := client.Get(srv.URL)
require.NoError(t, err)
defer res.Body.Close()
require.Equal(t, http.StatusOK, res.StatusCode)
}