Skip to content
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

Optionally include pipeline build span attributes in related step spans #811

Closed
1davidmichael opened this issue Feb 28, 2024 · 3 comments
Closed
Labels
enhancement New feature or request

Comments

@1davidmichael
Copy link

What feature do you want to see added?

We are currently using this plugin within our Jenkins and we have multiple steps or stages that share the same name. We would like the ability to track information such as the # of times called or duration of the stages based on the associated pipeline. We are currently unable to do this because step spans do not include the ci.pipeline.name or ci.pipeline.id.

Is it possible to allow some of these to carry on into the step spans? Or is there another solution to link Root Build spans and stage spans together? I've tried doing something like this but haven't had luck with it:

withSpanAttribute(key: "custom_pipeline_name", value: "${env.JOB_NAME}")

https://github.com/jenkinsci/opentelemetry-plugin/blob/main/docs/job-traces.md

Upstream changes

No response

Are you interested in contributing this feature?

No response

@1davidmichael 1davidmichael added the enhancement New feature or request label Feb 28, 2024
@kuisathaverat
Copy link
Contributor

kuisathaverat commented Mar 15, 2024

The trace.id atribute is the same in all the spans in the same transaction(pipeline build), that field will relate all of them.

The following filter in discover will return all the pipeline steps of a pipeline, notice that we filter the logs events that are also related to the same trace.id.

trace.id : b23ef79dc016ab89e24485853d954c1e and not data_stream.type: "logs" 

@christophe-kamphaus-jemmic
Copy link
Contributor

Please take a look at my PR, it might have what you are looking for:

with it you can write

String trimSuffix(String original, String suffix) {
    if (original.endsWith(suffix)) {
        return original.substring(0, original.length() - suffix.length());
    }
    return original;
}

String trimWithDefault(String original, String suffix, String _default) {
    String trimmed = trimSuffix(original, suffix);
    return trimmed.length()==0 ? _default : trimSuffix(trimmed, "/");
}

pipeline {
    options {
        // set these attributes on the root span / trace and (almost) all child spans
        withSpanAttributes([
            spanAttribute(key: "custom.pipeline.job.group", value: trimWithDefault(env.JOB_NAME, env.JOB_BASE_NAME, "-"), target: 'PIPELINE_ROOT_SPAN'), // useful for filtering with multibranch jobs
            spanAttribute(key: "custom.pipeline.job.name", value: env.JOB_NAME, target: 'PIPELINE_ROOT_SPAN'),
            spanAttribute(key: "custom.pipeline.job.id", value: env.BUILD_NUMBER, target: 'PIPELINE_ROOT_SPAN'),
            spanAttribute(key: "custom.pipeline.job.url", value: env.BUILD_URL, target: 'PIPELINE_ROOT_SPAN')
            // at this point in time env.GIT_COMMIT is not yet defined
        ])
    }
    stages {
        stage('build') {
            // additionally set this attribute only on the root span
            setSpanAttributes([spanAttribute(key: "custom.pipeline.job.commit", value: env.GIT_COMMIT, target: 'PIPELINE_ROOT_SPAN')])
            // ...
            withSpanAttributes([
                spanAttribute(key: "custom.step.tag", value: "xyz")
            ]) { // set this custom attribute only on the 'sh' span below
                sh '...'
            }
        }
    }
}

@kuisathaverat
Copy link
Contributor

the latest versions has the withSpanAttributes step that allow to add custom attribute context, for all the rest use trace.id and trace.parent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants