-
Notifications
You must be signed in to change notification settings - Fork 1.2k
sdk/log: self-observability: log created metric #7121
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
pellared
merged 13 commits into
open-telemetry:main
from
mahendrabishnoi2:logs-created-metrics
Aug 7, 2025
Merged
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0d400cc
copy feature flag code from sdk/trace/internal -> will discuss more o…
mahendrabishnoi2 75c8a0c
add doc comment about experimental metric (self observability)
mahendrabishnoi2 c2c73b6
- add support for `otel.sdk.log.created` metric, increment it when ne…
mahendrabishnoi2 f25ec73
- Fix the issue reported by a failed test case - SDKLogCreated.Int64C…
mahendrabishnoi2 7e4644b
test cases for SDKLogCreated metric
mahendrabishnoi2 379105c
go mod tidy
mahendrabishnoi2 60a3a7f
update CHANGELOG.md
mahendrabishnoi2 e13e4c0
Addressed review comments
mahendrabishnoi2 f30fea7
Addressed review comments
mahendrabishnoi2 ba25fe6
Addressed review comments - removed context from the test case setup …
mahendrabishnoi2 440f8a0
Merge branch 'main' into logs-created-metrics
pellared 1f201e3
addressed review comment
mahendrabishnoi2 d9bde60
Merge branch 'main' into logs-created-metrics
mahendrabishnoi2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,34 @@ | ||
| # Experimental Features | ||
|
|
||
| The Logs SDK contains features that have not yet stabilized in the OpenTelemetry specification. | ||
| These features are added to the OpenTelemetry Go Logs SDK prior to stabilization in the specification so that users can start experimenting with them and provide feedback. | ||
|
|
||
| These feature may change in backwards incompatible ways as feedback is applied. | ||
| See the [Compatibility and Stability](#compatibility-and-stability) section for more information. | ||
|
|
||
| ## Features | ||
|
|
||
| - [Self-Observability](#self-observability) | ||
|
|
||
| ### Self-Observability | ||
|
|
||
| The SDK provides a self-observability feature that allows you to monitor the SDK itself. | ||
|
|
||
| To opt-in, set the environment variable `OTEL_GO_X_SELF_OBSERVABILITY` to `true`. | ||
|
|
||
| When enabled, the SDK will create the following metrics using the global `MeterProvider`: | ||
|
|
||
| - `otel.sdk.log.created` | ||
|
|
||
| Please see the [Semantic conventions for OpenTelemetry SDK metrics] documentation for more details on these metrics. | ||
|
|
||
| [Semantic conventions for OpenTelemetry SDK metrics]: https://github.com/open-telemetry/semantic-conventions/blob/v1.36.0/docs/otel/sdk-metrics.md | ||
|
|
||
| ## Compatibility and Stability | ||
|
|
||
| Experimental features do not fall within the scope of the OpenTelemetry Go versioning and stability [policy](../../../../VERSIONING.md). | ||
| These features may be removed or modified in successive version releases, including patch versions. | ||
|
|
||
| When an experimental feature is promoted to a stable feature, a migration path will be included in the changelog entry of the release. | ||
| There is no guarantee that any environment variable feature flags that enabled the experimental feature will be supported by the stable version. | ||
| If they are supported, they may be accompanied with a deprecation notice stating a timeline for the removal of that support. | ||
mahendrabishnoi2 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,63 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // Package x documents experimental features for [go.opentelemetry.io/otel/sdk/log]. | ||
| package x // import "go.opentelemetry.io/otel/sdk/log/internal/x" | ||
|
|
||
| import ( | ||
| "os" | ||
| "strings" | ||
| ) | ||
|
|
||
| // SelfObservability is an experimental feature flag that determines if SDK | ||
| // self-observability metrics are enabled. | ||
| // | ||
| // To enable this feature set the OTEL_GO_X_SELF_OBSERVABILITY environment variable | ||
| // to the case-insensitive string value of "true" (i.e. "True" and "TRUE" | ||
| // will also enable this). | ||
| var SelfObservability = newFeature("SELF_OBSERVABILITY", func(v string) (string, bool) { | ||
| if strings.EqualFold(v, "true") { | ||
| return v, true | ||
| } | ||
| return "", false | ||
| }) | ||
|
|
||
| // Feature is an experimental feature control flag. It provides a uniform way | ||
| // to interact with these feature flags and parse their values. | ||
| type Feature[T any] struct { | ||
| key string | ||
| parse func(v string) (T, bool) | ||
| } | ||
|
|
||
| func newFeature[T any](suffix string, parse func(string) (T, bool)) Feature[T] { | ||
| const envKeyRoot = "OTEL_GO_X_" | ||
| return Feature[T]{ | ||
| key: envKeyRoot + suffix, | ||
| parse: parse, | ||
| } | ||
| } | ||
|
|
||
| // Key returns the environment variable key that needs to be set to enable the | ||
| // feature. | ||
| func (f Feature[T]) Key() string { return f.key } | ||
|
|
||
| // Lookup returns the user configured value for the feature and true if the | ||
| // user has enabled the feature. Otherwise, if the feature is not enabled, a | ||
| // zero-value and false are returned. | ||
| func (f Feature[T]) Lookup() (v T, ok bool) { | ||
| // https://github.com/open-telemetry/opentelemetry-specification/blob/62effed618589a0bec416a87e559c0a9d96289bb/specification/configuration/sdk-environment-variables.md#parsing-empty-value | ||
| // | ||
| // > The SDK MUST interpret an empty value of an environment variable the | ||
| // > same way as when the variable is unset. | ||
| vRaw := os.Getenv(f.key) | ||
| if vRaw == "" { | ||
| return v, ok | ||
| } | ||
| return f.parse(vRaw) | ||
| } | ||
|
|
||
| // Enabled reports whether the feature is enabled. | ||
| func (f Feature[T]) Enabled() bool { | ||
| _, ok := f.Lookup() | ||
| return ok | ||
| } |
This file contains hidden or 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,59 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package x | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestSelfObservability(t *testing.T) { | ||
| const key = "OTEL_GO_X_SELF_OBSERVABILITY" | ||
| require.Equal(t, key, SelfObservability.Key()) | ||
|
|
||
| t.Run("100", run(setenv(key, "100"), assertDisabled(SelfObservability))) | ||
| t.Run("true", run(setenv(key, "true"), assertEnabled(SelfObservability, "true"))) | ||
| t.Run("True", run(setenv(key, "True"), assertEnabled(SelfObservability, "True"))) | ||
| t.Run("false", run(setenv(key, "false"), assertDisabled(SelfObservability))) | ||
| t.Run("empty", run(assertDisabled(SelfObservability))) | ||
| } | ||
|
|
||
| func run(steps ...func(*testing.T)) func(*testing.T) { | ||
| return func(t *testing.T) { | ||
| t.Helper() | ||
| for _, step := range steps { | ||
| step(t) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func setenv(k, v string) func(t *testing.T) { //nolint:unparam // This is a reusable test utility function. | ||
| return func(t *testing.T) { t.Setenv(k, v) } | ||
| } | ||
|
|
||
| func assertEnabled[T any](f Feature[T], want T) func(*testing.T) { | ||
| return func(t *testing.T) { | ||
| t.Helper() | ||
| assert.True(t, f.Enabled(), "not enabled") | ||
|
|
||
| v, ok := f.Lookup() | ||
| assert.True(t, ok, "Lookup state") | ||
| assert.Equal(t, want, v, "Lookup value") | ||
| } | ||
| } | ||
|
|
||
| func assertDisabled[T any](f Feature[T]) func(*testing.T) { | ||
| var zero T | ||
| return func(t *testing.T) { | ||
| t.Helper() | ||
|
|
||
| assert.False(t, f.Enabled(), "enabled") | ||
|
|
||
| v, ok := f.Lookup() | ||
| assert.False(t, ok, "Lookup state") | ||
| assert.Equal(t, zero, v, "Lookup value") | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.